From 450abd2cedba37421f62d7931cac70dd42e54909 Mon Sep 17 00:00:00 2001 From: Andreas Neue Date: Thu, 3 Nov 2016 18:04:40 +0100 Subject: [PATCH] Several changes in answer generation in markov module --- modules/markov.go | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/modules/markov.go b/modules/markov.go index b56d8d6..7dbc983 100644 --- a/modules/markov.go +++ b/modules/markov.go @@ -44,7 +44,7 @@ func markovHandleMessage(m *irc.Message) { answerLen, _ := strconv.Atoi(ModParams["markov-answer-len"]) respChance, _ := strconv.Atoi(ModParams["markov-response-chance"]) - if rand.Intn(100) <= respChance || strings.HasPrefix(text, ModParams["_nick"]) { + if rand.Intn(100) <= respChance || strings.Index(text, ModParams["_nick"]) != -1 { responseText := markovChain.Generate(answerLen, text) if responseText != "" { go func() { @@ -161,6 +161,10 @@ func markovNewChain(prefixLen int) *MarkovChain { // Write parses the bytes into prefixes and suffixes that are stored in MarkovChain. 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) + } sr := strings.NewReader(in) p := make(MarkovPrefix, c.prefixLen) for { @@ -180,24 +184,20 @@ func (c *MarkovChain) Write(in string) (int, error) { // Generate returns a string of at most n words generated from MarkovChain. 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) + } c.mu.Lock() defer c.mu.Unlock() p := make(MarkovPrefix, c.prefixLen) p = strings.Split(in, " ") - for { - if len(p) == c.prefixLen { - break - } - if len(p) < c.prefixLen { - p = append(p, "") - } - if len(p) > c.prefixLen { - if rand.Intn(2) == 0 { - p = p[1:] - } else { - p = p[0 : len(p)-1] - } - } + 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() var words []string @@ -208,6 +208,9 @@ func (c *MarkovChain) Generate(n int, in string) string { } next := choices[rand.Intn(len(choices))] words = append(words, next) + if strings.HasSuffix(next, ".") || strings.HasSuffix(next, "!") || strings.HasSuffix(next, "?") { + break + } p.Shift(next) } if len(words) == 0 {