markov.go: a few changes

This commit is contained in:
Andreas Neue 2016-11-27 00:34:25 +01:00
parent abdab70ce1
commit 4388bc22b8
1 changed files with 33 additions and 20 deletions

View File

@ -187,15 +187,24 @@ func (c *MarkovChain) Generate(n int, in string) string {
} }
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() 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, " ") p = strings.Split(in, " ")
if len(p) > c.prefixLen { if len(p) > c.prefixLen {
i := rand.Intn(len(p) - 1) i := rand.Intn(len(p) - 2)
p = p[i : i+c.prefixLen] p = p[i : i+c.prefixLen]
} }
prefix := p.String() */
xlog.Debug("Looking for answer on [%s]", prefix) p = make(MarkovPrefix, 1)
var words []string 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++ { for i := 0; i < n; i++ {
choices := c.MarkovChain[p.String()] choices := c.MarkovChain[p.String()]
if len(choices) == 0 { if len(choices) == 0 {
@ -208,13 +217,17 @@ func (c *MarkovChain) Generate(n int, in string) string {
} }
p.Shift(next) p.Shift(next)
} }
prefix = strings.Trim(prefix, " ") if len(words) > 0 {
break
}
}
start = strings.Trim(start, " ")
if len(words) == 0 { if len(words) == 0 {
xlog.Debug("No answer found") xlog.Debug("No answer found")
return prefix + " ... pfrrrz" return start + " ... pfrrrz"
} else { } else {
xlog.Debug("Found words: [%s]", strings.Join(words, " ")) xlog.Debug("Found words: [%s]", strings.Join(words, " "))
return prefix + " " + strings.Join(words, " ") return start + " " + strings.Join(words, " ")
} }
} }