Browse Source

added progress bar on generting markov chains

pull/1/head
arnaucode 7 years ago
parent
commit
feb90306eb
4 changed files with 86 additions and 42 deletions
  1. +9
    -25
      main.go
  2. +14
    -17
      markov.go
  3. +47
    -0
      optionTweetMarkov.go
  4. +16
    -0
      readTxt.go

+ 9
- 25
main.go

@ -4,7 +4,6 @@ import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
@ -17,8 +16,14 @@ func main() {
c.Purple("https://github.com/arnaucode/twFlock")
fmt.Println("version " + version)
fmt.Println("Reading flockConfig.json file")
//flock := readConfigTokensAndConnect()
var flock Flock
flock := readConfigTokensAndConnect()
c.Yellow("generating markov chains (may take some seconds)")
text, _ := readTxt("text.txt")
states := markov.train(text)
c.Green("markov chains generated")
//var flock Flock
fmt.Println("---------------")
newcommand := bufio.NewReader(os.Stdin)
fmt.Print("Please select command number")
@ -40,28 +45,7 @@ option to select: `
break
case "2":
fmt.Println("selected 2 - Markov")
fmt.Print("entry the first word: ")
newcommand := bufio.NewReader(os.Stdin)
firstWord, _ := newcommand.ReadString('\n')
firstWord = strings.TrimSpace(firstWord)
fmt.Print("first word: ")
c.Purple(firstWord)
c.Red("how many words you want on the text?")
newcommand = bufio.NewReader(os.Stdin)
answer, _ := newcommand.ReadString('\n')
answer = strings.TrimSpace(answer)
fmt.Print("Number of words on text to generate: ")
c.Purple(answer)
count, err := strconv.Atoi(answer)
if err != nil {
fmt.Println("incorrect entry, need a positive number")
}
states := markov.train("the", "text.txt")
generatedText := markov.generateText(states, firstWord, count)
c.Green(generatedText)
optionTweetMarkov(states)
break
case "0":
fmt.Println("selected 0 - exit script")

+ 14
- 17
markov.go

@ -2,7 +2,6 @@ package main
import (
"fmt"
"io/ioutil"
"math/rand"
"strconv"
"strings"
@ -10,11 +9,6 @@ import (
type Markov struct{}
/*type NextState struct {
Word string
Count int
Prob float64
}*/
type State struct {
Word string
Count int
@ -24,6 +18,17 @@ type State struct {
var markov Markov
func printLoading(n int, total int) {
var bar []string
tantPerFourty := int((float64(n) / float64(total)) * 40)
tantPerCent := int((float64(n) / float64(total)) * 100)
for i := 0; i < tantPerFourty; i++ {
bar = append(bar, "█")
}
progressBar := strings.Join(bar, "")
fmt.Printf("\r " + progressBar + " - " + strconv.Itoa(tantPerCent) + "%")
}
func addWordToStates(states []State, word string) ([]State, int) {
iState := -1
for i := 0; i < len(states); i++ {
@ -54,6 +59,8 @@ func calcMarkovStates(words []string) []State {
if iState < len(words) {
states[iState].NextStates, _ = addWordToStates(states[iState].NextStates, words[i+1])
}
printLoading(i, len(words))
}
//count prob
@ -74,18 +81,8 @@ func textToWords(text string) []string {
return words
}
func readText(path string) (string, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
//Do something
}
dataClean := strings.Replace(string(data), "\n", " ", -1)
content := string(dataClean)
return content, err
}
func (markov Markov) train(text string) []State {
func (markov Markov) train(firstWord string, path string) []State {
text, _ := readText(path)
words := textToWords(text)
states := calcMarkovStates(words)
//fmt.Println(states)

+ 47
- 0
optionTweetMarkov.go

@ -0,0 +1,47 @@
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func optionTweetMarkov(states []State) {
fmt.Print("entry the first word: ")
newcommand := bufio.NewReader(os.Stdin)
firstWord, _ := newcommand.ReadString('\n')
firstWord = strings.TrimSpace(firstWord)
fmt.Print("first word: ")
c.Purple(firstWord)
c.Red("how many words you want on the text?")
newcommand = bufio.NewReader(os.Stdin)
answer, _ := newcommand.ReadString('\n')
answer = strings.TrimSpace(answer)
fmt.Print("Number of words on text to generate: ")
c.Purple(answer)
count, err := strconv.Atoi(answer)
if err != nil {
fmt.Println("incorrect entry, need a positive number")
}
c.Red("how many sentences you want to generate?")
newcommand = bufio.NewReader(os.Stdin)
answer, _ = newcommand.ReadString('\n')
answer = strings.TrimSpace(answer)
fmt.Print("Number of sentences to generate: ")
c.Purple(answer)
sentences, err := strconv.Atoi(answer)
if err != nil {
fmt.Println("incorrect entry, need a positive number")
}
fmt.Println("generating text")
for i := 0; i < sentences; i++ {
generatedText := markov.generateText(states, firstWord, count)
c.Green(generatedText)
}
}

+ 16
- 0
readTxt.go

@ -0,0 +1,16 @@
package main
import (
"io/ioutil"
"strings"
)
func readTxt(path string) (string, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
//Do something
}
dataClean := strings.Replace(string(data), "\n", " ", -1)
content := string(dataClean)
return content, err
}

Loading…
Cancel
Save