Skip to content

Instantly share code, notes, and snippets.

@thricedotted
Created May 5, 2015 14:16
Show Gist options
  • Save thricedotted/95e5ea06fc995c866a58 to your computer and use it in GitHub Desktop.
Save thricedotted/95e5ea06fc995c866a58 to your computer and use it in GitHub Desktop.
7on7 bot response
import codecs
import random
import pyttsx
from nltk.corpus import wordnet as wn
from pattern.en import parsetree, conjugate, pprint
class NoDefinitionError(Exception):
pass
# clean the output file
with open('output.txt', 'w') as f:
f.write('')
# start voice engine
engine = pyttsx.init()
engine.setProperty('voice', 'english-us')
engine.setProperty('rate', 150)
engine.startLoop(False)
def print_message(msg, msg_type=None):
if msg_type is None:
msg_type = 'BOT'
with open('output.txt', 'a') as f:
line = u"[{}] {}\n".format(msg_type.upper(), msg)
f.write(line)
def verb_response(sentence):
verbs = [w for w in sentence
if w.pos.startswith('VB')
and w.lemma != 'be']
print_message('Extracted verbs: {}'.format(' - '.join(w.lemma for w in verbs)))
objects = list(set(w for v in verbs
for w in v.chunk.related
if w.role == 'OBJ'))
if len(objects) > 0:
print_message('Extracted objects: {}'.format(' - '.join(w.string for w in objects)))
else:
print_message('Extracted objects: none')
subjects = list(set(w for v in verbs
for w in v.chunk.related
if w.role == 'SBJ'))
if len(subjects) > 0:
print_message('Extracted subjects: {}'.format(' - '.join(w.string for w in subjects)))
else:
print_message('Extracted subjects: none')
random.shuffle(verbs)
synsets = []
for verb in verbs:
synsets = wn.synsets(verb.lemma)
if len(synsets) > 0:
break
else:
raise NoDefinitionError
print_message('Choosing random verb: {}'.format(verb.lemma))
print_message('Getting dictionary defintion')
verb_synset = random.choice(synsets)
definition = verb_synset.definition()
print_message('Forming response')
template = "{} {} {}.".format(
" and ".join(s.string for s in subjects),
definition,
" and ".join(o.string for o in objects))
return template
def noun_response(sentence):
noun_words = [w.lemma for w in sentence
if w.pos.startswith('NN')]
print_message('Extracted nouns: {}'.format(' - '.join(noun_words)))
#noun = random.choice(noun_words)
#synsets = wn.synsets(noun)
random.shuffle(noun_words)
for noun in noun_words:
synsets = wn.synsets(noun)
if len(synsets) > 0:
break
else:
raise NoDefinitionError
print_message('Choosing random noun: {}'.format(noun))
templates = ["That is {}.",
"Like {}."]
print_message('Getting dictionary definition')
definition = random.choice(synsets).definition()
print_message('Forming response')
return random.choice(templates).format(definition)
def word_response(sentence):
synsets = [s for w in sentence for s in wn.synsets(w.lemma)]
if len(synsets) == 0:
raise NoDefinitionError
return random.choice(synsets).definition()
def any_response(sentence):
for fn in (verb_response, noun_response, word_response):
try:
return fn(sentence)
except NoDefinitionError:
pass
return "I don't know."
def process_text(text):
paragraph = parsetree(text, relations=True, lemmata=True)[:-1]
sentences = random.sample(paragraph, min(len(paragraph), 4))
responses = [any_response(s) for s in sentences]
denial = ["No.", "I don't.", "I don't understand."]
engine.say(random.choice(denial))
engine.iterate()
for r in responses:
response = r[0].upper() + ' '.join(r[1:].split())
print_message('"{}"'.format(response))
engine.say(r)
engine.iterate()
print_message('Done\n')
if __name__ == '__main__':
while True:
speech = raw_input('> ')
process_text(speech)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment