-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathencoder.py
More file actions
282 lines (236 loc) · 11.3 KB
/
Copy pathencoder.py
File metadata and controls
282 lines (236 loc) · 11.3 KB
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
# coding=utf-8
""" An encoder which learns byte pair encodings for white-space separated text. Can tokenize, encode, and decode. """
from collections import Counter
try:
from typing import Dict, Iterable, Callable, List, Any, Iterator
except ImportError:
pass
from tok import word_tokenize
from tqdm import tqdm
import toolz
import json
DEFAULT_EOW = '__eow'
DEFAULT_SOW = '__sow'
DEFAULT_UNK = '__unk'
DEFAULT_PAD = '__pad'
class Encoder:
""" Encodes white-space separated text using byte-pair encoding. See https://arxiv.org/abs/1508.07909 for details.
"""
def __init__(self, vocab_size=8192, pct_bpe=0.2, word_tokenizer=None,
silent=True, ngram_min=2, ngram_max=2, required_tokens=None, strict=False,
EOW=DEFAULT_EOW, SOW=DEFAULT_SOW, UNK=DEFAULT_UNK, PAD=DEFAULT_PAD):
if vocab_size < 1:
raise ValueError('vocab size must be greater than 0.')
self.EOW = EOW
self.SOW = SOW
self.eow_len = len(EOW)
self.sow_len = len(SOW)
self.UNK = UNK
self.PAD = PAD
self.required_tokens = list(set(required_tokens or []).union({self.UNK, self.PAD}))
self.vocab_size = vocab_size
self.pct_bpe = pct_bpe
self.word_vocab_size = max([int(vocab_size * (1 - pct_bpe)), len(self.required_tokens or [])])
self.bpe_vocab_size = vocab_size - self.word_vocab_size
self.word_tokenizer = word_tokenizer if word_tokenizer is not None else word_tokenize
self.custom_tokenizer = word_tokenizer is not None
self.word_vocab = {} # type: Dict[str, int]
self.bpe_vocab = {} # type: Dict[str, int]
self.inverse_word_vocab = {} # type: Dict[int, str]
self.inverse_bpe_vocab = {} # type: Dict[int, str]
self._progress_bar = iter if silent else tqdm
self.ngram_min = ngram_min
self.ngram_max = ngram_max
self.strict = strict
def mute(self):
""" Turn on silent mode """
self._progress_bar = iter
def unmute(self):
""" Turn off silent mode """
self._progress_bar = tqdm
def byte_pair_counts(self, words):
# type: (Encoder, Iterable[str]) -> Iterable[Counter]
""" Counts space separated token character pairs:
[('T h i s </w>', 4}] -> {'Th': 4, 'hi': 4, 'is': 4}
"""
for token, count in self._progress_bar(self.count_tokens(words).items()):
bp_counts = Counter() # type: Counter
for ngram in token.split(' '):
bp_counts[ngram] += count
for ngram_size in range(self.ngram_min, min([self.ngram_max, len(token)]) + 1):
ngrams = [''.join(ngram) for ngram in toolz.sliding_window(ngram_size, token.split(' '))]
for ngram in ngrams:
bp_counts[''.join(ngram)] += count
yield bp_counts
def count_tokens(self, words):
# type: (Encoder, Iterable[str]) -> Dict[str, int]
""" Count tokens into a BPE vocab """
token_counts = Counter(self._progress_bar(words))
return {' '.join(token): count for token, count in token_counts.items()}
def learn_word_vocab(self, sentences):
# type: (Encoder, Iterable[str]) -> Dict[str, int]
""" Build vocab from self.word_vocab_size most common tokens in provided sentences """
word_counts = Counter(word for word in toolz.concat(map(self.word_tokenizer, sentences)))
for token in set(self.required_tokens or []):
word_counts[token] = int(2**63)
sorted_word_counts = sorted(word_counts.items(), key=lambda p: -p[1])
return {word: idx for idx, (word, count) in enumerate(sorted_word_counts[:self.word_vocab_size])}
def learn_bpe_vocab(self, words):
# type: (Encoder, Iterable[str]) -> Dict[str, int]
""" Learns a vocab of byte pair encodings """
vocab = Counter() # type: Counter
for token in {self.SOW, self.EOW}:
vocab[token] = int(2**63)
for idx, byte_pair_count in enumerate(self.byte_pair_counts(words)):
for byte_pair, count in byte_pair_count.items():
vocab[byte_pair] += count
if (idx + 1) % 10000 == 0:
self.trim_vocab(10 * self.bpe_vocab_size, vocab)
sorted_bpe_counts = sorted(vocab.items(), key=lambda p: -p[1])[:self.bpe_vocab_size]
return {bp: idx + self.word_vocab_size for idx, (bp, count) in enumerate(sorted_bpe_counts)}
def fit(self, text):
# type: (Encoder, Iterable[str]) -> None
""" Learn vocab from text. """
_text = [l.lower().strip() for l in text]
# First, learn word vocab
self.word_vocab = self.learn_word_vocab(_text)
remaining_words = [word for word in toolz.concat(map(self.word_tokenizer, _text))
if word not in self.word_vocab]
self.bpe_vocab = self.learn_bpe_vocab(remaining_words)
self.inverse_word_vocab = {idx: token for token, idx in self.word_vocab.items()}
self.inverse_bpe_vocab = {idx: token for token, idx in self.bpe_vocab.items()}
@staticmethod
def trim_vocab(n, vocab):
# type: (int, Dict[str, int]) -> None
""" Deletes all pairs below 10 * vocab size to prevent memory problems """
pair_counts = sorted(vocab.items(), key=lambda p: -p[1])
pairs_to_trim = [pair for pair, count in pair_counts[n:]]
for pair in pairs_to_trim:
del vocab[pair]
def subword_tokenize(self, word):
# type: (Encoder, str) -> List[str]
""" Tokenizes inside an unknown token using BPE """
end_idx = min([len(word), self.ngram_max])
sw_tokens = [self.SOW]
start_idx = 0
while start_idx < len(word):
subword = word[start_idx:end_idx]
if subword in self.bpe_vocab:
sw_tokens.append(subword)
start_idx = end_idx
end_idx = min([len(word), start_idx + self.ngram_max])
elif len(subword) == 1:
sw_tokens.append(self.UNK)
start_idx = end_idx
end_idx = min([len(word), start_idx + self.ngram_max])
else:
end_idx -= 1
sw_tokens.append(self.EOW)
return sw_tokens
def tokenize(self, sentence):
# type: (Encoder, str) -> List[str]
""" Split a sentence into word and subword tokens """
word_tokens = self.word_tokenizer(sentence.lower().strip())
tokens = []
for word_token in word_tokens:
if word_token in self.word_vocab:
tokens.append(word_token)
else:
tokens.extend(self.subword_tokenize(word_token))
return tokens
def transform(self, sentences, reverse=False, fixed_length=None):
# type: (Encoder, Iterable[str], bool, int) -> Iterable[List[int]]
""" Turns space separated tokens into vocab idxs """
direction = -1 if reverse else 1
for sentence in self._progress_bar(sentences):
encoded = []
tokens = list(self.tokenize(sentence.lower().strip()))
for token in tokens:
if token in self.word_vocab:
encoded.append(self.word_vocab[token])
elif token in self.bpe_vocab:
encoded.append(self.bpe_vocab[token])
else:
encoded.append(self.word_vocab[self.UNK])
if fixed_length is not None:
encoded = encoded[:fixed_length]
while len(encoded) < fixed_length:
encoded.append(self.word_vocab[self.PAD])
yield encoded[::direction]
def inverse_transform(self, rows):
# type: (Encoder, Iterable[List[int]]) -> Iterator[str]
""" Turns token indexes back into space-joined text. """
for row in rows:
words = []
rebuilding_word = False
current_word = ''
for idx in row:
if self.inverse_bpe_vocab.get(idx) == self.SOW:
if rebuilding_word and self.strict:
raise ValueError('Encountered second SOW token before EOW.')
rebuilding_word = True
elif self.inverse_bpe_vocab.get(idx) == self.EOW:
if not rebuilding_word and self.strict:
raise ValueError('Encountered EOW without matching SOW.')
rebuilding_word = False
words.append(current_word)
current_word = ''
elif rebuilding_word and (idx in self.inverse_bpe_vocab):
current_word += self.inverse_bpe_vocab[idx]
elif rebuilding_word and (idx in self.inverse_word_vocab):
current_word += self.inverse_word_vocab[idx]
elif idx in self.inverse_word_vocab:
words.append(self.inverse_word_vocab[idx])
elif idx in self.inverse_bpe_vocab:
if self.strict:
raise ValueError("Found BPE index {} when not rebuilding word!".format(idx))
else:
words.append(self.inverse_bpe_vocab[idx])
else:
raise ValueError("Got index {} that was not in word or BPE vocabs!".format(idx))
yield ' '.join(w for w in words if w != '')
def vocabs_to_dict(self, dont_warn=False):
# type: (Encoder, bool) -> Dict[str, Dict[str, int]]
""" Turns vocab into dict that is json-serializeable """
if self.custom_tokenizer and not dont_warn:
print("WARNING! You've specified a non-default tokenizer. You'll need to reassign it when you load the "
"model!")
return {
'byte_pairs': self.bpe_vocab,
'words': self.word_vocab,
'kwargs': {
'vocab_size': self.vocab_size,
'pct_bpe': self.pct_bpe,
'silent': self._progress_bar is iter,
'ngram_min': self.ngram_min,
'ngram_max': self.ngram_max,
'required_tokens': self.required_tokens,
'strict': self.strict,
'EOW': self.EOW,
'SOW': self.SOW,
'UNK': self.UNK,
'PAD': self.PAD,
}
}
def save(self, outpath, dont_warn=False):
# type: (Encoder, str, bool) -> None
""" Serializes and saves encoder to provided path """
with open(outpath, 'w') as outfile:
json.dump(self.vocabs_to_dict(dont_warn), outfile)
@classmethod
def from_dict(cls, vocabs):
# type: (Any, Dict[str, Dict[str, int]]) -> Encoder
""" Load encoder from dict produced with vocabs_to_dict """
encoder = Encoder(**vocabs['kwargs'])
encoder.word_vocab = vocabs['words']
encoder.bpe_vocab = vocabs['byte_pairs']
encoder.inverse_bpe_vocab = {v: k for k, v in encoder.bpe_vocab.items()}
encoder.inverse_word_vocab = {v: k for k, v in encoder.word_vocab.items()}
return encoder
@classmethod
def load(cls, in_path):
# type: (Any, str) -> Encoder
""" Loads an encoder from path saved with save """
with open(in_path) as infile:
obj = json.load(infile)
return cls.from_dict(obj)