Last active
August 29, 2015 14:10
-
-
Save maurelio1234/6957fa6ead05633a54dc to your computer and use it in GitHub Desktop.
ident fixes
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
#coding: utf-8 | |
from sys import exit | |
import random | |
import re | |
from datetime import datetime | |
random.seed() | |
firstexchange = True | |
askedquestion = None | |
all_topics = [] | |
# helpers | |
def topic(f): | |
all_topics.append(f) | |
def g(r): | |
v = f(r) | |
print 'enter', f, '(', r, ')', '==', v | |
return v | |
return g | |
def default(f): | |
global current_topic | |
current_topic = f | |
def ask(q): | |
say(q) | |
askedquestion = q | |
def say(w): | |
if w: | |
print 'me: '+ w | |
askedquestion = None | |
def find_new_topic(): | |
current_topic = random.choice(all_topics) | |
current_topic(None) | |
# actual meat | |
@default | |
@topic | |
def greetings(r): | |
global firstexchange, askedquestion | |
if r is None: | |
hour = datetime.now().hour | |
if hour < 16: | |
goodday = 'buongiorno' | |
else: | |
goodday = 'buonasera' | |
say(random.choice(['ciao', goodday])) | |
elif (not firstexchange and r == 'ciao') or r in ['arrivederci', 'arrivederla']: | |
say(r) | |
exit() | |
elif r in ['ciao', 'buonasera', 'buongiorno']: | |
say(r) | |
elif r == 'come sta?': | |
say(random.choice(['bene, grazie', 'bene, grazie', 'molto bene, grazie', 'non troppo bene', 'non c\'è male'])) | |
ask(random.choice(['', 'e Lei?'])) | |
elif askedquestion == 'e Lei?': | |
find_new_topic() | |
else: | |
return False | |
firstexchange = False | |
return True | |
@topic | |
def language(r): | |
if r is None: | |
say('parla italiano?') | |
elif askedquestion == 'parla italiano?' and r == 'no': | |
ask('parla inglese?') | |
elif r == 'si': | |
find_new_topic() | |
elif r == 'parla italiano?': | |
say('si') | |
else: | |
return False | |
return True | |
@topic | |
def name(r): | |
print 'name', '(', r, ')' | |
if r is None: | |
say('come si chiama?') | |
elif askedquestion == 'come si chiama?': | |
# get name: mi chiamo X | |
m = re.match(r"mi chiamo (?P<name>\w+)", r) | |
if m: | |
name = m.group('name') | |
say('piacere, ' + name) | |
else: | |
return False | |
elif r == 'come si chiama?': | |
say('mi chiamo bot') | |
elif r == 'piacere': | |
find_new_topic() | |
else: | |
return False | |
return True | |
# main loop | |
print all_topics | |
def process(r): | |
global current_topic | |
if not current_topic(r): | |
for topic in all_topics: | |
if topic(r): | |
current_topic = topic | |
return | |
ask('prego?') | |
exit() | |
while True : | |
r = raw_input('you: ') | |
process(r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment