-
Notifications
You must be signed in to change notification settings - Fork 0
/
QClass.py
234 lines (191 loc) · 6.05 KB
/
QClass.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
__author__ = 'distro'
import sys
import re
import pandas as pd
import nltk
MODEL_PATH="model/"
MODEL_FILE="classifier.pkl"
TFIDF_FILE="tfidf.pkl"
QUESLE_FILE="quesle.pkl"
ValidQuesWords=['A', 'About', 'Can', 'Define', 'For', 'Give', 'How', 'In', 'Name', 'On', 'Tell', 'The', 'To', 'What', 'Whats', 'When', 'Where', 'Wheres', 'Which', 'Who', 'Whom', 'Whos', 'Whose', 'Why']
ClassMapper={"UNKNOWN":0,"WHAT":1,"WHEN":2,"WHO":3}
def getInverseClassMapper(Mapper):
RevMap={}
for key in Mapper:
RevMap[Mapper[key]]=key
return RevMap
def clean(ques):
ques=ques.rstrip('?:!.,;')
ques=re.sub('[!@#$,\`\']', '', ques)
return ques.lower().strip()
def loadModel(path):
from sklearn.externals import joblib
return joblib.load(path)
def getQuesWord(ques):
word=ques.strip().split(" ")[0].strip()
if word in ValidQuesWords:
return word
else:
return "InvalidQues"
def hasNumbers(inputString):
return any(char.isdigit() for char in inputString)
def wordCount(ques):
return len(re.findall(r'\w+', ques))
def getPosFeatures(ques):
featurelist=[]
poslist=[]
for token,pos in nltk.pos_tag(nltk.word_tokenize(ques)):
poslist.append(pos)
# Counting Adjectives, having POS Tags JJ/JJS/JJR
count=0
for pos in poslist:
if pos in ["JJ","JJS","JJR"]:
count+=1
featurelist.append(count)
# Counting Nouns, having POS Tags NN/NNS/NNP/NNPS
count=0
for pos in poslist:
if pos in ["NN","NNS","NNP","NNPS"]:
count+=1
featurelist.append(count)
# Counting Personal pronoun, Having POS Tags PRP
count=0
for pos in poslist:
if pos=="PRP":
count+=1
featurelist.append(count)
# Counting Possessive pronoun, Having POS Tags PRP$
count=0
for pos in poslist:
if pos=="PRP$":
count+=1
featurelist.append(count)
# Counting Adverbs, Having POS Tags RB/RBS/RBR/RP
count=0
for pos in poslist:
if pos in ["RB","RBS","RBR","RP"]:
count+=1
featurelist.append(count)
# Counting Verb, base form, Having POS Tags VB
count=0
for pos in poslist:
if pos=="VB":
count+=1
featurelist.append(count)
# Counting Verb, past tense, Having POS Tags VBD
count=0
for pos in poslist:
if pos=="VBD":
count+=1
featurelist.append(count)
# Counting Verb, gerund or present participle, Having POS Tags VBG
count=0
for pos in poslist:
if pos=="VBG":
count+=1
featurelist.append(count)
# Counting Verb, past participle, Having POS Tags VBN
count=0
for pos in poslist:
if pos=="VBN":
count+=1
featurelist.append(count)
# Counting Verb, non-3rd person singular present, Having POS Tags VBP
count=0
for pos in poslist:
if pos=="VBP":
count+=1
featurelist.append(count)
# Counting Verb, 3rd person singular present, Having POS Tags VBZ
count=0
for pos in poslist:
if pos=="VBZ":
count+=1
featurelist.append(count)
# Counting Verbs, Having POS Tags VB/VBD/VBG/VBN/VBP/VBZ
count=0
for pos in poslist:
if pos in ["VB","VBD","VBG","VBN","VBP","VBZ"]:
count+=1
featurelist.append(count)
# Counting Wh-determiner, Having POS Tags WDT
count=0
for pos in poslist:
if pos=="WDT":
count+=1
featurelist.append(count)
# Counting Wh-pronoun, Having POS Tags WP
count=0
for pos in poslist:
if pos=="WP":
count+=1
featurelist.append(count)
# Counting Wh-adverb, Having POS Tags WRB
count=0
for pos in poslist:
if pos=="WRB":
count+=1
featurelist.append(count)
# Counting Wh-Tokens, Having POS Tags WDT/WP/WRB
count=0
for pos in poslist:
if pos in ["WDT","WP","WRB"]:
count+=1
featurelist.append(count)
return featurelist
def extract_entity_names(t):
entity_names = []
if hasattr(t, 'node') and t.node:
if t.node in ['GPE','PEOPLE','ORGANIZATION']:
entity_names.append(' '.join([child[0] for child in t]))
else:
for child in t:
entity_names.extend(extract_entity_names(child))
return entity_names
def getNERFeatures(ques):
namedEnt = nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(ques)))
return len(extract_entity_names(namedEnt))
def checkAffirmation(ques):
pattern=r'[a-z]* (anyone |anybody |you)[a-z]*(tell|know)[a-z]*'
BeVerbs=["is","am","are","was","were","been","being"]
ModalVerbs=["can","could","shall","should","will","would","may","might"]
AuxVerbs=["do","did","does","have","had","has"]
if ques.startswith(tuple(BeVerbs)) or ques.startswith(tuple(ModalVerbs)) or ques.startswith(tuple(AuxVerbs)):
if ques.startswith(tuple(BeVerbs)) and " or " in ques:
return False
else:
if re.search(pattern,ques):
return False
else:
return True
else:
return False
def getFeatures(ques):
tvf=loadModel(MODEL_PATH+TFIDF_FILE)
quesle=loadModel(MODEL_PATH+QUESLE_FILE)
features=tvf.transform([ques]).toarray().flatten().tolist()
features.append(quesle.transform(getQuesWord(ques)))
features.append(hasNumbers(ques))
features.append(wordCount(ques))
features.extend(getPosFeatures(ques))
features.append(getNERFeatures(ques))
return pd.DataFrame([features])
def getWhClass(ques):
clf=loadModel(MODEL_PATH+MODEL_FILE)
df=getFeatures(ques)
InverseClassMapper=getInverseClassMapper(ClassMapper)
return InverseClassMapper[clf.predict(df)[0]]
def getQClass(ques):
ques=clean(ques)
if checkAffirmation(ques):
return "Affirmation"
else:
return getWhClass(ques)
if __name__=="__main__":
if len(sys.argv)==1:
print "Please pass an input string"
elif len(sys.argv)>2:
print "Incorrect Format!! Enclose input string in double quotes"
else:
ques=sys.argv[1]
print getQClass(ques)