-
Notifications
You must be signed in to change notification settings - Fork 0
/
markovChain.go
119 lines (96 loc) · 2.48 KB
/
markovChain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"strconv"
"strings"
)
type MarkovChain struct {
value string
count int
prob float64
nextChain []MarkovChain
}
func addWordToChain(chain []MarkovChain, word string) ([]MarkovChain, int) {
index := -1
for i := 0; i < len(chain); i++ {
if chain[i].value == word {
index = i
}
}
if index >= 0 {
chain[index].count++
} else {
var tempChain MarkovChain
tempChain.value = word
tempChain.count = 1
chain = append(chain, tempChain)
index = len(chain) - 1
}
return chain, index
}
func generateMarkovChain(words []string) []MarkovChain {
var chain []MarkovChain
var index int
for i := 0; i < len(words)-1; i++ {
chain, index = addWordToChain(chain, words[i])
if index < len(words){
chain[index].nextChain, _ = addWordToChain(chain[index].nextChain, words[i+1])
}
printLoading(i, len(words))
}
for i := 0; i < len(chain); i++{
chain[i].prob = float64(chain[i].count)/float64(len(words))*100
for j := 0; j < len(chain[i].nextChain); j++ {
chain[i].nextChain[j].prob = float64(chain[i].nextChain[j].count)/float64(len(words))*100
}
}
return chain
}
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 getNextWord(chain []MarkovChain, word string)string{
index := -1
for i := 0; i < len(chain); i++ {
if chain[i].value == word{
index = i
}
}
if index < 0 {
return "word doesn't exist in chain"
}
var nextWord MarkovChain
if chain[index].nextChain != nil{
nextWord = chain[index].nextChain[0]
for i := 0; i < len(chain[index].nextChain); i++ {
if chain[index].nextChain[i].prob > nextWord.prob {
nextWord = chain[index].nextChain[i]
}
}
}
return nextWord.value
}
func Train(text []string)[]MarkovChain{
chain := generateMarkovChain(text)
return chain
}
func GenerateMarkovText(chain []MarkovChain, startWord string, number int) string{
var generatedText []string
word := startWord
generatedText = append(generatedText, word)
for i := 0; i < number; i++ {
word = getNextWord(chain, word)
if word == "word doesn't exist in chain" {
return "word doesn't exist in chain"
}
generatedText = append(generatedText, word)
}
return strings.Join(append(generatedText, "."), " ")
}