From 7fe45e2bc76451bc9dae0c4d1dca98bf1ab35d85 Mon Sep 17 00:00:00 2001 From: an Date: Mon, 26 Aug 2019 07:21:27 +0200 Subject: [PATCH] markov.go: slightly reworked --- modules/markov.go | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/modules/markov.go b/modules/markov.go index 02e5532..176f3ac 100644 --- a/modules/markov.go +++ b/modules/markov.go @@ -130,12 +130,8 @@ func (p MarkovPrefix) String() string { // Shift removes the first word from the Prefix and appends the given word. func (p MarkovPrefix) Shift(word string) { - if len(p) < *markovPrefixLen { - p = append(p, word) - } else { - copy(p, p[1:]) - p[len(p)-1] = word - } + copy(p, p[1:]) + p[len(p)-1] = word } // MarkovChain contains a map ("chain") of prefixes to a list of suffixes. @@ -155,6 +151,15 @@ func markovNewChain(prefixLen int) *MarkovChain { } } +func markovContains(a []string, s string) bool { + for _, v := range a { + if v == s { + return true + } + } + return false +} + // 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) @@ -164,17 +169,20 @@ func (c *MarkovChain) Write(in string) (int, error) { } sr := strings.NewReader(in) p := make(MarkovPrefix, c.prefixLen) + c.mu.Lock() + defer c.mu.Unlock() for { var s string if _, err := fmt.Fscan(sr, &s); err != nil { break } key := p.String() - 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) - fmt.Println(c.MarkovChain[key]) + if !markovContains(c.MarkovChain[key], s) { + c.MarkovChain[key] = append(c.MarkovChain[key], s) + xlog.Debug("Chain len: %d, learned [%s] [%s]", len(c.MarkovChain), key, s) + } else { + xlog.Debug("Chain len: %d, [%s] [%s] is already known", len(c.MarkovChain), key, s) + } p.Shift(s) } return len(in), nil @@ -193,19 +201,10 @@ func (c *MarkovChain) Generate(n int, in string) string { 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) - 2) - p = p[i : i+c.prefixLen] - } - */ - p = make(MarkovPrefix, 1) + p = make(MarkovPrefix, c.prefixLen) inWords := strings.Split(in, " ") start = inWords[rand.Intn(len(inWords))] - p[0] = start - //ss = p.String() + p.Shift(start) xlog.Debug("Looking for answer on [%s]", start) for i := 0; i < n; i++ { choices := c.MarkovChain[p.String()]