class PluginTeachMe(Plugin): def __init__(self, *args): Plugin.__init__(self, *args) self.classifier = None self.load({}) self.curr_msg = '' self.last_msg = '' self.last_joke = () self.just_joked = False def load(self, data): storage_backend = MemoryBackend(data) self.classifier = NaiveBayesClassifier(storage_backend) def save(self): return self.classifier.storage.data def get_what_to_learn(self): if self.curr_msg in ('CMB', 'cmb'): return 'CMB' if self.curr_msg in ('CTB', 'ctb'): return 'CTB' if self.curr_msg in ('TWSS', 'twss'): return "That's what she said!" return 'None' def got_congratulated(self): return self.curr_msg in ('GG', 'gg', 'GG Tofbot', 'gg Tofbot') def did_bad_joke(self): return self.curr_msg in ('TG', 'tg', 'TG Tofbot', 'tg Tofbot') def handle_msg(self, msg_text, chan, nick): just_joked = self.just_joked self.just_joked = False self.last_msg = self.curr_msg self.curr_msg = msg_text.strip() if self.got_congratulated(): if self.last_joke: self.classifier.train(*self.last_joke) elif self.did_bad_joke(): if self.last_joke: self.classifier.train(self.last_joke[0], 'None') else: scores = self.classifier.classify(self.curr_msg.split()) joke = 'None' if scores: joke = scores[0][0] if joke != 'None': self.say(joke) self.last_joke = (self.curr_msg.split(), joke) else: if not just_joked: self.classifier.train(self.last_msg.split(), self.get_what_to_learn())
class PluginTeachMe(Plugin): def __init__(self, *args): Plugin.__init__(self, *args) self.classifier = None self.load({}) self.curr_msg = "" self.last_msg = "" self.last_joke = () self.just_joked = False def load(self, data): storage_backend = MemoryBackend(data) self.classifier = NaiveBayesClassifier(storage_backend) def save(self): return self.classifier.storage.data def get_what_to_learn(self): if self.curr_msg in ("CMB", "cmb"): return "CMB" if self.curr_msg in ("CTB", "ctb"): return "CTB" if self.curr_msg in ("TWSS", "twss"): return "That's what she said!" return "None" def got_congratulated(self): return self.curr_msg in ("GG", "gg", "GG Tofbot", "gg Tofbot") def did_bad_joke(self): return self.curr_msg in ("TG", "tg", "TG Tofbot", "tg Tofbot") def handle_msg(self, msg_text, chan, nick): just_joked = self.just_joked self.just_joked = False self.last_msg = self.curr_msg self.curr_msg = msg_text.strip() if self.got_congratulated(): if self.last_joke: self.classifier.train(*self.last_joke) elif self.did_bad_joke(): if self.last_joke: self.classifier.train(self.last_joke[0], "None") else: scores = self.classifier.classify(self.curr_msg.split()) joke = "None" if scores: joke = scores[0][0] if joke != "None": self.say(joke) self.last_joke = (self.curr_msg.split(), joke) else: if not just_joked: self.classifier.train(self.last_msg.split(), self.get_what_to_learn())
os.path.join(ROOT_DIR, 'data', 'emails'), 0.6) label_lookup = get_label_lookup( os.path.join(ROOT_DIR, 'data', 'labels.txt')) nb_classifier = NaiveBayesClassifier() training_data = [ (label_lookup[x], features_from_file(os.path.join(ROOT_DIR, 'data', 'emails', x))) for x in train ] nb_classifier.train(training_data) true_positive = true_negative = false_positive = false_negative = 0 for filename in test: predicted_label = nb_classifier.classify( features_from_file( os.path.join(ROOT_DIR, 'data', 'emails', filename)), 'spam', 'not_spam') if predicted_label == 'spam' and label_lookup[filename] == 'spam': true_positive += 1 if predicted_label == 'not_spam' and label_lookup[ filename] == 'not_spam': true_negative += 1 if predicted_label == 'spam' and label_lookup[filename] == 'not_spam': false_positive += 1 if predicted_label == 'not_spam' and label_lookup[filename] == 'spam': false_negative += 1 print "True Positives:", true_positive print "True Negatives:", true_negative print "False Positives:", false_positive print "False Negatives:", false_negative