-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_grabber_mini.py
128 lines (100 loc) · 3.72 KB
/
vector_grabber_mini.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
from wvlib import wvlib
import numpy
import random
from scipy.spatial.distance import cosine
def get_data_with_negs(limit=1000000, output=True):
#limit = 1000
examples, vocab_list, vecs = get_data(limit=limit)
#Let us create negative examples
n_examples = []
e_examples = []
objects = []
for e in examples:
if e[-1].startswith('X'):
n_examples.append(e)
objects.append(e[0][-1])
else:
e_examples.append(e)
i_examples = []
random.seed(1234)
random.shuffle(objects)
for e in n_examples:
original_object = e[0][2]
original_vector = vecs[original_object]
rnd_obj = random.choice(objects)
#import pdb;pdb.set_trace()
while cosine(vecs[rnd_obj], original_vector) < 0.6:
rnd_obj = random.choice(objects)
i_examples.append(((e[0][0], e[0][1], rnd_obj), 'IX'))
#Txt output it!
out = open('dataset_xm.txt', 'wt')
for l in [e_examples, n_examples, i_examples]:
for e in l:
try:
tokens = [vocab_list[e[0][0]], vocab_list[e[0][1]], vocab_list[e[0][2]], e[-1]]
out.write('\t'.join(tokens) + '\n')
except:
print l
pass#import pdb;pdb.set_trace()
out.close()
#Pickle it out!
import pickle
out = open('dataset_xm.list', 'wb')
pickle.dump([n_examples + e_examples + i_examples, vocab_list, vecs], out)
out.close()
def get_data(limit=100000):
#Let us say the point of this little program is to get the data,
#get the vectors and the create both embedding matrix
#and the data in nice indexes
#wv = wvlib.load("/usr/share/ParseBank/vector-space-models/FIN/w2v_pbv3_lm.rev01.bin",max_rank=1000000)
wv = wvlib.load("/home/ginter/w2v/pb34_lemma_200_v2.bin").normalize()#,max_rank=10000000000).normalize()
#wv.normalize()
#remember to normalize!
lines2 = open('./example_harvest/the_res', 'rt').readlines()[:50]
lines = open('test_sent_2.txt', 'rt').readlines()
lines += lines2
#Such a small vocab I can ignore this stuff: vocab_set = set()
vocab_list = []
vecs = []
examples = []
corrupt_examples = []
labels = []
incomplete = []
triplets = set()
for line in lines[1:]:
if len(line) > 4:
exp = line.strip()
indexes = []
for w in exp.split()[:-1]:
success = True
if w not in vocab_list:
vocab_list.append(w)
try:
vecs.append(wv.word_to_vector(w.decode('utf8')))
except:
vecs.append(numpy.zeros(200,))
#print w
incomplete.append(w)
indexes.append(vocab_list.index(w))
destination_ok = True
for w in exp.split()[:-1]:
if w in incomplete:
#print '!', w
destination_ok = False
if destination_ok:
try:
if '.'.join([str(indexes[0]), str(indexes[1]), str(indexes[2])]) not in triplets:
examples.append((indexes, exp.split()[-1]))
print len(examples)
triplets.add('.'.join([str(indexes[0]), str(indexes[1]), str(indexes[2])]))
if len(examples) > limit:
break
except:
print indexes
else:
corrupt_examples.append((indexes, exp.split()[-1]))
return examples, vocab_list, vecs
#print len(corrupt_examples)
#import pdb;pdb.set_trace()
if __name__ == "__main__":
get_data_with_negs()