if no answer can be found, build sentence on empty string

This commit is contained in:
an 2020-02-14 12:57:19 +01:00
parent 5207b0fde0
commit 78056ade35
1 changed files with 6 additions and 8 deletions

View File

@ -222,7 +222,11 @@ func (c *MarkovChain) Generate(n int, in string) string {
for attempt := 0; attempt < 10; attempt++ {
p = make(MarkovPrefix, c.prefixLen)
inWords := strings.Split(in, " ")
start = inWords[rand.Intn(len(inWords))]
if attempt < 9 {
start = inWords[rand.Intn(len(inWords))]
} else {
start = ""
}
p.Shift(start)
xlog.Debug("Looking for answer on [%s]", start)
for i := 0; i < n; i++ {
@ -242,13 +246,7 @@ func (c *MarkovChain) Generate(n int, in string) string {
}
}
start = strings.Trim(start, " ")
if len(words) == 0 {
xlog.Debug("No answer found")
return start + " ... pfrrrz"
} else {
xlog.Debug("Found words: [%s]", strings.Join(words, " "))
return start + " " + strings.Join(words, " ")
}
return start + " " + strings.Join(words, " ")
}
// Save the chain to a file