-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordnet_sandbox.py
358 lines (305 loc) · 10.6 KB
/
wordnet_sandbox.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
# -*- coding: utf-8 -*-
"""
Created on Thu May 2 14:40:19 2019
@author: Mark_S
"""
from gensim.utils import simple_preprocess
from gensim.parsing.preprocessing import STOPWORDS
from gensim.models import KeyedVectors
from nltk.stem import SnowballStemmer
from nltk.corpus import wordnet as wn
from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
from nltk.data import find
import numpy as np
import pandas as pd
in_file = 'snli_1.0_dev.csv'
out_file = 'syntax_labels.txt'
ppos = {'n':'NN',
'v':'VB',
'adj': 'JJ',
'adv':'RB'}
stemmer = SnowballStemmer('english')
path_scores = {}
syn_scores = {}
def main():
df = pd.read_csv(in_file)
conditions = [(df['gold_label'] == 'entailment'),
(df['gold_label'] == 'neutral'),
(df['gold_label'] == 'contradiction')]
choices = [1, 0, -1]
df['label'] = np.select(conditions, choices)
df = df.drop(labels = ['sentence1_binary_parse', 'sentence2_binary_parse',
'captionID', 'pairID', 'label1', 'label2', 'label3',
'label4', 'label5', 'gold_label'], axis=1)
sent_bleu_scores = {pos:[] for pos in ppos.keys()}
bigram_scores = {pos:[] for pos in ppos.keys()}
object_sim_scores = []
pp_object_sim_scores = []
def_scores = []
indef_scores = []
stops = [len(df)*((i+1)/4) for i in range(4)]
for i, row in enumerate(df.iterrows()):
if i in stops:
print(i/len(df))
row = row[1]
text1, text2 = row['sentence1'], row['sentence2']
p1, p2 = row['sentence1_parse'], row['sentence2_parse']
ob_1, ob_2 = get_object(p1), get_object(p2)
ppob_1, ppob_2 = get_object(p1, head='PP'), get_object(p2, head='PP')
get_sent_bleu_score(p1, p2, sent_bleu_scores)
#get_sent_bigram_scores(p1, p2, bigram_scores)
object_sim_scores.append(weighted_avg(get_syn_score(ob_1, ob_2, 'n')))
pp_object_sim_scores.append(weighted_avg(get_syn_score(ppob_1, ppob_2, 'n')))
def_scores.append(get_definite_count(p1) - get_definite_count(p2))
indef_scores.append(get_indefinite_count(p1) - get_indefinite_count(p2))
df['n_scores'], df['v_scores'] = sent_bleu_scores['n'], sent_bleu_scores['v']
df['adj_scores'], df['adv_scores'] = sent_bleu_scores['adj'], sent_bleu_scores['adv']
df['object_scores'] = object_sim_scores
df['pp_object_scores'] = pp_object_sim_scores
df['def_scores'] = def_scores
df['indef_scores'] = indef_scores
#df['n_bigram_scores'], df['v_bigram_scores'] = bigram_scores['n'], bigram_scores['v']
#df['adj_bigram_scores'], df['adv_bigram_scores'] = bigram_scores['adj'], bigram_scores['adv']
def get_sent_bigram_scores(parse_1, parse_2, scores):
bigrams_1, bigrams_2 = get_bigrams(parse_1), get_bigrams(parse_2)
pos_1, pos_2 = get_word_and_pos(parse_1), get_word_and_pos(parse_2)
for pos in ppos.keys():
bg_1 = get_bigrams_same_pos(bigrams_1, pos_1, pos)
bg_2 = get_bigrams_same_pos(bigrams_2, pos_2, pos)
scores[pos].append(weighted_avg(score_bigrams(bg_1, bg_2)))
def get_bigrams(sent):
result = []
sent = sent.split(sep='(')
try:
next_tok = sent[1].split()[1]
except IndexError:
return ''
for i, tok in enumerate(sent[:-1]):
tok = tok.split()
if len(tok) < 2:
continue
tok = tok[1]
result.append((tok, next_tok))
next_tok = sent[i+1]
return result
def get_bigrams_same_pos(bigrams, sent_pos, pos):
result = []
for bg in bigrams:
if sent_pos[bg[1]] == pos:
result.append(bg)
return result
def score_bigrams(bigrams_1, bigrams_2):
result = []
for bg in bigrams_1:
bg_score = []
for comp_bg in bigrams_2:
bg_score.append(get_syn_score(bg, comp_bg))
result.append(max(bg_score))
return result
def get_sent_bleu_score(p1, p2, scores):
for pos in ppos.keys():
words_1, words_2 = get_pos(p1, pos, stemmer), get_pos(p2, pos, stemmer)
scores[pos].append(get_bleu_score(words_1, words_2))
def get_object(parse, stemmer=SnowballStemmer('english'), head='VP'):
result = []
parse = parse.split(sep="("+head)
for clause in parse:
x = get_pos(clause, 'n')
if x:
result.append(x[0])
return result
def get_word_and_pos(parse):
result = {}
for lexeme in parse.split(sep='('):
lexeme = lexeme.split()
if len(lexeme) < 2:
continue
pos_tag = lexeme[0][:2]
lemma = stemmer.stem(lexeme[1])
if pos_tag in ppos:
result[lemma] = pos_tag
else:
result[lemma] = '??'
return result
def get_pos(parse, pos, stemmer=SnowballStemmer('english')):
result = []
parse = parse.replace(')','')
parse = parse.split(sep='(')
for lexeme in parse:
lexeme = lexeme.split()
if len(lexeme) < 2:
continue
if lexeme[0][:2] == ppos[pos]:
lemma = lexeme[1]
result.append(stemmer.stem(lemma))
return(result)
def get_synonyms(word, pos=None):
result = []
syns = wn.synsets(word, pos)
for s in syns:
for l in s.lemmas():
lemma_form = l.name()
if lemma_form not in result:
result.append(lemma_form)
return result
def get_syns_of_syns(word, pos=None):
result = get_synonyms(word, pos)
for x in result[:]:
these_syns = get_synonyms(x)
for syn in these_syns:
if syn not in result:
result.append(syn)
return result
def get_antonyms(word, pos=None):
result = []
ants = wn.synsets(word, pos)
for a in ants:
for l in a.lemmas():
these_ants = l.antonyms()
for this_a in these_ants:
lemma_form = this_a.name()
if lemma_form not in result:
result.append(lemma_form)
return result
def get_ants_of_syns(word, pos=None):
syns = get_synonyms(word, pos)
result = get_antonyms(word, pos)
for x in syns:
these_ants = get_antonyms(x)
for a in these_ants:
if a not in result:
result.append(a)
return result
def get_syns_of_ants(word, pos=None):
result = get_antonyms(word, pos)
for x in result[:]:
these_syns = get_synonyms(x)
for syn in these_syns:
if syn not in result:
result.append(syn)
return result
def get_ants_of_ants(word, pos=None):
result = []
ants = get_antonyms(word, pos)
for x in ants:
these_ants = get_antonyms(x)
for a in these_ants:
if a not in result:
result.append(a)
return result
def is_synonym(word_1, word_2, pos=None):
return word_1 in get_synonyms(word_2, pos) \
or word_2 in get_synonyms(word_1, pos)
def is_antonym(word_1, word_2, pos=None):
return word_1 in get_antonyms(word_2, pos) \
or word_2 in get_antonyms(word_1, pos)
def is_remote_synonym(word_1, word_2, pos=None):
return word_1 in get_syns_of_syns(word_2, pos)\
or word_1 in get_ants_of_ants(word_2, pos)\
or word_2 in get_syns_of_syns(word_1, pos)\
or word_2 in get_ants_of_ants(word_1, pos)
def is_remote_antonym(word_1, word_2, pos=None):
return word_1 in get_syns_of_ants(word_2, pos)\
or word_1 in get_ants_of_syns(word_2, pos)\
or word_2 in get_syns_of_ants(word_1, pos)\
or word_2 in get_ants_of_syns(word_1, pos)\
def get_syn_score(sent_1, sent_2, pos):
result = []
for word in sent_1:
w_s = []
for comp_word in sent_2:
w_s.append(get_syn_score_word(word, comp_word, pos))
if w_s:
result.append(min(w_s))
return result
def get_syn_score_word(word_1, word_2, pos):
sent_1 = sent_1.split() if type(sent_1) is str else sent_1
sent_2 = sent_2.split() if type(sent_2) is str else sent_2
if (word_1, word_2) in syn_scores.keys():
return syn_scores[(word_1, word_2)]
if is_synonym(word_1, word_2, pos):
syn_scores[(word_1, word_2)] = 1
return 1
elif is_antonym(word_1, word_2, pos):
syn_scores[(word_1, word_2)] = -1
return -1.5
elif is_remote_synonym(word_1, word_2, pos):
syn_scores[(word_1, word_2)] = 0.5
return 0.75
elif is_remote_antonym(word_1, word_2, pos):
syn_scores[(word_1, word_2)] = -0.5
return -1
else:
syn_scores[(word_1, word_2)] = 0
return 0
def get_path_score(sent_1, sent_2, pos):
sent_1 = sent_1.split() if type(sent_1) is str else sent_1
sent_2 = sent_2.split() if type(sent_2) is str else sent_2
sent_scores = []
for word in sent_1:
w_s = []
synsets = wn.synsets(word, pos)
if not synsets:
continue
for comp_word in sent_2:
if (word, comp_word) in path_scores.keys():
w_s.append(path_scores[(word, comp_word)])
continue
comp_synsets = wn.synsets(comp_word, pos)
if not comp_synsets:
continue
for ss in synsets:
for c_ss in comp_synsets:
score = ss.wup_similarity(c_ss)
if score:
path_scores[(word, comp_word)] = score
w_s.append(score)
if w_s:
w_s = max(w_s)
sent_scores.append(w_s)
return sent_scores
def get_bleu_score(ref, hyp):
ref = ref.split() if type(ref) is str else ref
hyp = hyp.split() if type(hyp) is str else hyp
return sentence_bleu([ref], hyp, weights = [1,0.75,0.3,0.1],\
smoothing_function=SmoothingFunction().method1)
def get_definite_count(s):
s = s.lower().replace(')', '')
try:
return s.split().count('the')**2 / s.count('np')
except ZeroDivisionError:
return 0
def get_indefinite_count(s):
s = s.lower().replace(')','')
try:
return (s.split().count('a') + s.split().count('an'))**2 / s.count('np')
except ZeroDivisionError:
return 0
def avg(l):
try:
return sum(l) / len(l)
except ZeroDivisionError:
return 0
def weighted_avg(l):
if not l: # avoids some potential errors, namely empty lists
return 0
return(avg(l) + avg([x for x in l if x != 0]))
def safe_max(l):
try:
return max(l)
except ValueError:
return 0
def safe_min(l):
try:
return min(l)
except ValueError:
return 0
def safe_median(l):
try:
return l[len(l)//2]
except IndexError:
return 0
main()
#woman = wn.synset('woman.n.01')
#sister = wn.synset('sister.n.01')
#print(is_remote_synonym('sibling', 'brother'))