markov.go: learning after answering (prevents repition of input)

This commit is contained in:
Andreas Neue 2016-11-05 10:40:20 +01:00
parent 0f080a2946
commit 5f9238a433
2 changed files with 12 additions and 11 deletions

View File

@ -45,7 +45,7 @@ var (
func main() {
sayCh = make(chan string, 1024)
xlog.Init(xlog.INFO)
xlog.Init(xlog.DEBUG)
//bot := ircx.Classic(*server, *name)
cfg := ircx.Config{User: *name, MaxRetries: 1000}

View File

@ -37,11 +37,8 @@ func markovHandleMessage(m *irc.Message) {
if text == "" {
return
}
text = markovParseText(text)
markovChain.Write(text)
answerLen, _ := strconv.Atoi(ModParams["markov-answer-len"])
respChance, _ := strconv.Atoi(ModParams["markov-response-chance"])
if rand.Intn(100) <= respChance || strings.Index(text, strings.ToLower(ModParams["_nick"])) != -1 {
@ -53,6 +50,8 @@ func markovHandleMessage(m *irc.Message) {
}()
}
}
markovChain.Write(text)
}
func markovRun() {
@ -132,7 +131,7 @@ type MarkovPrefix []string
// String returns the Prefix as a string (for use as a map key).
func (p MarkovPrefix) String() string {
return strings.Join(p, " ")
return strings.Trim(strings.Join(p, " "), " ")
}
// Shift removes the first word from the Prefix and appends the given word.
@ -163,7 +162,7 @@ func (c *MarkovChain) Write(in string) (int, error) {
in = strings.ToLower(in)
if strings.HasPrefix(in, strings.ToLower(ModParams["_nick"])) {
tok := strings.Split(in, " ")
in = strings.Replace(in, tok[0]+" ", "", 0)
in = strings.Replace(in, tok[0]+" ", "", 1)
}
sr := strings.NewReader(in)
p := make(MarkovPrefix, c.prefixLen)
@ -176,6 +175,7 @@ func (c *MarkovChain) Write(in string) (int, error) {
c.mu.Lock()
c.MarkovChain[key] = append(c.MarkovChain[key], s)
c.mu.Unlock()
xlog.Debug("Chain len: %d, learned [%s] [%s]", len(c.MarkovChain), key, s)
p.Shift(s)
}
return len(in), nil
@ -186,20 +186,18 @@ func (c *MarkovChain) Generate(n int, in string) string {
in = strings.ToLower(in)
if strings.HasPrefix(in, strings.ToLower(ModParams["_nick"])) {
tok := strings.Split(in, " ")
in = strings.Replace(in, tok[0]+" ", "", 0)
in = strings.Replace(in, tok[0]+" ", "", 1)
}
c.mu.Lock()
defer c.mu.Unlock()
p := make(MarkovPrefix, c.prefixLen)
p = strings.Split(in, " ")
if len(p) < c.prefixLen {
p = append(p, "")
}
if len(p) > c.prefixLen {
i := rand.Intn(len(p) - 1)
p = p[i : i+c.prefixLen]
}
prefix := p.String()
xlog.Debug("Looking for answer on [%s]", prefix)
var words []string
for i := 0; i < n; i++ {
choices := c.MarkovChain[p.String()]
@ -213,9 +211,12 @@ func (c *MarkovChain) Generate(n int, in string) string {
}
p.Shift(next)
}
prefix = strings.Trim(prefix, " ")
if len(words) == 0 {
return ""
xlog.Debug("No answer found")
return prefix + " ... pfrrrz"
} else {
xlog.Debug("Found words: [%s]", strings.Join(words, " "))
return prefix + " " + strings.Join(words, " ")
}
}