From fac71aad22f2da2aeae327f4446ef67e32e4bae9 Mon Sep 17 00:00:00 2001 From: Thomas Coppi Date: Thu, 13 Sep 2007 23:02:28 -0600 Subject: [PATCH] Save and load hash tables states with Pickle --- modules/markov.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/modules/markov.py b/modules/markov.py index 8a15592..b6f1113 100644 --- a/modules/markov.py +++ b/modules/markov.py @@ -5,6 +5,7 @@ #names and such will be similar. import random +import pickle statetab = {} @@ -13,9 +14,35 @@ word1 = word2 = " " def __init__(scrap): scrap.register_msg_event(markov_learn) scrap.register_msg_event(markov_talk) + scrap.register_msg_event(markov_load) + scrap.register_msg_event(markov_dump) random.seed() +def markov_load(c,list,bot): + global statetab + + cmd = list[4].split(" ")[0] + + if cmd == "mkload": + fp = list[4].split(" ")[1] + + pkfile = open(fp,"r") + + statetab = pickle.load(pkfile) + +def markov_dump(c,list,bot): + global statetab + + cmd = list[4].split(" ")[0] + + if cmd == "mkdump": + fp = list[4].split(" ")[1] + + pkfile = open(fp,"w+") + + pickle.dump(statetab,pkfile) + def markov_learn(c,list,bot): """ Should not be called directly """ words = list[4].split(" ") @@ -45,6 +72,7 @@ def emit_chain(key): global statetab global word1 + i = 0 retval = "" if key != " ": @@ -74,6 +102,11 @@ def emit_chain(key): retval = retval + suffix[t] + " " + i = i + 1 + + if i >= 100: + return retval + return retval def markov_talk(c,list,bot): -- 2.11.4.GIT