Skip to content

Instantly share code, notes, and snippets.

@dpflug
Created November 19, 2013 04:26
Show Gist options
  • Save dpflug/7540315 to your computer and use it in GitHub Desktop.
Save dpflug/7540315 to your computer and use it in GitHub Desktop.
A plugin for the Yakr IRC bot that may eventually mimic alex from IFMUD. https://github.com/theepicsnail/Yakr http://pages.cs.wisc.edu/~dbs/ifmud/alex.html
from yakr.plugin_base import *
from re import match
import pickle
set_command_prefix("")
ALEX_STORE = "AlexStore.pkl"
ALEX_CACHE = {}
def start():
global ALEX_CACHE
try:
ALEX_CACHE = pickle.load(file(ALEX_STORE))
except:
ALEX_CACHE = {}
def stop():
pickle.dump(ALEX_CACHE, file(ALEX_STORE, "w"))
class AlexKnowledge(object):
def define(self, word, isare, definition):
lword = word.lower()
if hasattr(self, "representation") and lword != self.representation.lower():
return "I'm confused. I thought we were talking about {}".format(self.representation)
if lword in ALEX_CACHE:
known = ALEX_CACHE[lword]
if definition in known.definition:
return "Oh, I'm aware. ;)"
else:
known.definition.append(definition)
return "If you say so, {}{} {}. I trust you.".format(known.representation, known.isare, definition)
else:
self.definition = [definition]
self.representation = word
self.isare = isare
ALEX_CACHE[lword] = self
return "Ok. {}{} {}.".format(self.representation, self.isare, self.definition[0])
def undefine(self, word, definition):
lword = word.lower()
if hasattr(self, "representation") and lword != self.representation.lower():
return "I'm confused. I thought we were talking about {}".format(self.representation)
if lword in ALEX_CACHE:
known = ALEX_CACHE[lword]
if definition not in known.definition:
return "I'm sorry. I don't know that {}{} {}.".format(known.representation, known.isare, definition)
elif len(known.definition) <= 1:
del known
return "Ok, I've forgotten everything I knew about {}.".format(lword)
else:
known.definition.remove(definition)
return "Ok, I've forgotten that {}{} {}.".format(known.representation, known.isare, definition)
else:
return "I'm sorry. I don't know anything about {}".format(lword)
def say(self):
outstring = "From what I hear, {}{} {}.".format(self.representation, self.isare, self.definition[0])
if len(self.definition) > 1:
for d in self.definition[1:]:
outstring += " Also, {}.".format(d)
return outstring
@command("what(('s|'re| is| are) (.*?))\??$")
def lookup(who, what, where):
"""
Look up the word/phrase in current knowledge and return the definitions.
"""
m = re.match("('s|'re|is|are) (.*)", what)
if not m:
return
else:
mg = m.groups()
lwhat = mg[1].lower()
if lwhat in ALEX_CACHE:
say(where, who + ": " + ALEX_CACHE[lwhat].say())
else:
say(where, who + ": Well, I don't know, but feel free to teach me when you find out! (\"!learn foo is bar\" or \"!learn quxes are boors\")")
@command("(\w*)\?$")
def lookup2(who, what, where):
lwhat = what.lower()
if lwhat in ALEX_CACHE:
say(where, who + ": " + ALEX_CACHE[lwhat].say())
@command("!learn ((.*)('s|'re| is| are)(.*?)[.!?]?$)")
def define(who, what, where):
thisdef = match("(?P<forget>forget that )?(?P<word>.*)(?P<isare>('s|'re| is| are)) (?P<definition>.*)", what).groupdict()
if thisdef and thisdef['forget']:
if thisdef["word"] in ALEX_CACHE:
ak = ALEX_CACHE[thisdef["word"]]
else:
ak = AlexKnowledge()
say(where, who + ": " + ak.undefine(thisdef["word"], thisdef["definition"]))
elif thisdef:
say(where, who + ": " + AlexKnowledge().define(thisdef["word"], thisdef["isare"], thisdef["definition"]))
else:
say(where, who + ": I'm sorry, I don't know what you mean. Try something like: !learn foo is bar")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment