Building Chatbots in Python
Alan Nichol
Co-founder and CTO, Rasa
USER: "do you remember when you ate strawberries in the garden?"
ELIZA: "How could I forget when I ate strawberries in the garden?"
import re
pattern = "do you remember .*"
message = "do you remember when you ate strawberries in the garden"
match = re.search(pattern, message)
if match: print("string matches!")
string matches!
import re pattern = "if (.*)"
message = "what would happen if bots took over the world" match = re.search(pattern, message)
match.group(0)
'what would happen if bots took over the world'
match.group(1)
'bots took over the world'
import re def swap_pronouns(phrase): if 'I' in phrase: return re.sub('I', 'you', phrase) if 'my' in phrase: return re.sub('my', 'your', phrase) else: return phrase
swap_pronouns("I walk my dog")
'You walk your dog'
pattern = 'do you remember (.*)' message = 'do you remember when you ate strawberries in the garden'
phrase = pattern.search(pattern, message).group(1) phrase
'when you ate strawberries in the garden'
response = choose_response(pattern)
response
'how could I forget {}'
phrase = swap_pronouns(phrase)
phrase
'when I ate strawberries in the garden'
response.format(phrase)
'how could I forget when I ate strawberries in the garden'
Building Chatbots in Python