-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_functions.py
509 lines (437 loc) · 17.3 KB
/
feature_functions.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
from nltk import sent_tokenize, word_tokenize
import nltk
import numpy
import pickle
import datetime
import re
from nltk.corpus import stopwords
phones = ["phone", "phones", "smartphone", "smartphones", "mobile"]
org_list = ["microsoft","google","samsung","htc","sony","apple","micromax","blackberry","karbonn","lenovo","lg","motorola","moto","facebook","whatsapp","xiomi","xiaomi"]
date_list=["year","month","week","yesterday","years","months","tomorrow","today","weeks","day","days","date","when","time"]
family_list=["galaxy", "lumia", "g", "h", "x", "e", "nexus", "desire", "series", "s", "iphone", "xperia","note"]
os_list=["android", "ios", "windows", "symbian", "bada", "os", "blackberry", "firefox"]
version_list=["lollipop", "kitkat", "jellybean","honeycomb","gingerbread","icecream","sandwich"]
app_list=["temple", "run", "candy", "crush", "whatsapp", "zomato", "autoid", "unblock", "photosphere", "siri", "gmail", "youtube", "messenger"]
month_list=["january","february","march","april","may","june","july","august","september","october","november","december"]
feature_list=["screen","size","dual","sim","camera","front","back","rear","touch-screen","touch","screen","AOSP","wifi","wi-fi","display","battery","memory","ram","mp"]
currency_symbols = ["rs", "inr", "$", "usd", "cents", "rupees"]
size_list = ["inch", "cm", "inches", "cms", r'"', "''", "pixel", "px", "mega", "gb", "mb", "kb", "kilo", "giga", "mega-pixel" ]
punctuation_list = [",", ";", "'", ".", ":", "?", "'s", "''", "' '", "!"]
stop = stopwords.words('english')
class FeatureFunctions(object):
def __init__(self, tag_list = None):
self.wmap = {}
self.flist = {} #[self.f1, self.f2, self.f3, self.f4, self.f5, self.f6, self.f7, self.f8, self.f9, self.f10, self.f11, self.f12, self.f13]
self.fdict = {}
for k, v in FeatureFunctions.__dict__.items():
if hasattr(v, "__call__"):
if k[0] == 'f':
self.flist[k] = v # .append(v)
tag = k[1:].split("_")[0]
val = self.fdict.get(tag, [])
val.append(v)
self.fdict[tag] = val
self.supported_tags = self.fdict.keys()
return
def set_wmap(self, wmap): # given a list of words sets wmap
self.wmap=wmap
return
def check_list(self, clist, w):
#return 0
w1 = w.lower()
for cl in clist:
if w1 in cl:
return 1
return 0
def evaluate(self, xi, tag):
feats = []
for t, f in self.fdict.items():
if t == tag:
for f1 in f:
feats.append(int(f1(self, xi, tag)))
else:
for f1 in f:
feats.append(0)
return feats
# feature functions
# Org Tags
def fOrg_1(self, x, y):
try :
if y=="Org" and self.wmap[x["wn"]]["words"][x["i"]+1].lower() in ["phone","phones","smartphone","smartphones"]:
return 1
except :
return 0
return 0
def fOrg_2(self, x, y):
try :
if y=="Org" and self.wmap[x["wn"]]["words"][x["i"]+1].lower() in ["launches", "launched" ,"released", "releases"]:
return 1
except :
return 0
return 0
def fOrg_3(self, x, y):
if y=="Org" and self.wmap[x["wn"]]["words"][x["i"]].lower() in org_list:
return 1
return 0
'''def fOrg_4(self, x, y):
if y=="Org" and x["tb"]=="*" and x["ta"]=="*":
return 1
return 0
def fOrg_5(self, x, y):
if y=="Org" and x["ta"]=="*":
return 1
return 0'''
#Family tags
def fFamily_1(self, x, y):
if y=="Family" and self.wmap[x["wn"]]["words"][x["i"]].lower() in family_list:
return 1
return 0
def fFamily_2(self, x, y):
if y=="Family" and x["tb"]=="Org":
return 1
return 0
'''def fFamily_3(self, x, y):
if y=="Family" and (("i" or "I" and "buy") in self.wmap[x["wn"]]["words"]):
return 1
return 0'''
def fFamily_4(self, x, y):
try:
if y=="Family" and x["tb"]=="Org" and x["ta"]=="Other":
return 1
except:
return 0
return 0
'''def fFamily_5(self, x, y):
if y=="Family" and x["ta"]=="*":
return 1
return 0
def fFamily_6(self, x, y):
if y=="Family" and x["tb"]=="Other":
return 1
return 0'''
#Version tags
def fVersion_1(self, x, y):
if y=="Version" and "version" in self.wmap[x["wn"]]["words"]:
return 1
return 0
def fVersion_2(self, x, y):
if y=="Version" and self.wmap[x["wn"]]["words"][x["i"]].lower() in version_list:
return 1
return 0
def fVersion_3(self, x, y):
m = re.match(r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?',self.wmap[x["wn"]]["words"][x["i"]])
if y=="Version" and m and x["tb"]=="Version":
return 1
return 0
def fVersion_4(self, x, y):
if y=="Version" and x["tb"]=="OS":
return 1
return 0
def fVersion_5(self, x, y):
if y=="Version" and x["ta"]=="Org":
return 1
return 0
'''def fVersion_6(self, x, y):
if y=="Version" and self.wmap[x["wn"]]["words"][x["i"]-1] in (os_list or family_list):
return 1
return 0'''
def fVersion_6(self, x, y):
if y=="Version" and x["tb"]=="Version":
return 1
return 0
#Other tags
#Model
def fOther_1(self, x, y):
if y=="Other" and x["tb"]=="Family" and x["ta"]=="Org":
return 1
return 0
#Model
def fOther_2(self, x, y):
if y=="Other" and x["tb"]=="Other" and x["ta"]=="Family":
return 1
return 0
#App
def fOther_3(self, x, y):
if y=="Other" and self.wmap[x["wn"]]["words"][x["i"]] in app_list:
return 1
return 0
#Date
def fOther_4(self, x, y):
if y=="Other" and self.wmap[x["wn"]]["words"][x["i"]] in date_list:
return 1
return 0
#Date
def fOther_5(self, x, y):
if y=="Other":
for m in month_list:
if self.wmap[x["wn"]]["words"][x["i"]].lower() in m:
return 1
return 0
#Date
def fOther_6(self, x, y):
try:
if y=="Other" and self.wmap[x["wn"]]["words"][x["i"]-1].lower() in ["before","after"]:
return 1
except :
return 0
return 0
#Place
def fOther_7(self, x, y):
if y=="Other":
for suffix in ["lore", "pur", "nagar", "bad", "stan", "india", "karnataka", "usa", "halli", "land", "ai"]:
if suffix in self.wmap[x["wn"]]["words"][x["i"]].lower() :
return 1
return 0
#Place
def fOther_8(self, x, y):
if y=="Other" and self.wmap[x["wn"]]["words"][x["i"]].lower() in ["home", "where", "country", "place" ,"city" ,"state", "online" ,"offline", "showroom"]:
return 1
return 0
#App
def fOther_9(self, x, y):
if y=="Other" and self.wmap[x["wn"]]["words"][x["i"]].lower() in ["apps", "app", "play", "store"]:
return 1
return 0
#App
def fOther_10(self, x, y):
if y=="Other" and x["tb"]=="OS":
return 1
return 0
def fOther_11(self, x, y):
if y=="Other" and self.wmap[x["wn"]]["words"][x["i"]].lower() in stop:
return 1
return 0
def fOther_12(self, x, y):
if y=="Other" and self.wmap[x["wn"]]["words"][x["i"]].lower() in punctuation_list:
return 1
return 0
def fOther_12(self, x, y):
if y=="Other" and x["ta"]=="Other" and x["tb"]=="Other":
return 1
return 0
def fOther_13(self, x, y):
try:
if y=="Other" and x["tb"]=="Other" and x["ta"]=="*":
return 1
except:
return 0
return 0
#Price tags
def fPrice_1(self, x, y):
try:
if y=="Price" and ("Rs".lower() or "Rs.".lower()) in self.wmap[x["wn"]]["words"][x["i"]].lower() or ("rs" or "rs.") in self.wmap[x["wn"]]["words"][x["i"]-1].lower():
return 1
except :
return 0
return 0
def fPrice_2(self, x, y):
try :
if y=="Price" and ( self.wmap[x["wn"]]["words"][x["i"]-2].lower() in ("priced", "less", "greater", "price") or self.wmap[x["wn"]]["words"][x["i"]-1].lower() in ("under", "priced") ):
return 1
except :
return 0
return 0
def fPrice_3(self, x, y):
m = re.match("(\d+)[Kk]",self.wmap[x["wn"]]["words"][x["i"]])
if y=="Price" and m:
return 1
return 0
def fPrice_4(self, x, y):
if y=="Price" and "price" in self.wmap[x["wn"]]["words"] or "range" in self.wmap[x["wn"]]["words"]:
return 1
return 0
def fPrice_5(self, x, y):
try:
if y=="Price" and ("dollars" or "$") in self.wmap[x["wn"]]["words"][x["i"]+1].lower() or ("dollars" or "$") in self.wmap[x["wn"]]["words"][x["i"]-1].lower():
return 1
except:
return 0
return 0
def fPrice_6(self, x, y):
m = re.match(r'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0',self.wmap[x["wn"]]["words"][x["i"]])
if y=="Price" and m:
return 1
return 0
#OS tags
def fOS_1(self, x, y):
if y=="OS" and self.wmap[x["wn"]]["words"][x["i"]].lower() in os_list:
return 1
return 0
def fOS_2(self, x, y):
try:
if y=="OS" and self.wmap[x["wn"]]["words"][x["i"]-1].lower() in ["run", "runs", "have", "has", "in", "on" ,"having", "an", "with"]:
return 1
except:
return 0
return 0
def fOS_3(self, x, y):
try:
if y=="OS" and self.wmap[x["wn"]]["words"][x["i"]+1].lower() in ["phone","phones", "version", "smartphones", "smartphone"]:
return 1
except:
return 0
return 0
def fOS_4(self, x, y):
if y=="OS" and self.wmap[x["wn"]]["words"][x["i"]].lower() in os_list:
return 1
return 0
def fOS_5(self, x, y):
if y=="OS" and x["tb"]=="Other":
return 1
return 0
def fOS_6(self, x, y):
if y=="OS" and x["tb"]=="Other" and x["ta"]=="Other":
return 1
return 0
#Phone tags
def fPhone_1(self, x, y):
if y=="Phone" and self.wmap[x["wn"]]["words"][x["i"]].lower() in ["phones", "phone", "smartphone","smartphones"]:
return 1
return 0
def fPhone_2(self, x, y):
if y=="Phone" and x["tb"] in ["Org", "Family", "OS", "Model"]:
return 1
return 0
def fPhone_3(self, x, y):
if y=="Phone" and ("phone" or "phones") in self.wmap[x["wn"]]["words"]:
return 1
return 0
def fPhone_4(self, x, y):
if y=="Phone" and x["tb"]=="OS":
return 1
return 0
def fPhone_5(self, x, y):
if y=="Phone" and x["tb"]=="Org":
return 1
return 0
#Feature tags
def fFeature_1(self, x, y):
if y=="Feature" and self.wmap[x["wn"]]["words"][x["i"]].lower() in feature_list:
return 1
return 0
def fFeature_2(self, x, y):
if y=="Feature" and x["tb"]=="Other":
return 1
return 0
def fFeature_3(self, x, y):
if y=="Feature" and x["ta"]=="Other" and x["tb"]=="Other":
return 1
return 0
def fFeature_4(self, x, y):
if y=="Feature" and x["tb"]=="Feature":
return 1
return 0
def fFeature_5(self, x, y):
if y=="Feature" and x["tb"]=="Feature" and x["ta"]=="Other":
return 1
return 0
def fFeature_6(self, x, y):
m = re.match(r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?',self.wmap[x["wn"]]["words"][x["i"]])
if y=="Feature" and m:
return 1
return 0
def build_history(data_list, supported_tags):
history_list = [] # list of all histories
words_map = {}
count = 0
count_list=[]
index=0
for data in data_list: # data is the inputs entered by a given student
data1 = data['data']
for rec in data1:
updates = rec['updates']
sent = rec['sentence']
words = []
for i in range(len(updates)):
words.append(updates[i]['word'])
#------------------------------------------------------------------------------------------------
# NOTE: below code is a temporary hack to build the MAxEnt for just 2 tags - we will change this later
if(updates[i]['tag'] == "Model"):
updates[i]['tag']="Version"
elif(updates[i]['tag'] == "Size"):
updates[i]['tag']="Feature"
elif(updates[i]['tag'] not in supported_tags):
updates[i]['tag'] = "Other"
#------------------------------------------------------------------------------------------------
words_map[count] = {'words': words}#, 'pos_tags': nltk.pos_tag(words)}
len_sent=len(words)
if index==0:
count_list.append(len_sent)
else:
count_list.append(count_list[index-1]+len_sent)
index+=1
for i in range(len(updates)):
history = {}
history["i"] = i
if i == 0:
history["ta"] = "*" # special tag
history["tb"] = "*" # special tag
elif i == 1:
history["ta"] = "*" # special tag
history["tb"] = updates[i - 1]['tag']
else:
history["ta"] = updates[i - 2]['tag']
history["tb"] = updates[i - 1]['tag']
history["wn"] = count
history_list.append((history, updates[i]['tag'], ))
count += 1
return (history_list, words_map, count_list)
def test(clf, history_list, wmap):
result = []
for history in history_list:
mymap = self.wmap[history[0]["wn"]]
words = mymap['words']
#tags = mymap['pos_tags']
index = history[0]["i"]
val = clf.classify(history[0])
result.append({'predicted': val, 'word': words[index], 'expected': history[1]})
return result
if __name__ == "__main__":
pass
#----- REPLACE THESE PATHS FOR YOUR SYSTEM ---------------------
'''json_file = r"all_data.json"
pickle_file = r"all_data.p"
# ----------------------------------------------------------------
TRAIN = 1 #int(raw_input("Enter 1 for Train, 0 to use pickeled file: "))
supported_tags = ["Org", "OS", "Version", "Family", "Price", "Phone", "Feature" ,"Other"]
tag_set = {"Org": 0, "Other": 1}
dims = 9
trg_data_x = []
trg_data_y = []
trg_data = {'Org': [], 'Other': []}
data = json.loads(open(json_file).read())['root']
(history_list, wmap, count_list) = build_history(data[0:70], supported_tags)
print "After build_history"
func_obj = FeatureFunctions()#, supported_tags)
func_obj.set_wmap(wmap)
clf = MyMaxEnt(history_list, func_obj, reg_lambda = 0.001, pic_file = pickle_file)
print clf.model
if TRAIN == 1:
clf.train()
'''
'''result = test(clf, history_list[-500:])
count_correct=0
for r in result:
print r['word'], r['predicted'], r['expected']
if r['predicted'] == r['expected']:
count_correct+=1
print count_correct
'''
'''
tagged_sentences = []
predicted = []
for num in range(1, 1300):
h_list = history_list[count_list[num-1]+1:count_list[num]+1]
tagged_sentence=[]
sentence_words = wmap[num]["words"]
path=Viterbi(sentence_words, clf, supported_tags, h_list)
print path
for index in range(len(sentence_words)):
tagged_sentence.append(h_list[index][1])
tagged_sentences.append(tagged_sentence)
predicted.append(path)
ner=ner_metrics.NerMetrics(tagged_sentences,predicted)
metrics=ner.compute()
print "Metrics", metrics
ner.print_results()
'''