From 5f9238a433faf8a096ee32aa9a59f9b344c3d674 Mon Sep 17 00:00:00 2001 From: Andreas Neue Date: Sat, 5 Nov 2016 10:40:20 +0100 Subject: [PATCH] markov.go: learning after answering (prevents repition of input) --- main.go | 2 +- modules/markov.go | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 96c6dbd..666c480 100644 --- a/main.go +++ b/main.go @@ -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} diff --git a/modules/markov.go b/modules/markov.go index 509b471..5359f59 100644 --- a/modules/markov.go +++ b/modules/markov.go @@ -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, " ") } }