forked from tca19/dict2vec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
executable file
·210 lines (182 loc) · 7.85 KB
/
evaluate.py
File metadata and controls
executable file
·210 lines (182 loc) · 7.85 KB
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
#!/usr/bin/env python3
#
# Copyright (c) 2017-present, All rights reserved.
# Written by Julien Tissier <30314448+tca19@users.noreply.github.com>
#
# This file is part of Dict2vec.
#
# Dict2vec is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Dict2vec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License at the root of this repository for
# more details.
#
# You should have received a copy of the GNU General Public License
# along with Dict2vec. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import math
import argparse
import glob
import numpy as np
import scipy.stats as st
import requests
import bz2
from bs4 import BeautifulSoup as bs
FILE_DIR = "data/eval/"
results = dict()
missed_pairs = dict()
missed_words = dict()
imprs = dict()
def tanimotoSim(v1, v2):
"""Return the Tanimoto similarity between v1 and v2 (numpy arrays)"""
dotProd = np.dot(v1, v2)
return dotProd / (np.linalg.norm(v1)**2 + np.linalg.norm(v2)**2 - dotProd)
def cosineSim(v1, v2):
"""Return the cosine similarity between v1 and v2 (numpy arrays)"""
dotProd = np.dot(v1, v2)
return dotProd / (np.linalg.norm(v1) * np.linalg.norm(v2))
def make_synonyms():
"""Download synonyms"""
url = 'https://github.com/putnich/sr-sh-nlp/blob/main/SH-Wiktionary/SH-Wiktionary-Synonyms.xml.bz2?raw=true'
path = 'data/SH-Wiktionary-Synonyms.xml'
synonyms_weak_path = FILE_DIR + 'synonyms-weak.txt'
synonyms_strong_path = FILE_DIR + 'synonyms-strong.txt'
if os.path.exists(synonyms_weak_path) and os.path.exists(synonyms_strong_path):
return
with open(glob.glob('data/strong-pairs*')[0], 'r', encoding='utf-8') as f:
strong_pairs = f.read().split('\n')
with open(glob.glob('data/weak-pairs*')[0], 'r', encoding='utf-8') as f:
weak_pairs = f.read().split('\n')
if not os.path.exists(path):
result = requests.get(url, allow_redirects=True)
obj = bz2.BZ2Decompressor()
with open(path, 'wb') as f:
f.write(obj.decompress(result.content))
"""Generate synonyms list"""
with open(path, 'r', encoding='utf-8') as f:
soup = bs(f.read(), 'html.parser')
entries = soup.find_all('entry')
synonyms_strong = list()
synonyms_weak = list()
with open('dict-dl/5000-words.txt', 'r', encoding='utf-8') as f:
words = f.read().split('\n')
for entry in entries:
head = entry.find('orth').text.strip()
if not head in words:
continue
xrs = entry.find_all('xr', {'type': 'synonymy'})
for xr in xrs:
innerlinks = xr.find_all('innerlink')
synonym = ''.join([i.text.strip() for i in innerlinks])
if head == synonym:
continue
pair = head + ' ' + synonym
if synonym in words and pair not in synonyms_strong and pair in strong_pairs:
synonyms_strong.append(pair)
if synonym in words and pair not in synonyms_weak and pair in weak_pairs:
synonyms_weak.append(pair)
with open(synonyms_strong_path, 'w+', encoding='utf-8') as f:
f.write('\n'.join(synonyms_strong))
with open(synonyms_weak_path, 'w+', encoding='utf-8') as f:
f.write('\n'.join(synonyms_weak))
def init_results():
"""Read the filename for each file in the evaluation directory"""
for filename in os.listdir(FILE_DIR):
if not filename in results:
results[filename] = []
def evaluate(filenames):
models = dict()
for filename in filenames:
model = dict()
"""Compute Cosine similarity per each file and model"""
# step 0 : read the first line to get the number of words and the dimension
nb_line = 0
nb_dims = 0
with open(filename, encoding='utf-8') as f:
line = f.readline().split()
nb_line = int(line[0])
nb_dims = int(line[1])
mat = np.zeros((nb_line, nb_dims))
wordToNum = {}
count = 0
with open(filename, encoding='utf-8') as f:
f.readline() # skip first line because it does not contains a vector
for line in f:
line = line.split()
word, vals = line[0], list(map(float, line[1:]))
# if number of vals is different from nb_dims, bad vector, drop it
if len(vals) != nb_dims:
continue
mat[count] = np.array(vals)
wordToNum[word] = count
count += 1
model['mat'] = mat
model['wordToNum'] = wordToNum
models[filename] = model
# step 1 : iterate over each evaluation data file and compute cosine
for filename in results:
pairs_not_found, total_pairs = 0, 0
words_not_found, total_words = 0, 0
total_imprs = 0
imprs_count = 0
with open(os.path.join(FILE_DIR, filename), encoding='utf-8') as f:
for line in f:
w1, w2 = line.split()
w1, w2 = w1.lower(), w2.lower()
total_words += 2
total_pairs += 1
cosines = list()
for key in models.keys():
if not w1 in models[key]['wordToNum']:
words_not_found += 1
if not w2 in models[key]['wordToNum']:
words_not_found += 1
if not w1 in models[key]['wordToNum'] or not w2 in models[key]['wordToNum']:
pairs_not_found += 1
else:
v1, v2 = models[key]['mat'][models[key]['wordToNum'][w1]], models[key]['mat'][models[key]['wordToNum'][w2]]
cosine = cosineSim(v1, v2)
cosines.append(cosine)
#tanimoto = tanimotoSim(v1, v2)
#file_similarity.append(val)
#embedding_similarity.append(tanimoto)
if cosines:
total_imprs += int(cosines[1] >= cosines[0])
imprs_count += 1
missed_pairs[filename] = (pairs_not_found, total_pairs)
missed_words[filename] = (words_not_found, total_words)
imprs[filename] = (total_imprs, imprs_count)
def stats():
"""Compute statistics on results"""
title = "{}| {}| {} ".format("Filename".ljust(20), "Missed words/pairs".center(20), "Average improvement".rjust(16))
print(title)
print("="*len(title))
for filename in sorted(results.keys()):
# total amount of the improvement
total_imprs = str(round(imprs[filename][0] / imprs[filename][1] * 100, 2))
# ratio = number of missed / total
ratio_words = missed_words[filename][0] / missed_words[filename][1]
ratio_pairs = missed_pairs[filename][0] / missed_pairs[filename][1]
missed_infos = "{:.2f}% / {:.2f}%".format(
round(ratio_words*100, 2), round(ratio_pairs*100, 2))
print("{}| {}| {}%".format(
filename.ljust(20),
missed_infos.center(20),
total_imprs.rjust(16)))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Evaluate semantic similarities of word embeddings.",
)
parser.add_argument('-w2v', '--word2vec', help="""File containing Word2Vec model.""", required=True)
parser.add_argument('-d2v', '--dict2vec', help="""File containing Dict2Vec model.""", required=True)
args = parser.parse_args()
make_synonyms()
init_results()
evaluate([args.word2vec, args.dict2vec])
stats()