Browse Source

generating Markov states bug fixed, starting the implementation of generating text

pull/1/head
arnaucode 7 years ago
parent
commit
8264b47ed8
3 changed files with 47 additions and 17 deletions
  1. +3
    -1
      main.go
  2. +40
    -16
      markov.go
  3. +4
    -0
      text.txt

+ 3
- 1
main.go

@ -39,7 +39,9 @@ option to select: `
break break
case "2": case "2":
fmt.Println("selected 2 - Markov") 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) //fmt.Println(text)
break break
case "0": case "0":

+ 40
- 16
markov.go

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand"
"strconv" "strconv"
"strings" "strings"
) )
@ -30,8 +31,7 @@ func addWordToStates(states []State, word string) ([]State, int) {
iState = i iState = i
} }
} }
if iState > 0 {
if iState >= 0 {
states[iState].Count++ states[iState].Count++
} else { } else {
var tempState State var tempState State
@ -40,33 +40,32 @@ func addWordToStates(states []State, word string) ([]State, int) {
states = append(states, tempState) states = append(states, tempState)
iState = len(states) - 1 iState = len(states) - 1
} }
//fmt.Println(iState)
return states, iState return states, iState
} }
func countWordsProb(words []string) {
func calcMarkovStates(words []string) []State {
var states []State var states []State
totalWordsCount := 0
//count words //count words
for i := 0; i < len(words); i++ {
for i := 0; i < len(words)-1; i++ {
var iState int var iState int
totalWordsCount++
states, iState = addWordToStates(states, words[i]) 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 //count prob
for i := 0; i < len(states); i++ { 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++ { 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 { func textToWords(text string) []string {
@ -84,9 +83,34 @@ func readText(path string) (string, error) {
return content, err return content, err
} }
func (markov Markov) train(firstWord string, path string) string {
func (markov Markov) train(firstWord string, path string) []State {
text, _ := readText(path) text, _ := readText(path)
words := textToWords(text) 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"
} }

+ 4
- 0
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. 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.

Loading…
Cancel
Save