In [17]:
import re
In [3]:
r = "(hi|hello|hey)[ ]*([a-z]*)"
re.match(r, 'Hello Rosa', flags=re.IGNORECASE)
Out[3]:
In [4]:
re.match(r, "hi ho, hi ho, it's off to work ...", flags=re.IGNORECASE)
Out[4]:
In [5]:
re.match(r, "hey, what's up", flags=re.IGNORECASE)
Out[5]:
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]:
In [10]:
re_greeting.match('Hello Rosa').groups()
Out[10]:
In [11]:
re_greeting.match("Good morning Rosa")
Out[11]:
In [12]:
re_greeting.match("Good Manning Rosa")
In [13]:
re_greeting.match('Good evening Rosa Parks').groups()
Out[13]:
In [14]:
re_greeting.match("Good Morn'n Rosa")
Out[14]:
In [15]:
re_greeting.match("yo Rosa")
Out[15]:
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))
In [24]:
from collections import Counter
In [25]:
Counter("Guten Morgen Rosa".split())
Out[25]:
In [26]:
Counter("Good morning, Rosa!".split())
Out[26]:
In [27]:
from itertools import permutations
[" ".join(combo) for combo in permutations("Good morning Rosa!".split(), 3)]
Out[27]:
In [28]:
s = """Find textbooks with titles containing 'NLP',
or 'natural' and 'language', or
'computational' and 'linguistics'."""
len(set(s.split()))
Out[28]:
In [29]:
import numpy as np
np.arange(1, 12 + 1).prod() # factorial(12) = arange(1, 13).prod()
Out[29]:
In [ ]: