From ec68d52e3c987d90191fe5413c8a0aa21b04099e Mon Sep 17 00:00:00 2001 From: Thomas Coppi Date: Fri, 7 Sep 2007 23:14:36 -0600 Subject: [PATCH] Markov.py actually semi-works, still some problems, probably needs to be rewritten too. --- modules/markov.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/modules/markov.py b/modules/markov.py index d5f7b92..b2c53e1 100644 --- a/modules/markov.py +++ b/modules/markov.py @@ -4,15 +4,24 @@ #"The Practice of Programming", so some of the variable #names and such will be similar. +import random + statetab = {} +word1 = word2 = " " + def __init__(scrap): scrap.register_msg_event(markov_learn) + scrap.register_msg_event(markov_talk) + + random.seed() def markov_learn(c,list,bot): """ Should not be called directly """ words = list[4].split(" ") w1 = w2 = " " + global statetab + global statetab_size for i in words: try: @@ -27,4 +36,41 @@ def markov_learn(c,list,bot): except KeyError: statetab[w1] = {} - print statetab +def emit_chain(key): + global statetab + global word1 + + retval = "" + +# for i in range(random.randint(1,4)): + for i in range(int(key)): + try: + suffix = statetab[word1] + except KeyError: + word1 = random.choice(statetab.keys()) + return "wah?" + + try: + t = random.choice(suffix.keys()) + except IndexError: + word1 = random.choice(statetab.keys()) + return "Not enough entropy." + + word1 = t + + retval = retval + suffix[t] + " " + + return retval + +def markov_talk(c,list,bot): + """ Makes the markov chain talk to you """ + + cmd = list[4].split(" ")[0] + + try: + key = list[4].split(" ")[1] + except IndexError: + key = 1 + + if list[3] and cmd == "talk": + c.privmsg(list[5],"%s" % emit_chain(key)) -- 2.11.4.GIT