-
Notifications
You must be signed in to change notification settings - Fork 0
/
semantic_symbol_extractor.py
286 lines (280 loc) · 10.4 KB
/
semantic_symbol_extractor.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
import OpenHowNet
import scipy
import numpy as np
import nltk
import spacy
hownet_dict = OpenHowNet.HowNetDict()
#%%
f_words = "f_words.txt"
with open(f_words,encoding='utf8') as f:
s_words = f.readlines()
for w in range(len(s_words)):
i = s_words[w].find(' ')
s_words[w] = s_words[w][:i]
#%% Musa's Method
sememe_list_path = "SKB_mrd2skb/mrd2skb_sememes.txt"
sememe_dict_path = "SKB_mrd2skb/mrd2skb_skb.txt"
#sememe_dict_path = "SKB_mrd2skb/hownet_en.txt"
#lemmatization_path = "DictSKB-main/NLI/dataset/lemmatization.txt"
with open(sememe_dict_path,encoding='utf8') as f:
sememe_dict = f.readlines()
with open(sememe_list_path,encoding='utf8') as f:
sememe_list = f.readlines()
# with open(lemmatization_path,encoding='utf8') as f:
# lemmatization = f.readlines()
# #%% New Sememe Generation Method
# sememe_dict_path = "DictSKB-main/NLI/dataset/sememe_dict.txt"
# sememe_list_path = "DictSKB-main/NLI/dataset/sememe.txt"
# lemmatization_path = "DictSKB-main/NLI/dataset/lemmatization.txt"
# with open(sememe_dict_path,encoding='utf8') as f:
# sememe_dict = f.readlines()
# with open(sememe_list_path,encoding='utf8') as f:
# sememe_list = f.readlines()
# with open(lemmatization_path,encoding='utf8') as f:
# lemmatization = f.readlines()
#%%
sememe_dictionary = {}
for line in range(0,len(sememe_dict),2):
word = sememe_dict[line].strip()
if len(word.split()) == 1:
sememes = sememe_dict[line+1].split()
if len(sememes) > 0:
sememe_dictionary[word] = sememes
#%% SPACY SIMILARITY
import gensim.downloader as api
from sklearn.metrics.pairwise import cosine_similarity
model = api.load("glove-twitter-200")
#v_1 = model.get_vector('phone').reshape(1,-1)
#v_2 = model.get_vector('computer').reshape(1,-1)
#sim = cosine_similarity(v_1,v_2)
#%%
sememe_glove_dict = {}
word_glove_dict = {}
for k in list(sememe_dictionary.keys()):
sememes = sememe_dictionary[k]
for w in sememes:
if w not in sememe_glove_dict:
try:
v_i = model.get_vector(w).reshape(1,-1)
sememe_glove_dict[w] = v_i
except:
v_i = np.zeros((1,200))
sememe_glove_dict[w] = v_i
try:
word_glove_dict[k] = model.get_vector(k).reshape(1,-1)
except:
word_glove_dict[k] = np.zeros((1,200))
# #%% ABLATION STUDY İÇİN RANDOM SEÇME SEMEMELERİ
# chosen_sememe_dictionary = {}
# for k in list(sememe_dictionary.keys()):
# if len(sememe_dictionary[k]) < 4:
# chosen_sememe_dictionary[k] = sememe_dictionary[k]
# else:
# chosen_sememe_dictionary[k] = sememe_dictionary[k][:3]
#%% SEMEME CHOSING WITH GLOVE
chosen_sememe_dictionary = {}
for key in word_glove_dict.keys():
sememes = sememe_dictionary[key]
top_sim = np.ones((1,(len(sememes))))*-1
v_1 = word_glove_dict[key]
top_k_sem = []
for s in range(len(sememes)):
v_2 = sememe_glove_dict[sememes[s]]
top_sim[0,s] = cosine_similarity(v_1,v_2)
try:
ind = np.argsort(top_sim)[:,-3:]
except:
try:
ind = np.argpartition(top_sim)[:-2]
except:
ind = np.argpartition(top_sim)[:-1]
for idx in ind[0,:]:
top_k_sem.append(sememes[idx])
chosen_sememe_dictionary[key] = top_k_sem
#%%
# from itertools import combinations
# from scipy.spatial import distance
# chosen_sememe_dictionary = {}
# for key in word_glove_dict.keys():
# sememes = sememe_dictionary[key]
# if len(sememes) < 4 :
# chosen_sememe_dictionary[key] = sememes
# else:
# comb = list(combinations(sememes,3))
# distances = np.zeros(len(comb))
# for i in range(len(comb)):
# w_1 = sememe_glove_dict[comb[i][0]]
# w_2 = sememe_glove_dict[comb[i][1]]
# w_3 = sememe_glove_dict[comb[i][2]]
# distances[i] += distance.euclidean(w_1,w_2)
# distances[i] += distance.euclidean(w_1,w_3)
# distances[i] += distance.euclidean(w_2,w_3)
# chosen_sememe_dictionary[key] = list(comb[np.argmax(distances)])
#%%
# import pickle
# with open('new_algo_chosen_sememe_dictionary', 'wb') as handle:
# pickle.dump(chosen_sememe_dictionary, handle, protocol=pickle.HIGHEST_PROTOCOL)
#%% RANDOM CHOSING
limited_sememe_dictionary = {}
for key in sememe_dictionary.keys():
sememe_limited = sememe_dictionary[key][0:3]
limited_sememe_dictionary[key] = sememe_limited
#%% Generate a list of English sememes in HowNet
key_list = list(hownet_dict.sememe_dic.keys())
key_list_en =[]
for word in key_list:
for i in range(len(word)):
if word[i] == '|':
key_list_en.append(word[0:i])
#%% LOAD MUSA GROUNDTRUTH
import pandas as pd
import pickle
with open('train_sentences.pickle', 'rb') as handle:
sentence_list_train = pickle.load(handle)
with open('validation_sentences.pickle', 'rb') as handle:
sentence_list_val = pickle.load(handle)
with open('test_sentences.pickle', 'rb') as handle:
sentence_list_test = pickle.load(handle)
# sentence_list_train =list(pd.read_csv('europarl_dataset_processed/train_data_ner_036.csv')['English'])
# sentence_list_val = list(pd.read_csv('europarl_dataset_processed/validation_data_ner_036.csv')['English'])
# sentence_list_testt= scipy.io.loadmat('europarl_dataset_processed/test_groundtruth_ner.mat')['sentence_list_sonnn']
#%%
from nltk.stem import WordNetLemmatizer
from nltk.corpus import wordnet
class custom_lemmatizer:
tag_dict = {"ADJ": wordnet.ADJ,
"NOUN": wordnet.NOUN,
"VERB": wordnet.VERB,
"ADV" : wordnet.ADV,
}
lemmatizer = WordNetLemmatizer()
def lemmatize(self, word_pos_tuple):
word = word_pos_tuple[0]
pos_tag = word_pos_tuple[1]
if pos_tag in self.tag_dict:
return self.lemmatizer.lemmatize(word,
self.tag_dict[pos_tag]).lower()
else:
return word.lower()
cm = custom_lemmatizer()
#%%
def sememes_from_word_dict(word,s_words,sememe_dict):
if word in s_words:
return [word]
elif word[-6:] =='entity':
return [word[:-6]]
else:
try:
word_sememes = sememe_dict[word]
return word_sememes[:3]
except:
return [word]
#%% DATASET GENERATION
def sememes_from_word_limited(word,s_words,max_sem):
if word in s_words:
return [word]
else:
sememes = hownet_dict.get_sememes_by_word(word = word, display='list', merge=True, K=None)
word_sememes = []
for i in range(min(max_sem,len(sememes))):
word_sememes.append(str(sememes[i]).split("|")[0])
if len(word_sememes) > 0:
return word_sememes
else:
# for ch in word:
# word_sememes.append(ch)
# return word_sememes
return [word]
#%%
def sememes_from_word(word,s_words):
if word in s_words:
return [word]
else:
sememes = hownet_dict.get_sememes_by_word(word = word, display='list', merge=True, K=None)
word_sememes = []
for i in range(len(sememes)):
word_sememes.append(str(sememes[i]).split("|")[0])
if len(word_sememes) > 0:
return word_sememes
#%%
def sememes_from_word_char(word,s_words):
if word in s_words:
return [word]
else:
sememes = hownet_dict.get_sememes_by_word(word = word, display='list', merge=True, K=None)
word_sememes = []
for i in range(len(sememes)):
word_sememes.append(str(sememes[i]).split("|")[0])
if len(word_sememes) > 0:
return word_sememes
else:
for ch in word:
word_sememes.append(ch)
return word_sememes
def extract_words_only(word, word_list):
return[word]
def sememe_extraction(dataset):
to_tokenize = dataset
lemmatized_s_list = []
for s in range(len(to_tokenize)):
tokenized_s = nltk.tokenize.word_tokenize(to_tokenize[s])
tagged_s = nltk.tag.pos_tag(tokenized_s, tagset='universal')
lemm = []
for t in tagged_s:
lemmatized_s = cm.lemmatize(t)
lemm.append(lemmatized_s)
lemmatized_s_list.append(lemm)
extracted_sememes_sent = []
for sentence in lemmatized_s_list:
ext_sem = []
for word in sentence:
#s_w = sememes_from_word_limited(word,s_words,3)
s_w = sememes_from_word_dict(word,s_words,chosen_sememe_dictionary)
#s_w = sememes_from_word_char(word,s_words)
#s_w = extract_words_only(word,s_words)
ext_sem.append(s_w)
extracted_sememes_sent.append(ext_sem)
extracted_sememes_one_list = []
for sent in extracted_sememes_sent:
list_sememes = []
for semm in sent:
if len(semm) > 0:
for w in semm:
list_sememes.append(w)
#list_sememes.append('spacee')
extracted_sememes_one_list.append(list_sememes)
extracted_sememes_text = []
for semm in extracted_sememes_one_list:
txt = ''
for w in semm:
txt += w
txt += ' '
extracted_sememes_text.append(txt)
return extracted_sememes_text
#%%
train_data = sememe_extraction(sentence_list_train)
test_data = sememe_extraction(sentence_list_test)
val_data = sememe_extraction(sentence_list_val)
#%%
import csv
with open('train_raw.pickle', 'rb') as handle:
train_raw = pickle.load(handle)
with open('validation_raw.pickle', 'rb') as handle:
val_raw = pickle.load(handle)
with open('test_raw.pickle', 'rb') as handle:
test_raw = pickle.load(handle)
new_list = []
for s in range(120000):
new_list.append([train_data[s],train_raw[s]])
df = pd.DataFrame(new_list, columns =['Sememe', 'English'])
df.to_csv("./train_data.csv", sep=',',index=False)
new_list = []
for s in range(25677):
new_list.append([test_data[s],test_raw[s]])
df = pd.DataFrame(new_list, columns =['Sememe', 'English'])
df.to_csv("./test_data.csv", sep=',',index=False)
new_list = []
for s in range(20000):
new_list.append([val_data[s],val_raw[s]])
df = pd.DataFrame(new_list, columns =['Sememe', 'English'])
df.to_csv("./validation_data.csv", sep=',',index=False)