From 8264b47ed8519bde6fba056c1dd872f3db63204a Mon Sep 17 00:00:00 2001 From: arnaucode Date: Sat, 22 Apr 2017 14:43:45 +0200 Subject: [PATCH] generating Markov states bug fixed, starting the implementation of generating text --- main.go | 4 +++- markov.go | 56 +++++++++++++++++++++++++++++++++++++++---------------- text.txt | 4 ++++ 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index 8715212..6fd1fa6 100644 --- a/main.go +++ b/main.go @@ -39,7 +39,9 @@ option to select: ` break case "2": fmt.Println("selected 2 - Markov") - _ = markov.train("the", "text.txt") + states := markov.train("the", "text.txt") + generatedText := markov.generateText(states, "Argus", 20) + c.Green(generatedText) //fmt.Println(text) break case "0": diff --git a/markov.go b/markov.go index f2f8556..ca7bfe3 100644 --- a/markov.go +++ b/markov.go @@ -3,6 +3,7 @@ package main import ( "fmt" "io/ioutil" + "math/rand" "strconv" "strings" ) @@ -30,8 +31,7 @@ func addWordToStates(states []State, word string) ([]State, int) { iState = i } } - - if iState > 0 { + if iState >= 0 { states[iState].Count++ } else { var tempState State @@ -40,33 +40,32 @@ func addWordToStates(states []State, word string) ([]State, int) { states = append(states, tempState) iState = len(states) - 1 + } - //fmt.Println(iState) return states, iState } -func countWordsProb(words []string) { +func calcMarkovStates(words []string) []State { var states []State - totalWordsCount := 0 //count words - for i := 0; i < len(words); i++ { + for i := 0; i < len(words)-1; i++ { var iState int - totalWordsCount++ states, iState = addWordToStates(states, words[i]) - if iState != len(words)-1 { - states[iState].NextStates, _ = addWordToStates(states[iState].NextStates, words[iState+1]) + if iState < len(words) { + states[iState].NextStates, _ = addWordToStates(states[iState].NextStates, words[i+1]) } } //count prob for i := 0; i < len(states); i++ { - states[i].Prob = (float64(states[i].Count) / float64(totalWordsCount) * 100) + states[i].Prob = (float64(states[i].Count) / float64(len(words)) * 100) for j := 0; j < len(states[i].NextStates); j++ { - states[i].NextStates[j].Prob = (float64(states[i].NextStates[j].Count) / float64(totalWordsCount) * 100) + states[i].NextStates[j].Prob = (float64(states[i].NextStates[j].Count) / float64(len(words)) * 100) } } - fmt.Println("totalWordsCount: " + strconv.Itoa(totalWordsCount)) - fmt.Println(states) + fmt.Println("total words: " + strconv.Itoa(len(words))) + //fmt.Println(states) + return states } func textToWords(text string) []string { @@ -84,9 +83,34 @@ func readText(path string) (string, error) { return content, err } -func (markov Markov) train(firstWord string, path string) string { +func (markov Markov) train(firstWord string, path string) []State { text, _ := readText(path) words := textToWords(text) - countWordsProb(words) - return text + states := calcMarkovStates(words) + //fmt.Println(states) + + return states +} + +func getNextMarkovState(states []State, word string) string { + iState := -1 + for i := 0; i < len(states); i++ { + if states[i].Word == word { + iState = i + } + } + if iState < 0 { + return "word no exist on the memory" + } + fmt.Println(rand.Float64()) + return word +} +func (markov Markov) generateText(states []State, initWord string, count int) string { + var generatedText []string + word := initWord + for i := 0; i < count; i++ { + word = getNextMarkovState(states, word) + generatedText = append(generatedText, word) + } + return "a" } diff --git a/text.txt b/text.txt index 869c14e..c9b9206 100644 --- a/text.txt +++ b/text.txt @@ -1 +1,5 @@ Argus Panoptes (or Argos) is a many-eyed giant in Greek mythology. The figure is known for having spawned the saying "the eyes of Argus", as in to be "followed by", "trailed by", "watched by", et cetera the eyes; the saying is used to describe being subject to strict scrutiny in one's actions to an invasive, distressing degree. The monstrous entity has been either directly included or indirectly alluded to in a wide variety of works influenced by Greco-Roman thought over the past several centuries. +In the 5th century and later, Argus' wakeful alertness was explained for an increasingly literal culture as his having so many eyes that only a few of the eyes would sleep at a time: there were always eyes still awake. In the 2nd century AD Pausanias noted at Argos, in the temple of Zeus Larissaios, an archaic image of Zeus with a third eye in the center of his forehead, allegedly Priam's Zeus Herkeios purloined from Troy. +Argus was Hera's servant. His great service to the Olympian pantheon was to slay the chthonic serpent-legged monster Echidna as she slept in her cave. Hera's defining task for Argus was to guard the white heifer Io from Zeus, keeping her chained to the sacred olive tree at the Argive Heraion. She charged him to "Tether this cow safely to an olive-tree at Nemea". Hera knew that the heifer was in reality Io, one of the many nymphs Zeus was coupling with to establish a new order. To free Io, Zeus had Argus slain by Hermes. The messenger of the Olympian gods, disguised as a shepherd, first put all of Argus' eyes asleep with spoken charms, then slew him by hitting him with a stone, the first stain of bloodshed among the new generation of gods. + +The sacrifice of Argus liberated Io and allowed her to wander the earth, although tormented by a gadfly sent by Hera.