From 60ae0392198144ecb0da906ce31296d10f332324 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Sat, 22 Apr 2017 12:59:00 +0200 Subject: [PATCH] started markov, get the probabilities of states and nextStates --- main.go | 9 +++++- markov.go | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ text.txt | 1 + 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 markov.go create mode 100644 text.txt diff --git a/main.go b/main.go index b22de2a..8715212 100644 --- a/main.go +++ b/main.go @@ -16,12 +16,14 @@ func main() { c.Purple("https://github.com/arnaucode/twFlock") fmt.Println("version " + version) fmt.Println("Reading flockConfig.json file") - flock := readConfigTokensAndConnect() + //flock := readConfigTokensAndConnect() + var flock Flock fmt.Println("---------------") newcommand := bufio.NewReader(os.Stdin) fmt.Print("Please select command number") options := ` 1 - Tweet from flock of bots + 2 - Markov 0 - Exit script option to select: ` for { @@ -35,6 +37,11 @@ option to select: ` fmt.Println("selected 1 - Tweet from flock of bots") optionTweetFromFlock(flock) break + case "2": + fmt.Println("selected 2 - Markov") + _ = markov.train("the", "text.txt") + //fmt.Println(text) + break case "0": fmt.Println("selected 0 - exit script") os.Exit(3) diff --git a/markov.go b/markov.go new file mode 100644 index 0000000..f2f8556 --- /dev/null +++ b/markov.go @@ -0,0 +1,92 @@ +package main + +import ( + "fmt" + "io/ioutil" + "strconv" + "strings" +) + +type Markov struct{} + +/*type NextState struct { + Word string + Count int + Prob float64 +}*/ +type State struct { + Word string + Count int + Prob float64 + NextStates []State +} + +var markov Markov + +func addWordToStates(states []State, word string) ([]State, int) { + iState := -1 + for i := 0; i < len(states); i++ { + if states[i].Word == word { + iState = i + } + } + + if iState > 0 { + states[iState].Count++ + } else { + var tempState State + tempState.Word = word + tempState.Count = 1 + + states = append(states, tempState) + iState = len(states) - 1 + } + //fmt.Println(iState) + return states, iState +} + +func countWordsProb(words []string) { + var states []State + totalWordsCount := 0 + //count words + for i := 0; i < len(words); 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]) + } + } + + //count prob + for i := 0; i < len(states); i++ { + states[i].Prob = (float64(states[i].Count) / float64(totalWordsCount) * 100) + for j := 0; j < len(states[i].NextStates); j++ { + states[i].NextStates[j].Prob = (float64(states[i].NextStates[j].Count) / float64(totalWordsCount) * 100) + } + } + fmt.Println("totalWordsCount: " + strconv.Itoa(totalWordsCount)) + fmt.Println(states) +} + +func textToWords(text string) []string { + s := strings.Split(text, " ") + words := s + return words +} + +func readText(path string) (string, error) { + data, err := ioutil.ReadFile(path) + if err != nil { + //Do something + } + content := string(data) + return content, err +} + +func (markov Markov) train(firstWord string, path string) string { + text, _ := readText(path) + words := textToWords(text) + countWordsProb(words) + return text +} diff --git a/text.txt b/text.txt new file mode 100644 index 0000000..869c14e --- /dev/null +++ b/text.txt @@ -0,0 +1 @@ +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.