forked from tdozat/Parser-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vocab.py
343 lines (296 loc) · 11.3 KB
/
vocab.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Copyright 2016 Timothy Dozat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
from collections import Counter
import numpy as np
import tensorflow as tf
from configurable import Configurable
# TODO MetaVocab?
#***************************************************************
class Vocab(Configurable):
""""""
SPECIAL_TOKENS = ('<PAD>', '<ROOT>', '<UNK>')
START_IDX = len(SPECIAL_TOKENS)
PAD, ROOT, UNK = range(START_IDX)
#=============================================================
def __init__(self, vocab_file, conll_idx, *args, **kwargs):
""""""
self._vocab_file = vocab_file
self._conll_idx = conll_idx
load_embed_file = kwargs.pop('load_embed_file', False)
global_step = kwargs.pop('global_step', None)
cased = kwargs.pop('cased', None)
super(Vocab, self).__init__(*args, **kwargs)
if cased is None:
self._cased = super(Vocab, self).cased
else:
self._cased = cased
if self.name == 'Tags':
self.SPECIAL_TOKENS = ('PAD', 'ROOT', 'UNK')
elif self.name == 'Rels':
self.SPECIAL_TOKENS = ('pad', 'root', 'unk')
self._counts = Counter()
self._str2idx = dict(zip(self.SPECIAL_TOKENS, range(Vocab.START_IDX)))
self._idx2str = dict(zip(range(Vocab.START_IDX), self.SPECIAL_TOKENS))
self._str2embed = {}
self._embed2str = {}
self.trainable_embeddings = None
self.pretrained_embeddings = None
if os.path.isfile(self.vocab_file):
self.load_vocab_file()
else:
self.add_train_file()
self.save_vocab_file()
if load_embed_file:
self.load_embed_file()
self._finalize()
if global_step is not None:
self._global_sigmoid = 1-tf.nn.sigmoid(3*(2*global_step/(self.train_iters-1)-1))
else:
self._global_sigmoid = 1
return
#=============================================================
def add(self, word, count=1):
""""""
if not self.cased:
word = word.lower()
self._counts[word] += int(count)
return
#=============================================================
def update(self, iterable):
""""""
for elt in iterable:
if isinstance(elt, basestring):
self.add(elt)
elif isinstance(iterable, dict):
self.add(elt, iterable[elt])
elif isinstance(elt, (tuple, list)) and len(elt) == 2:
self.add(*elt)
else:
raise ValueError('WTF did you just pass to Vocab.update?')
return
#=============================================================
def index_vocab(self):
""""""
cur_idx = Vocab.START_IDX
buff = []
for word_and_count in self._counts.most_common():
if (not buff) or buff[-1][1] == word_and_count[1]:
buff.append(word_and_count)
else:
buff.sort()
for word, count in buff:
if count >= self.min_occur_count and word not in self._str2idx:
self._str2idx[word] = cur_idx
self._idx2str[cur_idx] = word
cur_idx += 1
buff = [word_and_count]
buff.sort()
for word, count in buff:
if count >= self.min_occur_count and word not in self._str2idx:
self._str2idx[word] = cur_idx
self._idx2str[cur_idx] = word
cur_idx += 1
return
#=============================================================
def add_train_file(self):
""""""
with open(self.train_file) as f:
buff = []
for line_num, line in enumerate(f):
line = line.strip().split()
if line:
if len(line) == 10:
if hasattr(self.conll_idx, '__iter__'):
for idx in self.conll_idx:
self.add(line[idx])
else:
self.add(line[self.conll_idx])
else:
raise ValueError('The training file is misformatted at line %d' % (line_num+1))
self.index_vocab()
return
#=============================================================
def load_embed_file(self):
""""""
self._str2embed = dict(zip(self.SPECIAL_TOKENS, range(Vocab.START_IDX)))
self._embed2str = dict(zip(range(Vocab.START_IDX), self.SPECIAL_TOKENS))
embeds = []
with open(self.embed_file) as f:
cur_idx = Vocab.START_IDX
for line_num, line in enumerate(f):
line = line.strip().split()
if line:
try:
self._str2embed[line[0]] = cur_idx
self._embed2str[cur_idx] = line[0]
embeds.append(line[1:])
cur_idx += 1
except:
raise ValueError('The embedding file is misformatted at line %d' % (line_num+1))
self.pretrained_embeddings = np.array(embeds, dtype=np.float32)
self.pretrained_embeddings = np.pad(self.pretrained_embeddings, ((self.START_IDX, 0), (0, 0)), 'constant')
if os.path.isfile(self.embed_aux_file):
with open(self.embed_aux_file) as f:
for line in f:
line = line.strip().split()
if line[0] == self.SPECIAL_TOKENS[0]:
self.pretrained_embeddings[0] = np.array(line[1:], dtype=np.float32)
elif line[0] == self.SPECIAL_TOKENS[1]:
self.pretrained_embeddings[1] = np.array(line[1:], dtype=np.float32)
elif line[0] == self.SPECIAL_TOKENS[2]:
self.pretrained_embeddings[2] = np.array(line[1:], dtype=np.float32)
return
#=============================================================
def load_vocab_file(self):
""""""
with open(self.vocab_file) as f:
for line_num, line in enumerate(f):
line = line.strip().split()
if line:
if len(line) == 1:
line.insert(0, '')
if len(line) == 2:
self.add(*line)
else:
raise ValueError('The vocab file is misformatted at line %d' % (line_num+1))
self.index_vocab()
#=============================================================
def save_vocab_file(self):
""""""
with open(self.vocab_file, 'w') as f:
for word_and_count in self._counts.most_common():
f.write('%s\t%d\n' % (word_and_count))
return
#=============================================================
@staticmethod
def idxs2str(indices):
""""""
shape = Configurable.tupleshape(indices)
if len(shape) == 2:
return ' '.join(':'.join(str(subidx) for subidx in index) if index[0] == Vocab.UNK else str(index[0]) for index in indices)
elif len(shape) == 1:
return ' '.join(str(index) for index in indices)
elif len(shape) == 0:
return ''
else:
raise ValueError('Indices should have len(shape) 1 or 2, not %d' % len(shape))
return
#=============================================================
def get_embed(self, key):
""""""
return self._embed2str[key]
#=============================================================
def _finalize(self):
""""""
if self.pretrained_embeddings is None:
initializer = tf.random_normal_initializer()
embed_size = self.embed_size
else:
initializer = tf.zeros_initializer
embed_size = self.pretrained_embeddings.shape[1]
with tf.device('/cpu:0'):
with tf.variable_scope(self.name):
self.trainable_embeddings = tf.get_variable('Trainable', shape=(len(self._str2idx), embed_size), initializer=initializer)
if self.pretrained_embeddings is not None:
self.pretrained_embeddings /= np.std(self.pretrained_embeddings)
self.pretrained_embeddings = tf.Variable(self.pretrained_embeddings, trainable=False, name='Pretrained')
return
#=============================================================
def embedding_lookup(self, inputs, pret_inputs=None, moving_params=None):
""""""
if moving_params is not None:
trainable_embeddings = moving_params.average(self.trainable_embeddings)
else:
trainable_embeddings = self.trainable_embeddings
embed_input = tf.nn.embedding_lookup(trainable_embeddings, inputs)
if moving_params is None:
tf.add_to_collection('Weights', embed_input)
if self.pretrained_embeddings is not None and pret_inputs is not None:
return embed_input, tf.nn.embedding_lookup(self.pretrained_embeddings, pret_inputs)
else:
return embed_input
#=============================================================
def weighted_average(self, inputs, moving_params=None):
""""""
input_shape = tf.shape(inputs)
batch_size = input_shape[0]
bucket_size = input_shape[1]
input_size = len(self)
if moving_params is not None:
trainable_embeddings = moving_params.average(self.trainable_embeddings)
else:
trainable_embeddings = self.trainable_embeddings
embed_input = tf.matmul(tf.reshape(inputs, [-1, input_size]),
trainable_embeddings)
embed_input = tf.reshape(embed_input, tf.pack([batch_size, bucket_size, self.embed_size]))
embed_input.set_shape([tf.Dimension(None), tf.Dimension(None), tf.Dimension(self.embed_size)])
if moving_params is None:
tf.add_to_collection('Weights', embed_input)
return embed_input
#=============================================================
@property
def vocab_file(self):
return self._vocab_file
@property
def cased(self):
return self._cased
@property
def conll_idx(self):
return self._conll_idx
@property
def global_sigmoid(self):
return self._global_sigmoid
#=============================================================
def keys(self):
return self._str2idx.keys()
def values(self):
return self._str2idx.values()
def iteritems(self):
return self._str2idx.iteritems()
#=============================================================
def __getitem__(self, key):
if isinstance(key, basestring):
if not self.cased:
key = key.lower()
if self._str2embed:
return (self._str2idx.get(key, Vocab.UNK), self._str2embed.get(key.lower(), Vocab.UNK))
else:
return (self._str2idx.get(key, Vocab.UNK),)
elif isinstance(key, (int, long, np.int32, np.int64)):
return self._idx2str.get(key, self.SPECIAL_TOKENS[Vocab.UNK])
elif hasattr(key, '__iter__'):
return tuple(self[k] for k in key)
else:
raise ValueError('key to Vocab.__getitem__ must be (iterable of) string or integer')
return
def __contains__(self, key):
if isinstance(key, basestring):
if not self.cased:
key = key.lower()
return key in self._str2idx
elif isinstance(key, (int, long)):
return key in self._idx2str
else:
raise ValueError('key to Vocab.__contains__ must be string or integer')
return
def __len__(self):
return len(self._str2idx)
def __iter__(self):
return (key for key in self._str2idx)