From 13e26f1c71a156dc7f616b38448e8c6a3156bdec Mon Sep 17 00:00:00 2001 From: arnaucode Date: Sat, 22 Apr 2017 21:44:14 +0200 Subject: [PATCH] added progress bar on generating Markov states --- main.go | 7 ++++--- markov.go | 13 +++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 840d76c..85c9cfa 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,7 @@ import ( "strings" ) -func readText(path string) (string, error) { +func readTxt(path string) (string, error) { data, err := ioutil.ReadFile(path) if err != nil { //Do something @@ -38,10 +38,11 @@ func main() { fmt.Println("incorrect entry, need a positive number") } - text, _ := readText("text.txt") + text, _ := readTxt("text.txt") - fmt.Println("generating text") + fmt.Println("generating markov chains") states := markov.train(text) + fmt.Println("generating text") generatedText := markov.generateText(states, firstWord, count) fmt.Println("") fmt.Println(generatedText) diff --git a/markov.go b/markov.go index b6fa06d..0c7ad90 100644 --- a/markov.go +++ b/markov.go @@ -18,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++ { @@ -48,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