-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures.py
390 lines (295 loc) · 9.09 KB
/
features.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import nltk
from nltk import word_tokenize
lemma = nltk.wordnet.WordNetLemmatizer()
sno = nltk.stem.SnowballStemmer('english')
from nltk.corpus import stopwords
import pandas as pd
import csv
import sys
import hashlib
import re
import string
import itertools
line = ["xxx","Oracle 12.2 will be released for on-premises users on 15 March 2017",0,"S"]
pos = []
output = ""
header = ""
VerbCombos = ['VB',
'VBD',
'VBG',
'VBN',
'VBP',
'VBZ',
'WDT',
'WP',
'WP$',
'WRB',
'MD']
questionTriples = ['CD-VB-VBN',
'MD-PRP-VB' ,
'MD-VB-CD' ,
'NN-IN-DT' ,
'PRP-VB-PRP' ,
'PRP-WP-NNP' ,
'VB-CD-VB' ,
'VB-PRP-WP' ,
'VBZ-DT-NN' ,
'WP-VBZ-DT' ,
'WP-VBZ-NNP' ,
'WRB-MD-VB']
statementTriples = ['DT-JJ-NN',
'DT-NN-VBZ',
'DT-NNP-NNP',
'IN-DT-NN',
'IN-NN-NNS',
'MD-VB-VBN',
'NNP-IN-NNP',
'NNP-NNP-NNP',
'NNP-VBZ-DT',
'NNP-VBZ-NNP',
'NNS-IN-DT',
'VB-VBN-IN',
'VBZ-DT-JJ']
startTuples = ['NNS-DT',
'WP-VBZ',
'WRB-MD']
endTuples = ['IN-NN',
'VB-VBN',
'VBZ-NNP']
feature_keys = ["id",
"wordCount",
"stemmedCount",
"stemmedEndNN",
"CD",
"NN",
"NNP",
"NNPS",
"NNS",
"PRP",
"VBG",
"VBZ",
"startTuple0",
"endTuple0",
"endTuple1",
"endTuple2",
"verbBeforeNoun",
"qMark",
"qVerbCombo",
"qTripleScore",
"sTripleScore",
"class"]
def strip_sentence(sentence):
sentence = sentence.strip(",")
sentence = ''.join(filter(lambda x: x in string.printable, sentence))
sentence = sentence.translate(str.maketrans('','',string.punctuation))
return(sentence)
def exists_pair_combos(comboCheckList, sentence):
pos = get_pos(sentence)
tag_string = "-".join([ i[1] for i in pos ])
combo_list = []
for pair in itertools.permutations(comboCheckList,2):
if(pair[0] == "MD"):
pair = ["",""]
combo_list.append("-".join(pair))
if any(code in tag_string for code in combo_list):
return 1
else:
return 0
def get_pos(sentence):
sentenceParsed = word_tokenize(sentence)
return(nltk.pos_tag(sentenceParsed))
def count_qmark(sentence):
return(sentence.count("?") )
def count_POSType(pos, ptype):
count = 0
tags = [ i[1] for i in pos ]
return(tags.count(ptype))
def exists_vb_before_nn(pos):
pos_tags = [ i[1] for i in pos ]
pos_tags = [ re.sub(r'V.*','V', str) for str in pos_tags ]
pos_tags = [ re.sub(r'NN.*','NN', str) for str in pos_tags ]
vi =99
ni =99
mi =99
if "NN" in pos_tags:
ni = pos_tags.index("NN")
if "V" in pos_tags:
vi = pos_tags.index("V")
if "MD" in pos_tags:
mi = pos_tags.index("MD")
if vi < ni or mi < ni :
return(1)
else:
return(0)
def exists_stemmed_end_NN(stemmed):
stemmedEndNN = 0
stemmed_end = get_first_last_tuples(" ".join(stemmed))[1]
if stemmed_end == "NN-NN":
stemmedEndNN = 1
return(stemmedEndNN)
def exists_startTuple(startTuple):
exists_startTuples = []
for tstring in startTuples:
if startTuple in tstring:
exists_startTuples.append(1)
else:
exists_startTuples.append(0)
return(exists_startTuples)
def exists_endTuple(endTuple):
exists_endTuples = []
for tstring in endTuples:
if endTuple in tstring:
exists_endTuples.append(1)
else:
exists_endTuples.append(0)
return(exists_endTuples)
def exists_triples(triples, tripleSet):
exists = []
for tstring in tripleSet:
if tstring in triples:
exists.append(1)
else:
exists.append(0)
return(exists)
def get_triples(pos):
list_of_triple_strings = []
pos = [ i[1] for i in pos ]
n = len(pos)
if n > 2:
for i in range(0,n-2):
t = "-".join(pos[i:i+3])
list_of_triple_strings.append(t)
return list_of_triple_strings
def get_first_last_tuples(sentence):
first_last_tuples = []
sentenceParsed = word_tokenize(sentence)
pos = nltk.pos_tag(sentenceParsed)
pos = [ i[1] for i in pos ]
n = len(pos)
first = ""
last = ""
if n > 1:
first = "-".join(pos[0:2])
last = "-".join(pos[-2:])
first_last_tuples = [first, last]
return first_last_tuples
def lemmatize(sentence):
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(sentence)
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w.lower())
lem = []
for w in filtered_sentence:
lem.append(lemma.lemmatize(w))
return lem
def stematize(sentence):
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(sentence)
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)
stemmed = []
for w in filtered_sentence:
stemmed.append(sno.stem(w))
return stemmed
def get_string(id,sentence,c="X"):
header,output = "",""
pos = get_pos(sentence)
qMark = count_qmark(sentence)
sentence = strip_sentence(sentence)
#lemmed = lemmatize(sentence)
stemmed = stematize(sentence)
wordCount = len(sentence.split())
stemmedCount = len(stemmed)
qVerbCombo = exists_pair_combos(VerbCombos,sentence)
verbBeforeNoun = exists_vb_before_nn(pos)
output = id + "," + str(wordCount) + "," + str(stemmedCount) + "," + str(qVerbCombo)+ "," + str(qMark) + "," + str(verbBeforeNoun)
header = header + "id,wordCount,stemmedCount,qVerbCombo,qMark,verbBeforeNoun"
for ptype in ["VBG", "VBZ", "NNP", "NN", "NNS", "NNPS","PRP", "CD"]:
output = output + "," + str( count_POSType(pos,ptype) )
header = header + "," + ptype
output = output + "," + str(exists_stemmed_end_NN(stemmed))
header = header + ",StemmedEndNN,"
startTuple,endTuple = get_first_last_tuples(sentence)
l = exists_startTuple(startTuple)
output = output + "," + ",".join(str(i) for i in l)
for i in range(0,len(l)):
header = header + "startTuple" + str(i+1) + ","
l = exists_endTuple(endTuple)
output = output + "," + ",".join(str(i) for i in l)
for i in range(0,len(l)):
header = header + "endTuple" + str(i+1) + ","
triples = get_triples(pos)
l = exists_triples(triples, questionTriples)
total = sum(l)
output = output + "," + str(total)
header = header + "qTripleScore" + ","
l = exists_triples(triples, statementTriples)
total = sum(l)
output = output + "," + str(total)
header = header + "sTripleScore" + ","
output = output + "," + c
header = header + "class"
return output,header
def features_dict(id,sentence,c="X"):
features = {}
pos = get_pos(sentence)
features["id"] = id
features["qMark"] = count_qmark(sentence)
sentence = strip_sentence(sentence)
stemmed = stematize(sentence)
startTuple,endTuple = get_first_last_tuples(sentence)
features["wordCount"] = len(sentence.split())
features["stemmedCount"] = len(stemmed)
features["qVerbCombo"] = exists_pair_combos(VerbCombos,sentence)
features["verbBeforeNoun"] = exists_vb_before_nn(pos)
for ptype in ["VBG", "VBZ", "NNP", "NN", "NNS", "NNPS","PRP", "CD"]:
features[ptype] = count_POSType(pos,ptype)
features["stemmedEndNN"] = exists_stemmed_end_NN(stemmed)
l = exists_startTuple(startTuple)
for i in range(0,len(l)):
features["startTuple" + str(i)] = l[i]
l = exists_endTuple(endTuple)
for i in range(0,len(l)):
features["endTuple" + str(i)] = l[i]
triples = get_triples(pos)
l = exists_triples(triples, questionTriples)
features["qTripleScore"] = sum(l)
l = exists_triples(triples, statementTriples)
features["sTripleScore"] = sum(l)
features["class"] = c
return features
def features_series(features_dict):
values=[]
for key in feature_keys:
values.append(features_dict[key])
features_series = pd.Series(values)
return features_series
## MAIN ##
if __name__ == '__main__':
print("Starting...")
c = "X"
header = ""
output = ""
if len(sys.argv) > 1:
sentence = sys.argv[1]
else:
sentence = line[1]
id = hashlib.md5(str(sentence).encode('utf-8')).hexdigest()[:16]
features = features_dict(id,sentence, c)
pos = get_pos(sentence)
print(pos)
print(features)
for key,value in features.items():
print(key, value)
#header string
for key, value in features.items():
header = header + ", " + key
output = output + ", " + str(value)
header = header[1:]
output = output[1:]
print("HEADER:", header)
print("VALUES:", output)