Text processing with regular expressions

Building Chatbots in Python

Alan Nichol

Co-founder and CTO, Rasa

Regular expressions

  • Match messages against known patterns
  • Extract key phrases
  • Transform sentences grammatically
Building Chatbots in Python

The regex behind ELIZA

USER: "do you remember when you ate strawberries in the garden?"

ELIZA: "How could I forget when I ate strawberries in the garden?"

Building Chatbots in Python

Pattern matching

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!
Building Chatbots in Python

Extracting key phrases

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'
Building Chatbots in Python

Grammatical transformation

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'
Building Chatbots in Python

Putting it all together

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 {}'
Building Chatbots in Python

Putting it all together

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

Let's practice!

Building Chatbots in Python

Preparing Video For Download...