Skip to content

Instantly share code, notes, and snippets.

@aniversarioperu
Created October 19, 2013 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aniversarioperu/7060825 to your computer and use it in GitHub Desktop.
Save aniversarioperu/7060825 to your computer and use it in GitHub Desktop.
import pickle
import random
import sys
import re
# Modified from http://pythonism.wordpress.com/2010/04/18/a-simple-chatbot-in-python/
a = open('lexicon-luke','rb')
successorlist = pickle.load(a)
a.close()
avoid = ["a", "en", "el", "que", "la", "del", "esta", "de", "su", "con"]
def nextword(a):
if a in successorlist:
try:
return random.choice(successorlist[a])
except:
return ''
else:
return 'de'
def get_response(input):
response = ''
s = random.choice(input.split())
while True:
neword = nextword(s)
if neword != '':
response += neword + ' '
s = neword
if len(response) > 120:
for i in avoid:
regex = re.compile('\s%s\s*$'%i, re.I)
if regex.search(response):
response = regex.sub("", response).strip()
break
return response.strip()
if __name__ == "__main__":
speech = ''
while speech != 'quit':
speech = raw_input('>')
response = get_response(speech)
print response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment