Created
October 19, 2013 20:03
-
-
Save aniversarioperu/7060825 to your computer and use it in GitHub Desktop.
Lukebot. Modified from http://pythonism.wordpress.com/2010/04/18/a-simple-chatbot-in-python/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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