[Otfbot-svn] r494 - in trunk: examples lib plugins/ircClient

allo at BerliOS allo
So Nov 16 22:50:08 CET 2008


Author: allo
Date: 2008-11-16 22:50:07 +0100 (Sun, 16 Nov 2008)
New Revision: 494

Added:
   trunk/examples/eliza.yaml
   trunk/lib/eliza.py
Modified:
   trunk/plugins/ircClient/kiMod.py
Log:
eliza ki


Added: trunk/examples/eliza.yaml
===================================================================
--- trunk/examples/eliza.yaml	2008-11-16 21:13:22 UTC (rev 493)
+++ trunk/examples/eliza.yaml	2008-11-16 21:50:07 UTC (rev 494)
@@ -0,0 +1,25 @@
+- am: are
+  are: am
+  i: you
+  i'd: you would
+  i'll: you will
+  i've: you have
+  me: you
+  my: your
+  was: were
+  you: me
+  you'll: I will
+  you've: I have
+  your: my
+  yours: mine
+- hi$:
+  - hi!
+  - hello!
+  i am (.*):
+  - I am %s, too!
+  - I really like people, which are %s!
+  - I wish i were %s.
+  (.*):
+  - I do not know how to answer to this
+  - I do not understand
+  - what do you mean with that?

Added: trunk/lib/eliza.py
===================================================================
--- trunk/lib/eliza.py	2008-11-16 21:13:22 UTC (rev 493)
+++ trunk/lib/eliza.py	2008-11-16 21:50:07 UTC (rev 494)
@@ -0,0 +1,23 @@
+import random, re
+class eliza:
+	reflections={}
+	patterns={}
+	def setReflections(self, refl):
+		self.reflections=refl
+	def setPatterns(self, pat):
+		self.patterns={}
+		for key in pat.keys():
+			self.patterns[re.compile(key, re.I)]=pat[key]
+	def reply(self, input):
+		for regex in self.patterns.keys():
+			match=regex.match(input)
+			if match:
+				answer=random.choice(self.patterns[regex])
+				if "%s" in answer:
+					answer=answer%match.groups()
+				if answer[-2] in '.?!':
+					for refl in self.reflections.keys():
+						answer=answer.replace(refl, self.reflections[refl])
+					answer=answer[:-1]
+				return answer
+

Modified: trunk/plugins/ircClient/kiMod.py
===================================================================
--- trunk/plugins/ircClient/kiMod.py	2008-11-16 21:13:22 UTC (rev 493)
+++ trunk/plugins/ircClient/kiMod.py	2008-11-16 21:50:07 UTC (rev 494)
@@ -21,6 +21,8 @@
 import string, re, random, time, atexit, os.path
 import urllib, urllib2, socket
 import chatMod, functions
+from eliza import eliza
+import yaml
 
 MEGAHAL=1
 NIALL=1
@@ -132,6 +134,19 @@
 	def cleanup(self):
 		self.niall.cleanup()
 
+class elizaResponder(responder):
+	def __init__(self, bot, datadir):
+		self.eliza=eliza()
+		if os.path.exists(datadir+"/eliza.yaml"):
+			file=open(datadir+"/eliza.yaml")
+			data=file.read()
+			file.close()
+			tmp=yaml.load(data)
+			self.eliza.setReflections(tmp[0])
+			self.eliza.setPatterns(tmp[1])
+	def reply(self, msg):
+		return self.eliza.reply(msg)
+
 class megahalResponder(responder):
 	"""implements a responder based on the megahal ai-bot"""
 	def __init__(self, bot):
@@ -220,6 +235,8 @@
 			self.responder=webResponder(self.bot)
 		elif module=="udp":
 			self.responder=udpResponder(self.bot)
+		elif module=="eliza":
+			self.responder=elizaResponder(self.bot, datadir)
 		atexit.register(self.responder.cleanup)
 
 	def joined(self, channel):




Mehr Informationen über die Mailingliste Otfbot-dev