markov.go: a few changes
This commit is contained in:
parent
abdab70ce1
commit
4388bc22b8
|
@ -187,15 +187,24 @@ func (c *MarkovChain) Generate(n int, in string) string {
|
|||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
p := make(MarkovPrefix, c.prefixLen)
|
||||
var p MarkovPrefix
|
||||
var words []string
|
||||
var start string
|
||||
for attempt := 0; attempt < 10; attempt++ {
|
||||
/*
|
||||
p = make(MarkovPrefix, c.prefixLen)
|
||||
p = strings.Split(in, " ")
|
||||
if len(p) > c.prefixLen {
|
||||
i := rand.Intn(len(p) - 1)
|
||||
i := rand.Intn(len(p) - 2)
|
||||
p = p[i : i+c.prefixLen]
|
||||
}
|
||||
prefix := p.String()
|
||||
xlog.Debug("Looking for answer on [%s]", prefix)
|
||||
var words []string
|
||||
*/
|
||||
p = make(MarkovPrefix, 1)
|
||||
inWords := strings.Split(in, " ")
|
||||
start = inWords[rand.Intn(len(inWords))]
|
||||
p[0] = start
|
||||
//ss = p.String()
|
||||
xlog.Debug("Looking for answer on [%s]", start)
|
||||
for i := 0; i < n; i++ {
|
||||
choices := c.MarkovChain[p.String()]
|
||||
if len(choices) == 0 {
|
||||
|
@ -208,13 +217,17 @@ func (c *MarkovChain) Generate(n int, in string) string {
|
|||
}
|
||||
p.Shift(next)
|
||||
}
|
||||
prefix = strings.Trim(prefix, " ")
|
||||
if len(words) > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
start = strings.Trim(start, " ")
|
||||
if len(words) == 0 {
|
||||
xlog.Debug("No answer found")
|
||||
return prefix + " ... pfrrrz"
|
||||
return start + " ... pfrrrz"
|
||||
} else {
|
||||
xlog.Debug("Found words: [%s]", strings.Join(words, " "))
|
||||
return prefix + " " + strings.Join(words, " ")
|
||||
return start + " " + strings.Join(words, " ")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue