-
Notifications
You must be signed in to change notification settings - Fork 129
/
myio.py
184 lines (166 loc) · 6.3 KB
/
myio.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
import sys
import gzip
import random
from collections import Counter
from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
import theano
from nn import EmbeddingLayer
def say(s, stream=sys.stdout):
stream.write(s)
stream.flush()
def read_corpus(path):
empty_cnt = 0
raw_corpus = {}
fopen = gzip.open if path.endswith(".gz") else open
with fopen(path) as fin:
for line in fin:
id, title, body = line.split("\t")
if len(title) == 0:
print id
empty_cnt += 1
continue
title = title.strip().split()
body = body.strip().split()
raw_corpus[id] = (title, body)
say("{} empty titles ignored.\n".format(empty_cnt))
return raw_corpus
def create_embedding_layer(raw_corpus, n_d, embs=None, \
cut_off=2, unk="<unk>", padding="<padding>", fix_init_embs=True):
cnt = Counter(w for id, pair in raw_corpus.iteritems() \
for x in pair for w in x)
cnt[unk] = cut_off + 1
cnt[padding] = cut_off + 1
embedding_layer = EmbeddingLayer(
n_d = n_d,
#vocab = (w for w,c in cnt.iteritems() if c > cut_off),
vocab = [ unk, padding ],
embs = embs,
fix_init_embs = fix_init_embs
)
return embedding_layer
def create_idf_weights(corpus_path, embedding_layer):
vectorizer = TfidfVectorizer(min_df=1, ngram_range=(1,1), binary=False)
lst = [ ]
fopen = gzip.open if corpus_path.endswith(".gz") else open
with fopen(corpus_path) as fin:
for line in fin:
id, title, body = line.split("\t")
lst.append(title)
lst.append(body)
vectorizer.fit_transform(lst)
idfs = vectorizer.idf_
avg_idf = sum(idfs)/(len(idfs)+0.0)/4.0
weights = np.array([ avg_idf for i in xrange(embedding_layer.n_V) ],
dtype = theano.config.floatX)
vocab_map = embedding_layer.vocab_map
for word, idf_value in zip(vectorizer.get_feature_names(), idfs):
id = vocab_map.get(word, -1)
if id != -1:
weights[id] = idf_value
return theano.shared(weights, name="word_weights")
def map_corpus(raw_corpus, embedding_layer, max_len=100):
ids_corpus = { }
for id, pair in raw_corpus.iteritems():
item = (embedding_layer.map_to_ids(pair[0], filter_oov=True),
embedding_layer.map_to_ids(pair[1], filter_oov=True)[:max_len])
#if len(item[0]) == 0:
# say("empty title after mapping to IDs. Doc No.{}\n".format(id))
# continue
ids_corpus[id] = item
return ids_corpus
def read_annotations(path, K_neg=20, prune_pos_cnt=10):
lst = [ ]
with open(path) as fin:
for line in fin:
parts = line.split("\t")
pid, pos, neg = parts[:3]
pos = pos.split()
neg = neg.split()
if len(pos) == 0 or (len(pos) > prune_pos_cnt and prune_pos_cnt != -1): continue
if K_neg != -1:
random.shuffle(neg)
neg = neg[:K_neg]
s = set()
qids = [ ]
qlabels = [ ]
for q in neg:
if q not in s:
qids.append(q)
qlabels.append(0 if q not in pos else 1)
s.add(q)
for q in pos:
if q not in s:
qids.append(q)
qlabels.append(1)
s.add(q)
lst.append((pid, qids, qlabels))
return lst
def create_batches(ids_corpus, data, batch_size, padding_id, perm=None, pad_left=True):
if perm is None:
perm = range(len(data))
random.shuffle(perm)
N = len(data)
cnt = 0
pid2id = {}
titles = [ ]
bodies = [ ]
triples = [ ]
batches = [ ]
for u in xrange(N):
i = perm[u]
pid, qids, qlabels = data[i]
if pid not in ids_corpus: continue
cnt += 1
for id in [pid] + qids:
if id not in pid2id:
if id not in ids_corpus: continue
pid2id[id] = len(titles)
t, b = ids_corpus[id]
titles.append(t)
bodies.append(b)
pid = pid2id[pid]
pos = [ pid2id[q] for q, l in zip(qids, qlabels) if l == 1 and q in pid2id ]
neg = [ pid2id[q] for q, l in zip(qids, qlabels) if l == 0 and q in pid2id ]
triples += [ [pid,x]+neg for x in pos ]
if cnt == batch_size or u == N-1:
titles, bodies = create_one_batch(titles, bodies, padding_id, pad_left)
triples = create_hinge_batch(triples)
batches.append((titles, bodies, triples))
titles = [ ]
bodies = [ ]
triples = [ ]
pid2id = {}
cnt = 0
return batches
def create_eval_batches(ids_corpus, data, padding_id, pad_left):
lst = [ ]
for pid, qids, qlabels in data:
titles = [ ]
bodies = [ ]
for id in [pid]+qids:
t, b = ids_corpus[id]
titles.append(t)
bodies.append(b)
titles, bodies = create_one_batch(titles, bodies, padding_id, pad_left)
lst.append((titles, bodies, np.array(qlabels, dtype="int32")))
return lst
def create_one_batch(titles, bodies, padding_id, pad_left):
max_title_len = max(1, max(len(x) for x in titles))
max_body_len = max(1, max(len(x) for x in bodies))
if pad_left:
titles = np.column_stack([ np.pad(x,(max_title_len-len(x),0),'constant',
constant_values=padding_id) for x in titles])
bodies = np.column_stack([ np.pad(x,(max_body_len-len(x),0),'constant',
constant_values=padding_id) for x in bodies])
else:
titles = np.column_stack([ np.pad(x,(0,max_title_len-len(x)),'constant',
constant_values=padding_id) for x in titles])
bodies = np.column_stack([ np.pad(x,(0,max_body_len-len(x)),'constant',
constant_values=padding_id) for x in bodies])
return titles, bodies
def create_hinge_batch(triples):
max_len = max(len(x) for x in triples)
triples = np.vstack([ np.pad(x,(0,max_len-len(x)),'edge')
for x in triples ]).astype('int32')
return triples