summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsand <daniel@spatof.org>2013-05-16 13:39:00 (GMT)
committer sand <daniel@spatof.org>2013-05-16 13:39:00 (GMT)
commitbd7867465137cfc3a3ee396fb5037eeadef79f7f (patch)
tree514b18fb8f067763beff307aa58fafd429f28b57
parentca42ee8c1c65656ea371e74a235d9d2601f3ee98 (diff)
markov: seed iniziale per frasi
-rw-r--r--pinolo/plugins/markov.py24
1 files changed, 22 insertions, 2 deletions
diff --git a/pinolo/plugins/markov.py b/pinolo/plugins/markov.py
index ca244ea..5f16e91 100644
--- a/pinolo/plugins/markov.py
+++ b/pinolo/plugins/markov.py
@@ -114,12 +114,27 @@ class MarkovBrain(object):
weight = self.tokens[context].get(next_word, 0)
self.tokens[context][next_word] = (weight + 1)
+ def start_from_seed(self, seed):
+ tokens = self.lex(seed)
+ if len(tokens) < (self.context + 1):
+ return None
+
+ for context, next_word in self._sequence(tokens, self.context):
+ if context in self.tokens:
+ return context
+
+ return None
+
def say(self, seed=None, max_words=50):
# Empty database, we can't talk.
if not self.tokens:
return
-
- starter = random.choice(self.tokens.keys())
+
+ if seed:
+ starter = self.start_from_seed(seed)
+
+ if starter is None:
+ starter = random.choice(self.tokens.keys())
sequence = deque(tuple(starter))
sentence = list(starter)
@@ -181,3 +196,8 @@ class MarkovPlugin(Plugin):
if self._counter >= self.SAVE_EVERY:
self._counter = 0
self.markov.save()
+
+ if random.randint(0, 100) >= 92:
+ reply = self.markov.say(event.text)
+ if reply:
+ event.reply(reply, prefix=False)