Advertisement
Guest User

python-aiml.py

a guest
Sep 2nd, 2011
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import aiml
  4. import os
  5. import htmllib, formatter
  6. import StringIO
  7. import time
  8.  
  9. botname = ['imaccountname']
  10. wpm = 1000.0
  11. charsperword = 6.0
  12. secondsperchar = 60.0 / (wpm * charsperword)
  13. print secondsperchar
  14.  
  15. k = aiml.Kernel()
  16. for filename in os.listdir("./"):
  17.     if filename.endswith(".aiml"):
  18.         k.learn(filename)
  19.  
  20. def my_func(account, sender, message, conversation, flags):
  21.     accountname = purple.PurpleAccountGetUsername(account)
  22.     if accountname in botname and sender not in botname:
  23.         file = StringIO.StringIO()
  24.         p = htmllib.HTMLParser(formatter.AbstractFormatter(formatter.DumbWriter(file)))
  25.         p.feed(message)
  26.         p.close()
  27.         text = file.getvalue()
  28.         print sender + " said, \"" + text + "\""
  29.         reply = k.respond(text)
  30.         waittime = secondsperchar*len(reply)
  31.         print "I replied after " + str(waittime) + " seconds, \"" + reply + "\""
  32.         time.sleep(waittime)
  33.         if purple.PurpleConversationGetType(conversation)==2:
  34.             purple.PurpleConvChatSend(purple.PurpleConvChat(conversation), reply)
  35.         else:
  36.             purple.PurpleConvImSend(purple.PurpleConvIm(conversation), reply)
  37.  
  38. import dbus, gobject
  39. from dbus.mainloop.glib import DBusGMainLoop
  40. dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  41. bus = dbus.SessionBus()
  42.  
  43. obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
  44. purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
  45.  
  46. bus.add_signal_receiver(my_func,
  47.                         dbus_interface="im.pidgin.purple.PurpleInterface",
  48.                         signal_name="ReceivedChatMsg")
  49. bus.add_signal_receiver(my_func,
  50.                         dbus_interface="im.pidgin.purple.PurpleInterface",
  51.                         signal_name="ReceivedImMsg")
  52.  
  53. print "Available Accounts: ",
  54. for i in purple.PurpleAccountsGetAll():
  55.     print purple.PurpleAccountGetUsername(i)," ",
  56. print ""
  57.  
  58. loop = gobject.MainLoop()
  59. loop.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement