-
Notifications
You must be signed in to change notification settings - Fork 17
/
get_NE_list.py
428 lines (410 loc) · 15.3 KB
/
get_NE_list.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
# coding: utf-8
import os
import numpy as np
from config import config
import copy
import sys
from read_files import split_imdb_files, split_yahoo_files, split_agnews_files
import spacy
import argparse
import re
from collections import Counter, defaultdict
nlp = spacy.load('en')
parser = argparse.ArgumentParser('named entity recognition')
parser.add_argument('-d', '--dataset',
help='Data set',
choices=['imdb', 'agnews', 'yahoo'],
default='yahoo')
NE_type_dict = {
'PERSON': defaultdict(int), # People, including fictional.
'NORP': defaultdict(int), # Nationalities or religious or political groups.
'FAC': defaultdict(int), # Buildings, airports, highways, bridges, etc.
'ORG': defaultdict(int), # Companies, agencies, institutions, etc.
'GPE': defaultdict(int), # Countries, cities, states.
'LOC': defaultdict(int), # Non-GPE locations, mountain ranges, bodies of water.
'PRODUCT': defaultdict(int), # Object, vehicles, foods, etc.(Not services)
'EVENT': defaultdict(int), # Named hurricanes, battles, wars, sports events, etc.
'WORK_OF_ART': defaultdict(int), # Titles of books, songs, etc.
'LAW': defaultdict(int), # Named documents made into laws.
'LANGUAGE': defaultdict(int), # Any named language.
'DATE': defaultdict(int), # Absolute or relative dates or periods.
'TIME': defaultdict(int), # Times smaller than a day.
'PERCENT': defaultdict(int), # Percentage, including "%".
'MONEY': defaultdict(int), # Monetary values, including unit.
'QUANTITY': defaultdict(int), # Measurements, as of weight or distance.
'ORDINAL': defaultdict(int), # "first", "second", etc.
'CARDINAL': defaultdict(int), # Numerals that do not fall under another type.
}
def recognize_named_entity(texts):
'''
Returns all NEs in the input texts and their corresponding types
'''
NE_freq_dict = copy.deepcopy(NE_type_dict)
for text in texts:
doc = nlp(text)
for word in doc.ents:
NE_freq_dict[word.label_][word.text] += 1
return NE_freq_dict
def find_adv_NE(D_true, D_other):
'''
find NE_adv in D-D_y_true which is defined in the end of section 3.1
'''
# adv_NE_list = []
for type in NE_type_dict.keys():
# find the most frequent true and other NEs of the same type
true_NE_list = [NE_tuple[0] for (i, NE_tuple) in enumerate(D_true[type]) if i < 15]
other_NE_list = [NE_tuple[0] for (i, NE_tuple) in enumerate(D_other[type]) if i < 30]
for other_NE in other_NE_list:
if other_NE not in true_NE_list and len(other_NE.split()) == 1:
# adv_NE_list.append((type, other_NE))
print("'" + type + "': '" + other_NE + "',")
with open('./{}.txt'.format(args.dataset), 'a', encoding='utf-8') as f:
f.write("'" + type + "': '" + other_NE + "',\n")
break
class NameEntityList(object):
# If the original input in IMDB belongs to class 0 (negative)
imdb_0 = {'PERSON': 'David',
'NORP': 'Australian',
'FAC': 'Hound',
'ORG': 'Ford',
'GPE': 'India',
'LOC': 'Atlantic',
'PRODUCT': 'Highly',
'EVENT': 'Depression',
'WORK_OF_ART': 'Casablanca',
'LAW': 'Constitution',
'LANGUAGE': 'Portuguese',
'DATE': '2001',
'TIME': 'hours',
'PERCENT': '98%',
'MONEY': '4',
'QUANTITY': '70mm',
'ORDINAL': '5th',
'CARDINAL': '7',
}
# If the original input in IMDB belongs to class 1 (positive)
imdb_1 = {'PERSON': 'Lee',
'NORP': 'Christian',
'FAC': 'Shannon',
'ORG': 'BAD',
'GPE': 'Seagal',
'LOC': 'Malta',
'PRODUCT': 'Cat',
'EVENT': 'Hugo',
'WORK_OF_ART': 'Jaws',
'LAW': 'RICO',
'LANGUAGE': 'Sebastian',
'DATE': 'Friday',
'TIME': 'minutes',
'PERCENT': '75%',
'MONEY': '$',
'QUANTITY': '9mm',
'ORDINAL': 'sixth',
'CARDINAL': 'zero',
}
imdb = [imdb_0, imdb_1]
agnews_0 = {'PERSON': 'Williams',
'NORP': 'European',
'FAC': 'Olympic',
'ORG': 'Microsoft',
'GPE': 'Australia',
'LOC': 'Earth',
'PRODUCT': '#',
'EVENT': 'Cup',
'WORK_OF_ART': 'PowerBook',
'LAW': 'Pacers-Pistons',
'LANGUAGE': 'Chinese',
'DATE': 'third-quarter',
'TIME': 'Tonight',
'MONEY': '#39;t',
'QUANTITY': '#39;t',
'ORDINAL': '11th',
'CARDINAL': '1',
}
agnews_1 = {'PERSON': 'Bush',
'NORP': 'Iraqi',
'FAC': 'Outlook',
'ORG': 'Microsoft',
'GPE': 'Iraq',
'LOC': 'Asia',
'PRODUCT': '#',
'EVENT': 'Series',
'WORK_OF_ART': 'Nobel',
'LAW': 'Constitution',
'LANGUAGE': 'French',
'DATE': 'third-quarter',
'TIME': 'hours',
'MONEY': '39;Keefe',
'ORDINAL': '2nd',
'CARDINAL': 'Two',
}
agnews_2 = {'PERSON': 'Arafat',
'NORP': 'Iraqi',
'FAC': 'Olympic',
'ORG': 'AFP',
'GPE': 'Baghdad',
'LOC': 'Earth',
'PRODUCT': 'Soyuz',
'EVENT': 'Cup',
'WORK_OF_ART': 'PowerBook',
'LAW': 'Constitution',
'LANGUAGE': 'Filipino',
'DATE': 'Sunday',
'TIME': 'evening',
'MONEY': '39;m',
'QUANTITY': '20km',
'ORDINAL': 'eighth',
'CARDINAL': '6',
}
agnews_3 = {'PERSON': 'Arafat',
'NORP': 'Iraqi',
'FAC': 'Olympic',
'ORG': 'AFP',
'GPE': 'Iraq',
'LOC': 'Kashmir',
'PRODUCT': 'Yukos',
'EVENT': 'Cup',
'WORK_OF_ART': 'Gazprom',
'LAW': 'Pacers-Pistons',
'LANGUAGE': 'Hebrew',
'DATE': 'Saturday',
'TIME': 'overnight',
'MONEY': '39;m',
'QUANTITY': '#39;t',
'ORDINAL': '11th',
'CARDINAL': '6',
}
agnews = [agnews_0, agnews_1, agnews_2, agnews_3]
yahoo_0 = {'PERSON': 'Fantasy',
'NORP': 'Russian',
'FAC': 'Taxation',
'ORG': 'Congress',
'GPE': 'U.S.',
'LOC': 'Sea',
'PRODUCT': 'Variable',
'EVENT': 'Series',
'WORK_OF_ART': 'Stopping',
'LAW': 'Constitution',
'LANGUAGE': 'Hebrew',
'DATE': '2004-05',
'TIME': 'morning',
'MONEY': '$ale',
'QUANTITY': 'Hiberno-English',
'ORDINAL': 'Tertiary',
'CARDINAL': 'three',
}
yahoo_1 = {'PERSON': 'Equine',
'NORP': 'Japanese',
'FAC': 'Music',
'ORG': 'Congress',
'GPE': 'UK',
'LOC': 'Sea',
'PRODUCT': 'RuneScape',
'EVENT': 'Series',
'WORK_OF_ART': 'Stopping',
'LAW': 'Strap-',
'LANGUAGE': 'Spanish',
'DATE': '2004-05',
'TIME': 'night',
'PERCENT': '100%',
'MONEY': 'five-dollar',
'QUANTITY': 'Hiberno-English',
'ORDINAL': 'Sixth',
'CARDINAL': '5',
}
yahoo_2 = {'PERSON': 'Equine',
'NORP': 'Canadian',
'FAC': 'Music',
'ORG': 'Congress',
'GPE': 'California',
'LOC': 'Atlantic',
'PRODUCT': 'Variable',
'EVENT': 'Series',
'WORK_OF_ART': 'Weight',
'LANGUAGE': 'Filipino',
'DATE': '2004-05',
'TIME': 'night',
'PERCENT': '100%',
'MONEY': 'ten-dollar',
'QUANTITY': '$ale',
'ORDINAL': 'Tertiary',
'CARDINAL': 'two',
}
yahoo_3 = {'PERSON': 'Equine',
'NORP': 'Irish',
'FAC': 'Music',
'ORG': 'Congress',
'GPE': 'California',
'LOC': 'Sea',
'PRODUCT': 'RuneScape',
'EVENT': 'Series',
'WORK_OF_ART': 'Weight',
'LAW': 'Strap-',
'LANGUAGE': 'Spanish',
'DATE': '2004-05',
'TIME': 'tonight',
'PERCENT': '100%',
'MONEY': 'five-dollar',
'QUANTITY': 'Hiberno-English',
'ORDINAL': 'Sixth',
'CARDINAL': '5',
}
yahoo_4 = {'PERSON': 'Equine',
'NORP': 'Irish',
'FAC': 'Music',
'ORG': 'Congress',
'GPE': 'Canada',
'LOC': 'Sea',
'PRODUCT': 'Variable',
'WORK_OF_ART': 'Stopping',
'LAW': 'Constitution',
'LANGUAGE': 'Spanish',
'DATE': '2004-05',
'TIME': 'seconds',
'PERCENT': '100%',
'MONEY': 'hundred-dollar',
'QUANTITY': 'Hiberno-English',
'ORDINAL': 'Tertiary',
'CARDINAL': '100',
}
yahoo_5 = {'PERSON': 'Equine',
'NORP': 'English',
'FAC': 'Music',
'ORG': 'Congress',
'GPE': 'Australia',
'LOC': 'Sea',
'PRODUCT': 'Variable',
'EVENT': 'Series',
'WORK_OF_ART': 'Weight',
'LAW': 'Strap-',
'LANGUAGE': 'Filipino',
'DATE': '2004-05',
'TIME': 'seconds',
'MONEY': 'hundred-dollar',
'ORDINAL': 'Tertiary',
'CARDINAL': '2000',
}
yahoo_6 = {'PERSON': 'Fantasy',
'NORP': 'Islamic',
'FAC': 'Music',
'ORG': 'Congress',
'GPE': 'California',
'LOC': 'Sea',
'PRODUCT': 'Variable',
'EVENT': 'Series',
'WORK_OF_ART': 'Stopping',
'LANGUAGE': 'Filipino',
'DATE': '2004-05',
'TIME': 'seconds',
'PERCENT': '100%',
'MONEY': '$ale',
'QUANTITY': '$ale',
'ORDINAL': 'Tertiary',
'CARDINAL': '100',
}
yahoo_7 = {'PERSON': 'Fantasy',
'NORP': 'Canadian',
'FAC': 'Music',
'ORG': 'Congress',
'GPE': 'UK',
'LOC': 'West',
'PRODUCT': 'Variable',
'EVENT': 'Watergate',
'WORK_OF_ART': 'Stopping',
'LAW': 'Constitution',
'LANGUAGE': 'Filipino',
'DATE': '2004-05',
'TIME': 'tonight',
'PERCENT': '100%',
'MONEY': '$ale',
'QUANTITY': '$ale',
'ORDINAL': 'Tertiary',
'CARDINAL': '2000',
}
yahoo_8 = {'PERSON': 'Equine',
'NORP': 'Japanese',
'FAC': 'Music',
'ORG': 'Congress',
'GPE': 'Chicago',
'LOC': 'Sea',
'PRODUCT': 'Variable',
'EVENT': 'Series',
'WORK_OF_ART': 'Stopping',
'LAW': 'Strap-',
'LANGUAGE': 'Spanish',
'DATE': '2004-05',
'TIME': 'night',
'PERCENT': '100%',
'QUANTITY': '$ale',
'ORDINAL': 'Sixth',
'CARDINAL': '2',
}
yahoo_9 = {'PERSON': 'Equine',
'NORP': 'Chinese',
'FAC': 'Music',
'ORG': 'Digital',
'GPE': 'U.S.',
'LOC': 'Atlantic',
'PRODUCT': 'Variable',
'EVENT': 'Series',
'WORK_OF_ART': 'Weight',
'LAW': 'Constitution',
'LANGUAGE': 'Spanish',
'DATE': '1918-1945',
'TIME': 'night',
'PERCENT': '100%',
'MONEY': 'ten-dollar',
'QUANTITY': 'Hiberno-English',
'ORDINAL': 'Tertiary',
'CARDINAL': '5'
}
yahoo = [yahoo_0, yahoo_1, yahoo_2, yahoo_3, yahoo_4, yahoo_5, yahoo_6, yahoo_7, yahoo_8, yahoo_9]
L = {'imdb': imdb, 'agnews': agnews, 'yahoo': yahoo}
NE_list = NameEntityList()
if __name__ == '__main__':
args = parser.parse_args()
print('dataset:', args.dataset)
class_num = config.num_classes[args.dataset]
if args.dataset == 'imdb':
train_texts, train_labels, test_texts, test_labels = split_imdb_files()
# get input texts in different classes
pos_texts = train_texts[:12500]
pos_texts.extend(test_texts[:12500])
neg_texts = train_texts[12500:]
neg_texts.extend(test_texts[12500:])
texts = [neg_texts, pos_texts]
elif args.dataset == 'agnews':
texts = [[] for i in range(class_num)]
train_texts, train_labels, test_texts, test_labels = split_agnews_files()
for i, label in enumerate(train_labels):
texts[np.argmax(label)].append(train_texts[i])
for i, label in enumerate(test_labels):
texts[np.argmax(label)].append(test_texts[i])
elif args.dataset == 'yahoo':
train_texts, train_labels, test_texts, test_labels = split_yahoo_files()
texts = [[] for i in range(class_num)]
for i, label in enumerate(train_labels):
texts[np.argmax(label)].append(train_texts[i])
for i, label in enumerate(test_labels):
texts[np.argmax(label)].append(test_texts[i])
D_true_list = []
for i in range(class_num):
D_true = recognize_named_entity(texts[i]) # D_true contains the NEs in input texts with the label y_true
D_true_list.append(D_true)
for i in range(class_num):
D_true = copy.deepcopy(D_true_list[i])
D_other = copy.deepcopy(NE_type_dict)
for j in range(class_num):
if i == j:
continue
for type in NE_type_dict.keys():
# combine D_other[type] and D_true_list[j][type]
for key in D_true_list[j][type].keys():
D_other[type][key] += D_true_list[j][type][key]
for type in NE_type_dict.keys():
D_other[type] = sorted(D_other[type].items(), key=lambda k_v: k_v[1], reverse=True)
D_true[type] = sorted(D_true[type].items(), key=lambda k_v: k_v[1], reverse=True)
print('\nfind adv_NE_list in class', i)
with open('./{}.txt'.format(args.dataset), 'a', encoding='utf-8') as f:
f.write('\nfind adv_NE_list in class' + str(i))
find_adv_NE(D_true, D_other)