-
Notifications
You must be signed in to change notification settings - Fork 0
/
mwe_features.py
499 lines (440 loc) · 18.1 KB
/
mwe_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
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
# IMPORT
# from new_reader import read_streusle_json
from utils import reconstruct_sentence
import spacy_udpipe
spacy_udpipe.download('en')
nlp = spacy_udpipe.load('en')
# DELETE_MWE = ["V.LVC.cause", "INF.P", "CCONJ", "SYM", "PRON", "NUM", "INTJ", "SCONJ"]
# VERBAL = ["V.IAV", "V.VPC.full", "V.VID", "V.VPC.semi", "V.LVC.full"]
# OTHER = ["N", "DISC", "PP", "ADV", "ADJ", "DET", "AUX", "P"]
def read_lexicon(path_file):
import csv
lexicon = []
file = csv.reader(open(path_file, 'r', encoding='utf-8'), delimiter="\t")
first_row = True
len_lexicon = 0
for row in file:
if first_row:
len_lexicon = int(row[0])
first_row = False
else:
lexicon.append((row[0], row[1], row[2]))
assert (len(lexicon) == len_lexicon)
return lexicon
# def create_lexicon_from_json(datas, lexcat):
# MWE = 'smwes'
# VMWE = 'wmwes'
# lexicon = []
# lexicon_mwe = []
#
# list_lexcat = []
# if lexcat == 'all':
# for cat in DELETE_MWE:
# list_lexcat.append(cat)
# for cat in VMWE:
# list_lexcat.append(cat)
# for cat in OTHER:
# list_lexcat.append(cat)
# elif lexcat == 'vmwe':
# for cat in VMWE:
# list_lexcat.append(cat)
# elif lexcat == 'other':
# for cat in OTHER:
# list_lexcat.append(cat)
# elif lexcat == 'mwe13':
# for cat in VMWE:
# list_lexcat.append(cat)
# for cat in OTHER:
# list_lexcat.append(cat)
# else:
# print("Error argument lexcat: " + lexcat)
# exit(1001)
#
# for corpus in datas:
# for sentence in corpus:
# for mwe, value in sentence[MWE].items():
# if value['lexlemma'] not in lexicon_mwe and value["lexcat"] in list_lexcat:
# lexicon_mwe.append(value['lexlemma'])
# if value["lexcat"] is not None and value["lexcat"] in list_lexcat:
# if value["ss"] is not None:
# lexicon.append((value['lexlemma'], value["lexcat"], value["ss"]))
# else:
# lexicon.append((value['lexlemma'], value["lexcat"], "null"))
# return lexicon
# def search_mwe_in_tweet(tweet, hateful, lexicon, count_mwe, count_mwe_label):
# tweet = tweet.lower()
# count = 0
# HATE = "hate"
# NON_HATE = "non-hate"
# GLOBAL = "global"
# if GLOBAL not in count_mwe_label:
# count_mwe_label[GLOBAL] = {}
# count_mwe_label[HATE] = {}
# count_mwe_label[NON_HATE] = {}
# if GLOBAL not in count_mwe:
# count_mwe[GLOBAL] = {}
# count_mwe[HATE] = {}
# count_mwe[NON_HATE] = {}
# for mwe in lexicon:
# if mwe[0] in tweet:
# count += 1
# # LABEL MWEs
# if mwe[1] not in count_mwe_label[GLOBAL]:
# count_mwe_label[GLOBAL][mwe[1]] = 1
# count_mwe_label[HATE][mwe[1]] = 0
# count_mwe_label[NON_HATE][mwe[1]] = 0
# else:
# count_mwe_label[GLOBAL][mwe[1]] += 1
# # HATE TWEET
# if hateful == 1:
# if mwe[1] not in count_mwe_label[HATE]:
# count_mwe_label[HATE][mwe[1]] = 1
# else:
# count_mwe_label[HATE][mwe[1]] += 1
# else:
# # Non HATE TWEET
# if mwe[1] not in count_mwe_label[NON_HATE]:
# count_mwe_label[NON_HATE][mwe[1]] = 1
# else:
# count_mwe_label[NON_HATE][mwe[1]] += 1
#
# if mwe[0] not in count_mwe[GLOBAL]:
# count_mwe[GLOBAL][mwe[0]] = 1
# else:
# count_mwe[GLOBAL][mwe[0]] += 1
# if hateful == 1:
# if mwe[0] not in count_mwe[HATE]:
# count_mwe[HATE][mwe[0]] = 1
# else:
# count_mwe[HATE][mwe[0]] += 1
# else:
# if mwe[0] not in count_mwe[NON_HATE]:
# count_mwe[NON_HATE][mwe[0]] = 1
# else:
# count_mwe[NON_HATE][mwe[0]] += 1
# return count
# def count_uniq_hate_nonhate_and_global_categories(lexicon, count_mwe, count_label):
# HATE = "hate"
# NON_HATE = "non-hate"
# GLOBAL = "global"
# count = {GLOBAL: {},
# HATE: {},
# NON_HATE: {}}
# for label in count_label[GLOBAL]:
# count[GLOBAL][label] = 0
# count[HATE][label] = 0
# count[NON_HATE][label] = 0
# for mwe in lexicon:
# if mwe[0] in count_mwe[HATE] and mwe[0] in count_mwe[NON_HATE]:
# count[GLOBAL][mwe[1]] += count_mwe[HATE][mwe[0]]
# count[GLOBAL][mwe[1]] += count_mwe[NON_HATE][mwe[0]]
# elif mwe[0] in count_mwe[HATE] and mwe[0] not in count_mwe[NON_HATE]:
# count[HATE][mwe[1]] += count_mwe[HATE][mwe[0]]
# elif mwe[0] not in count_mwe[HATE] and mwe[0] in count_mwe[NON_HATE]:
# count[NON_HATE][mwe[1]] += count_mwe[NON_HATE][mwe[0]]
#
# return count
# def search_mwe_in_tweet_with_discontinuity(tweet, lexicon):
# import re
# tweet = tweet.lower()
# count_mwe = 0
# for mwe in lexicon:
# pattern = ''
# for word in mwe[0].split():
# if '+' in word:
# pattern += "\+" + '\s[\w]+'
# else:
# pattern += word + '\s[\w]+'
# regex = re.compile(pattern, re.IGNORECASE)
# if regex.findall(tweet):
# count_mwe += len(regex.findall(tweet))
# return count_mwe
def lemmatize_tweet(tweet):
parsing = nlp(tweet)
tweet_lemmatize = ''
for word in parsing:
tweet_lemmatize += word.lemma_ + ' '
return tweet_lemmatize
def no_overlaps(mwes):
if len(mwes) == 1:
return mwes
no_overlaps_in_mwes = []
for x in mwes:
no_overlaps_bool = True
for y in mwes:
if x != y:
if min(x) >= min(y) >= max(x):
no_overlaps_bool = False
if min(x) >= max(y) >= max(x):
no_overlaps_bool = False
if no_overlaps_bool:
no_overlaps_in_mwes.append(x)
return no_overlaps_in_mwes
def tokenize_label_mwe(lexicon):
vocab_label_mwe = {'<pad>': 0, 'NOMWE': 1}
vocab_label_mwe_strong = {'<pad>': 0, 'NOMWE': 1}
for mwe in lexicon:
if mwe[1] not in vocab_label_mwe:
vocab_label_mwe[mwe[1]] = len(vocab_label_mwe)
if mwe[2] not in vocab_label_mwe_strong:
vocab_label_mwe_strong[mwe[2]] = len(vocab_label_mwe_strong)
return vocab_label_mwe, vocab_label_mwe_strong
def annotated_corpus(tweets, lexicon, vocab, vocab_strong, max_len_features):
import numpy as np
vector_mwe_tweets = []
vector_mwe_strong_tweets = []
from tqdm import tqdm
for tweet in tqdm(tweets):
try:
tweet_lemmatized = lemmatize_tweet(tweet).split()
except:
tweet_lemmatized = tweet.split()
vector_tweet = np.zeros(max_len_features)
vector_tweet_strong = np.zeros(max_len_features)
for i in range(len(tweet_lemmatized)):
vector_tweet[i] = 1.0
vector_tweet_strong[i] = 1.0
mwes = []
mwe_label = []
mwe_label_strong = []
for mwe in lexicon:
mwe_split = mwe[0].split()
for index_lemma in range(len(tweet_lemmatized)):
if mwe_split[0] == tweet_lemmatized[index_lemma]:
mwe_index = []
if len(mwe_split) + index_lemma <= len(tweet_lemmatized):
for index_mwe in range(len(mwe_split)):
if mwe_split[index_mwe] == tweet_lemmatized[index_lemma + index_mwe]:
mwe_index.append(index_lemma + index_mwe)
if len(mwe_index) == len(mwe_split):
mwes.append(mwe_index)
mwe_label.append(mwe[1])
mwe_label_strong.append(mwe[2])
mwes = no_overlaps(mwes)
for mwe in range(len(mwes)):
for index in mwes[mwe]:
vector_tweet[index] = vocab[mwe_label[mwe]] # vocab_label_mwe[mwe_label[mwe]]
vector_tweet_strong[index] = vocab_strong[mwe_label_strong[mwe]] # vocab_label_mwe[mwe_label[mwe]]
vector_mwe_tweets.append(vector_tweet)
vector_mwe_strong_tweets.append(vector_tweet_strong)
return np.array(vector_mwe_tweets), np.array(vector_mwe_strong_tweets)
def annotated_corpus_with_discontinuity(tweets, lexicon, vocab, vocab_strong, max_len_features):
import re
import numpy as np
vector_mwe_tweets = []
vector_mwe_strong_tweets = []
from tqdm import tqdm
for tweet in tqdm(tweets):
try:
tweet_lemmatized = lemmatize_tweet(reconstruct_sentence(tweet))
except:
tweet_lemmatized = tweet
vector_tweet = np.zeros(max_len_features)
vector_tweet_strong = np.zeros(max_len_features)
for i in range(len(tweet_lemmatized.split())):
vector_tweet[i] = 1.0
vector_tweet_strong[i] = 1.0
mwes = []
mwe_label = []
mwe_label_strong = []
for mwe in lexicon:
pattern_discont = ""
pattern_cont = ""
for word in mwe[0].split():
if '+' in word:
pattern_discont += '\+' + '\s' + '[\w]+'
pattern_cont += '\+' + '\s'
else:
pattern_discont += word + '\s' + '[\w]+'
pattern_cont += word + '\s'
regex_discont = re.compile(pattern_discont, re.IGNORECASE)
regex_cont = re.compile(pattern_cont, re.IGNORECASE)
mwe_candidate = []
for res in regex_cont.findall(tweet_lemmatized):
mwe_candidate.append(res)
for res in regex_discont.findall(tweet_lemmatized):
mwe_candidate.append(res)
for mwe_c in mwe_candidate:
mwe_index = []
try:
for word in mwe_c.split():
mwe_index.append(tweet_lemmatized.split().index(word))
mwes.append(mwe_index)
mwe_label.append(mwe[1])
mwe_label_strong.append(mwe[2])
except:
mwe_index=[]
mwes = no_overlaps(mwes)
for mwe in range(len(mwes)):
for index in mwes[mwe]:
vector_tweet[index] = vocab[mwe_label[mwe]] # vocab_label_mwe[mwe_label[mwe]]
vector_tweet_strong[index] = vocab_strong[mwe_label_strong[mwe]] # vocab_label_mwe[mwe_label[mwe]]
vector_mwe_tweets.append(vector_tweet)
vector_mwe_strong_tweets.append(vector_tweet_strong)
return np.array(vector_mwe_tweets), np.array(vector_mwe_strong_tweets)
def write_vocab_mwe(path, vocab_mwe, vocab_mwe_strong):
file_vocab_mwe = open(path + '.voc', 'w', encoding='utf-8')
for key, value in vocab_mwe.items():
file_vocab_mwe.write(key + '\t' + str(value) + '\n')
file_vocab_mwe.close()
file_vocab_mwe_strong = open(path + '.vocstrong', 'w', encoding='utf-8')
for key, value in vocab_mwe_strong.items():
file_vocab_mwe_strong.write(key + '\t' + str(value) + '\n')
file_vocab_mwe_strong.close()
def load_vocab_mwe(path):
file_vocab_mwe = open(path, 'r', encoding=('utf-8'))
vocab = {}
for line in file_vocab_mwe.readlines():
vocab[line.split()[0]] = int(line.split()[1])
return vocab
def write_vector(path, vector_mwe):
import csv
writer = csv.writer(open(path, 'w', encoding="utf-8"), delimiter="\t")
for vector in vector_mwe:
writer.writerow(vector.tolist())
def load_vector(path, size):
import csv
import numpy as np
vectors = []
reader = csv.reader(open(path, 'r', encoding="utf-8"), delimiter="\t")
for row in reader:
assert len(row) == size
vector = np.zeros(size)
for v_index in range(len(row)):
vector[v_index] = float(row[v_index])
vectors.append(vector)
return np.array(vectors)
def annotated_mwe(path_lexicon, path_files=[], discontinuity=False, is_founta=False):
lexicon = read_lexicon(path_lexicon)
vocab_mwe, vocab_mwe_strong = tokenize_label_mwe(lexicon)
# Write vocab, vocab_strong
write_vocab_mwe(path_lexicon.split(".txt")[0], vocab_mwe, vocab_mwe_strong)
from utils import read_hateval
from utils import read_founta
for path in path_files:
# Load tweets
if is_founta:
tweets, labels, vocab = read_founta(path)
else:
tweets, label, vocab = read_hateval(path)
# Vectorize tweets
if discontinuity:
vector_mwe, vector_strong_mwe_features = annotated_corpus_with_discontinuity(
tweets=tweets, lexicon=lexicon, vocab=vocab_mwe,
vocab_strong=vocab_mwe_strong,
max_len_features=280)
else:
vector_mwe, vector_strong_mwe_features = annotated_corpus(
tweets=tweets, lexicon=lexicon, vocab=vocab_mwe,
vocab_strong=vocab_mwe_strong,
max_len_features=280)
# Write vectors
write_vector(path.split('.csv')[0] + ".mwe." + path_lexicon.split("/")[-1].split(".txt")[0], vector_mwe)
write_vector(path.split('.csv')[0] + ".mwestrong." + path_lexicon.split("/")[-1].split(".txt")[0],
vector_strong_mwe_features)
def annotated_only_mwe_features(path_lexicon, path_files, is_founta):
import csv
lexicon = read_lexicon(path_lexicon)
vocab_mwe, vocab_mwe_strong = tokenize_label_mwe(lexicon)
# Write vocab, vocab_strong
write_vocab_mwe(path_lexicon.split(".txt")[0], vocab_mwe, vocab_mwe_strong)
from utils import read_hateval
from utils import read_founta
for path in path_files:
# Load tweets
if is_founta:
tweets, labels, vocab = read_founta(path)
else:
tweets, label, vocab = read_hateval(path)
mwe_features = only_mwe_annotation(tweets=tweets, lexicon=lexicon)
writer = csv.writer(
open(path.split('.csv')[0] + ".mwe." + path_lexicon.split("/")[-1].split(".mwelemmas")[0], 'w',
encoding='UTF-8'), delimiter="\t")
for mwe in mwe_features:
writer.writerow(mwe)
def only_mwe_annotation(tweets, lexicon):
mwe_features_tweets = []
from tqdm import tqdm
for tweet in tqdm(tweets):
try:
tweet_lemmatized = lemmatize_tweet(reconstruct_sentence(tweet)).split()
except:
tweet_lemmatized = tweet
mwes = []
mwe_label = []
mwe_label_strong = []
for mwe in lexicon:
mwe_split = mwe[0].split()
for index_lemma in range(len(tweet_lemmatized)):
if mwe_split[0] == tweet_lemmatized[index_lemma]:
mwe_index = []
if len(mwe_split) + index_lemma < len(tweet_lemmatized) - 1:
for index_mwe in range(len(mwe_split)):
if mwe_split[index_mwe] == tweet_lemmatized[index_lemma + index_mwe]:
mwe_index.append(index_lemma + index_mwe)
if len(mwe_index) == len(mwe_split):
mwes.append(mwe_index)
mwe_label.append(mwe[1])
mwe_label_strong.append(mwe[2])
mwes = no_overlaps(mwes)
mwe_feat = []
mwes = sorted(mwes)
for mwe in range(len(mwes)):
for index in mwes[mwe]:
mwe_feat.append(tweet_lemmatized[index])
mwe_features_tweets.append(mwe_feat)
return mwe_features_tweets
def read_mwes(path, vocab_mwe, size):
import csv
import numpy as np
reader = csv.reader(open(path, 'r', encoding='UTF-8'), delimiter='\t')
mwe_features = []
for row in reader:
vector = []
mwes = []
for index_word in range(len(row)):
if row[index_word] != '':
mwes.append(row[index_word])
for pad in range(size - len(mwes)):
vector.append(vocab_mwe['<pad>'])
for mwe in mwes:
vector.append(vocab_mwe[mwe])
mwe_features.append(np.array(vector))
return np.array(mwe_features)
if __name__ == '__main__':
# MWE = 'smwes'
# VMWE = 'wmwes'
import argparse
parser = argparse.ArgumentParser(description="""
System to count multi word expressions in tweets.
""")
parser.add_argument("--path_files", dest="path_files",
required=False, type=str, nargs='+',
help="""
File at HatEval (Basile et al., 2019) and Founta (Founta et al., 2018) format.
""")
parser.add_argument("--path_lexicon", dest="path_lexicon",
required=True, type=str,
help="""
Lexicon path.
""")
parser.add_argument("--discontinuity", dest="discontinuity",
required=False, type=bool, nargs='?', const=True,
help="""
Option to use annotate the corpus with the possible discontinuity of the MWE.
""")
parser.add_argument("--founta", dest="founta",
required=False, type=bool, nargs='?', const=True,
help="""
If you are using Founta et al. (2018) corpus.
""")
parser.add_argument("--word_embeddings_feature", dest="word_embeddings_features",
required=False, type=bool, nargs='?', const=True,
help="""
Option to extract words which composed a MWE.
""")
args = parser.parse_args()
if args.word_embeddings_features:
annotated_only_mwe_features(args.path_lexicon, args.path_files, args.founta)
else:
annotated_mwe(args.path_lexicon, args.path_files, args.discontinuity, args.founta)