forked from facebookresearch/fastText
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_unsupervised.py
56 lines (45 loc) · 1.58 KB
/
train_unsupervised.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
#!/usr/bin/env python
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division, absolute_import, print_function
from fastText import train_unsupervised
import numpy as np
import os
from scipy import stats
# Because of fasttext we don't need to account for OOV
def compute_similarity(data_path):
def similarity(v1, v2):
n1 = np.linalg.norm(v1)
n2 = np.linalg.norm(v2)
return np.dot(v1, v2) / n1 / n2
mysim = []
gold = []
with open(data_path, 'rb') as fin:
for line in fin:
tline = line.split()
word1 = tline[0].lower()
word2 = tline[1].lower()
v1 = model.get_word_vector(word1)
v2 = model.get_word_vector(word2)
d = similarity(v1, v2)
mysim.append(d)
gold.append(float(tline[2]))
corr = stats.spearmanr(mysim, gold)
dataset = os.path.basename(data_path)
correlation = corr[0] * 100
return dataset, correlation, 0
if __name__ == "__main__":
model = train_unsupervised(
input=os.path.join(os.getenv("DATADIR", ''), 'fil9'),
model='skipgram',
)
model.save_model("fil9.bin")
dataset, corr, oov = compute_similarity('rw.txt')
print("{0:20s}: {1:2.0f} (OOV: {2:2.0f}%)".format(dataset, corr, 0))