A simple chatbot

match greetings


In [17]:
import re

In [3]:
r = "(hi|hello|hey)[ ]*([a-z]*)"
re.match(r, 'Hello Rosa', flags=re.IGNORECASE)


Out[3]:
<_sre.SRE_Match object; span=(0, 10), match='Hello Rosa'>

In [4]:
re.match(r, "hi ho, hi ho, it's off to work ...", flags=re.IGNORECASE)


Out[4]:
<_sre.SRE_Match object; span=(0, 5), match='hi ho'>

In [5]:
re.match(r, "hey, what's up", flags=re.IGNORECASE)


Out[5]:
<_sre.SRE_Match object; span=(0, 3), match='hey'>

In [16]:
r = r'''[^a-z]*([y]o|[h']?ello|ok|hey|(good[ ])?(morn[gin']{0,3}|afternoon|even[gin']{0,3}))[\s,;:]{1,3}([a-z]{1,20})'''
re_greeting = re.compile(r, flags=re.IGNORECASE)

In [9]:
re_greeting.match('Hello Rosa')


Out[9]:
<_sre.SRE_Match object; span=(0, 10), match='Hello Rosa'>

In [10]:
re_greeting.match('Hello Rosa').groups()


Out[10]:
('Hello', None, None, 'Rosa')

In [11]:
re_greeting.match("Good morning Rosa")


Out[11]:
<_sre.SRE_Match object; span=(0, 17), match='Good morning Rosa'>

In [12]:
re_greeting.match("Good Manning Rosa")

In [13]:
re_greeting.match('Good evening Rosa Parks').groups()


Out[13]:
('Good evening', 'Good ', 'evening', 'Rosa')

In [14]:
re_greeting.match("Good Morn'n Rosa")


Out[14]:
<_sre.SRE_Match object; span=(0, 16), match="Good Morn'n Rosa">

In [15]:
re_greeting.match("yo Rosa")


Out[15]:
<_sre.SRE_Match object; span=(0, 7), match='yo Rosa'>

output generator


In [22]:
my_names = set(['rosa', 'rose', 'chatty', 'chatbot', 'bot', 'chatterbot'])
curt_names = set(['hal', 'you', 'u'])
greeter_name = ''
match = re_greeting.match(input())
if match:
    at_name = match.groups()[-1]
    if at_name in curt_names:
        print("Good one.")
    elif at_name.lower() in my_names:
        print("Hi {}, How are you?".format(greeter_name))


Salvatore

bag-of-word vectors


In [24]:
from collections import Counter

In [25]:
Counter("Guten Morgen Rosa".split())


Out[25]:
Counter({'Guten': 1, 'Morgen': 1, 'Rosa': 1})

In [26]:
Counter("Good morning, Rosa!".split())


Out[26]:
Counter({'Good': 1, 'morning,': 1, 'Rosa!': 1})

word order and grammar


In [27]:
from itertools import permutations
[" ".join(combo) for combo in permutations("Good morning Rosa!".split(), 3)]


Out[27]:
['Good morning Rosa!',
 'Good Rosa! morning',
 'morning Good Rosa!',
 'morning Rosa! Good',
 'Rosa! Good morning',
 'Rosa! morning Good']

In [28]:
s = """Find textbooks with titles containing 'NLP', 
    or 'natural' and 'language', or 
    'computational' and  'linguistics'."""
len(set(s.split()))


Out[28]:
12

In [29]:
import numpy as np
np.arange(1, 12 + 1).prod()  # factorial(12) = arange(1, 13).prod()


Out[29]:
479001600

In [ ]: