-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.py
64 lines (57 loc) · 1.34 KB
/
lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from nltk.tokenize import word_tokenize
import nltk
from nltk.stem import WordNetLemmatizer
import re
lem = WordNetLemmatizer()
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
def filter_command(phrase):
tokens=[]
tok=word_tokenize(phrase)
for t in tok:
tokens.append(tok.lower())
tags = nltk.pos_tag(tokens)
work=[]
work_f=[]
subject=[]
number=[]
adj=[]
query=[]
name=0
for tup in tags:
if "VB" in tup[1]:
work.append(tup[0])
if "CD" in tup[1]:
number.append(tup[0])
if "JJ" in tup[1]:
adj.append(tup[0])
if "NN" in tup[1]:
subject.append(tup[0])
if "W" in tup[1] and "that" not in tup[0]:
query.append(tup[0])
for w in work:
work_f.append(lem.lemmatize(w.lower()))
if query:
if "you" in tokens or "your" in tokens:
task=0
elif 'weather' not in tokens or 'news' not in tokens or 'headlines' not in tokens:
task=1
elif 'play' in work_f or 'song' in subject or 'play' in subject:
task=2
elif 'book' in work_f or 'book' in tokens[0]:
task=3
elif 'weather' in subject:
task=4
elif 'news' in subject or 'headlines' in subject:
task=5
else:
if '?' in tokens and 'you' not in tokens and 'your' not in tokens:
task=1
else:
task=0
return task,work_f,subject,number,adj,query