-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathentity_vectors.py
executable file
·213 lines (163 loc) · 6.67 KB
/
entity_vectors.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
#!/usr/bin/env python
import os
import sys
import logging
import time
import re
from collections import defaultdict
from heapq import nlargest
import cPickle as pickle
from itertools import imap, izip, islice
import random
import urllib
import unicodedata
from multiprocessing.pool import ThreadPool
from ext import baker
import numpy as np
from utils import load_word2vec_model
from entity_models import EntityModel, EntityModelLR, EntityModelCentroid
WORDS_RE = re.compile('[a-zA-Z0-9]+')
CAMEL1_RE = re.compile(r'([A-Z0-9])([A-Z0-9])([a-z])')
CAMEL2_RE = re.compile(r'([a-z])([A-Z0-9])')
def extract_words(line, with_title_words=True):
fields = line.rstrip('\n').split('\t')
title = fields[0]
text = ''
if len(fields) > 1:
text = fields[1]
words = text.lower().split()
if with_title_words:
# extract words from the title itself
decoded_title = urllib.unquote(title).lower()
decoded_title = CAMEL1_RE.sub(r'\1 \2\3', decoded_title)
decoded_title = CAMEL2_RE.sub(r'\1 \2', decoded_title)
words += WORDS_RE.findall(decoded_title)
return title, words
def read_entity_word_seqs(filename, model, min_words=1):
with open(filename) as fin:
for line_idx, line in enumerate(fin):
if line_idx % 10000 == 0:
logging.info('Read %d lines', line_idx)
try:
entity, words = extract_words(line)
except Exception, e:
logging.info('Error reading line %r: %s', line, e)
continue
word_idxs = []
for w in words:
wd = model.vocab.get(w)
if wd is not None:
word_idxs.append(wd.index)
if len(word_idxs) < min_words:
continue
yield entity, word_idxs
@baker.command
def train(mode, model_file, descriptions_file, output_file=None,
neg_words_mult=2., lbda=50, min_words=1):
model = load_word2vec_model(model_file, mmap='r')
if mode == 'centroid':
entity_model = EntityModelCentroid()
elif mode == 'lr':
bins = np.cumsum([model.vocab[word].count
for word in model.index2word])
entity_model = EntityModelLR(bins, neg_words_mult, lbda)
else:
raise Exception('unsupported mode %s' % mode)
entity_model.train(model,
read_entity_word_seqs(descriptions_file, model, min_words))
if output_file is not None:
entity_model.save(output_file)
@baker.command
def train_eval(mode, model_file, descriptions_file,
neg_words_mult=2., lbda=50, min_words=50,
eval_lines=5000, eval_words=10):
model = load_word2vec_model(model_file, mmap='r')
if mode == 'centroid':
entity_model = EntityModelCentroid()
elif mode == 'lr':
bins = np.cumsum([model.vocab[word].count
for word in model.index2word])
entity_model = EntityModelLR(bins, neg_words_mult, lbda)
else:
raise Exception('unsupported mode %s' % mode)
rng = random.Random(1729)
eval_items = []
def sampled_word_seqs():
for i, (entity, t, word_idxs) in \
enumerate(read_entity_word_seqs(descriptions_file, model, min_words)):
rng.shuffle(word_idxs)
if i < eval_lines:
eval_items.append((entity, word_idxs[:eval_words], len(word_idxs)))
yield entity, t, word_idxs[eval_words:]
entity_model.train(model, sampled_word_seqs())
evaluate_retrieval(model, entity_model, eval_items)
def evaluate_retrieval(model, entity_model, eval_items):
pool = ThreadPool()
def entity_rank((entity, word_idxs, doc_len)):
if word_idxs:
entity_id = entity_model.entities[entity]
scores = entity_model.score(model,
entity_model.vectors,
word_idxs)
rank = np.sum(scores >= scores[entity_id])
else:
rank = entity_vecs.shape[0]
return int(np.log2(doc_len)), np.log2(rank)
ranks = defaultdict(list)
for size, rank in pool.imap(entity_rank, eval_items):
ranks[size].append(rank)
sorted_ranks = sorted(ranks.iteritems())
logging.info('%s overall score: %.3f by size: %s',
type(entity_model).__name__,
np.mean(np.hstack(ranks.values())),
' '.join('%d: %.3f' % (k, np.mean(v)) for k, v in sorted_ranks))
def parse_query(norm_entities, context):
words = []
to_match = []
for word in context.lower().split():
if word.startswith('+'):
word = word[1:]
to_match.append(word)
words.append(word)
matches = [entity for norm_entity, entity in norm_entities
if all(word in norm_entity for word in to_match)]
logging.info('%d matching entities', len(matches))
return words, matches
def top_entities(model, entity_model, entities, words, k=30):
word_idxs = [wd.index for wd in (model.vocab.get(word) for word in words)
if wd is not None]
if not word_idxs or not entities:
return []
entities_idxs = [entity_model.entities[e] for e in entities]
tick = time.time()
scores = entity_model.score(model,
entity_model.vectors[entities_idxs],
word_idxs)
top = nlargest(k, izip(scores, entities))
logging.info('%s scoring time: %.1f ms',
type(entity_model).__name__,
(time.time() - tick) * 1000)
return top
@baker.command
def eval(model_file, lr_entity_file, centroid_entity_file):
import readline
readline.parse_and_bind('set editing-mode emacs')
model = load_word2vec_model(model_file, mmap='r')
lr_entity_model = EntityModel.load(lr_entity_file, mmap='r')
centroid_entity_model = EntityModel.load(centroid_entity_file, mmap='r')
norm_entities = [(entity.lower(), entity) for entity in lr_entity_model.entities]
while True:
try:
line = raw_input('> ').strip()
except EOFError:
break
words, entities = parse_query(norm_entities, line)
lr_top = top_entities(model, lr_entity_model, entities, words)
centroid_top = top_entities(model, centroid_entity_model, entities, words)
for (lr_score, lr_ent), (centroid_score, centroid_ent) in zip(lr_top, centroid_top):
print '%-50s%10.3f | %-50s%10.3f' % (lr_ent, lr_score, centroid_ent, centroid_score)
if __name__ == '__main__':
np.random.seed(1729)
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s',
level=logging.INFO)
baker.run()