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
|
- # Copyright (c) 2017-present, Facebook, Inc.
- # All rights reserved.
- #
- # This source code is licensed under the license found in the LICENSE file in
- # the root directory of this source tree. An additional grant of patent rights
- # can be found in the PATENTS file in the same directory.
- from collections import Counter
- import os
- import torch
- class Dictionary(object):
- """A mapping from symbols to consecutive integers"""
- def __init__(self, pad='<pad>', eos='</s>', unk='<unk>'):
- self.unk_word, self.pad_word, self.eos_word = unk, pad, eos
- self.symbols = []
- self.count = []
- self.indices = {}
- # dictionary indexing starts at 1 for consistency with Lua
- self.add_symbol('<Lua heritage>')
- self.pad_index = self.add_symbol(pad)
- self.eos_index = self.add_symbol(eos)
- self.unk_index = self.add_symbol(unk)
- self.nspecial = len(self.symbols)
- def __eq__(self, other):
- return self.indices == other.indices
- def __getitem__(self, idx):
- if idx < len(self.symbols):
- return self.symbols[idx]
- return self.unk_word
- def __len__(self):
- """Returns the number of symbols in the dictionary"""
- return len(self.symbols)
- def index(self, sym):
- """Returns the index of the specified symbol"""
- if sym in self.indices:
- return self.indices[sym]
- return self.unk_index
- def string(self, tensor, bpe_symbol=None, escape_unk=False):
- """Helper for converting a tensor of token indices to a string.
- Can optionally remove BPE symbols or escape <unk> words.
- """
- if torch.is_tensor(tensor) and tensor.dim() == 2:
- return '\n'.join(self.string(t) for t in tensor)
- def token_string(i):
- if i == self.unk():
- return self.unk_string(escape_unk)
- else:
- return self[i]
- if bpe_symbol == 'sentencepiece':
- sent = ''.join(token_string(i) for i in tensor if i != self.eos())
- sent = sent.replace('\u2581', ' ').strip()
- else:
- sent = ' '.join(token_string(i) for i in tensor if i != self.eos())
- if bpe_symbol is not None and bpe_symbol != 'sentencepiece':
- sent = (sent + ' ').replace(bpe_symbol, '').rstrip()
- return sent
- def unk_string(self, escape=False):
- """Return unknown string, optionally escaped as: <<unk>>"""
- if escape:
- return '<{}>'.format(self.unk_word)
- else:
- return self.unk_word
- def add_symbol(self, word, n=1):
- """Adds a word to the dictionary"""
- if word in self.indices:
- idx = self.indices[word]
- self.count[idx] = self.count[idx] + n
- return idx
- else:
- idx = len(self.symbols)
- self.indices[word] = idx
- self.symbols.append(word)
- self.count.append(n)
- return idx
- def update(self, new_dict):
- """Updates counts from new dictionary."""
- for word in new_dict.symbols:
- idx2 = new_dict.indices[word]
- if word in self.indices:
- idx = self.indices[word]
- self.count[idx] = self.count[idx] + new_dict.count[idx2]
- else:
- idx = len(self.symbols)
- self.indices[word] = idx
- self.symbols.append(word)
- self.count.append(new_dict.count[idx2])
- def finalize(self, threshold=-1, nwords=-1, padding_factor=8):
- """Sort symbols by frequency in descending order, ignoring special ones.
- Args:
- - threshold defines the minimum word count
- - nwords defines the total number of words in the final dictionary,
- including special symbols
- - padding_factor can be used to pad the dictionary size to be a
- multiple of 8, which is important on some hardware (e.g., Nvidia
- Tensor Cores).
- """
- if nwords <= 0:
- nwords = len(self)
- new_indices = dict(zip(self.symbols[:self.nspecial], range(self.nspecial)))
- new_symbols = self.symbols[:self.nspecial]
- new_count = self.count[:self.nspecial]
- c = Counter(dict(zip(self.symbols[self.nspecial:], self.count[self.nspecial:])))
- for symbol, count in c.most_common(nwords - self.nspecial):
- if count >= threshold:
- new_indices[symbol] = len(new_symbols)
- new_symbols.append(symbol)
- new_count.append(count)
- else:
- break
- threshold_nwords = len(new_symbols)
- if padding_factor > 1:
- i = 0
- while threshold_nwords % padding_factor != 0:
- symbol = 'madeupword{:04d}'.format(i)
- new_indices[symbol] = len(new_symbols)
- new_symbols.append(symbol)
- new_count.append(0)
- i += 1
- threshold_nwords += 1
- assert len(new_symbols) % padding_factor == 0
- assert len(new_symbols) == len(new_indices)
- self.count = list(new_count)
- self.symbols = list(new_symbols)
- self.indices = new_indices
- def pad(self):
- """Helper to get index of pad symbol"""
- return self.pad_index
- def eos(self):
- """Helper to get index of end-of-sentence symbol"""
- return self.eos_index
- def unk(self):
- """Helper to get index of unk symbol"""
- return self.unk_index
- @classmethod
- def load(cls, f, ignore_utf_errors=False):
- """Loads the dictionary from a text file with the format:
- ```
- <symbol0> <count0>
- <symbol1> <count1>
- ...
- ```
- """
- if isinstance(f, str):
- try:
- if not ignore_utf_errors:
- with open(f, 'r', encoding='utf-8') as fd:
- return cls.load(fd)
- else:
- with open(f, 'r', encoding='utf-8', errors='ignore') as fd:
- return cls.load(fd)
- except FileNotFoundError as fnfe:
- raise fnfe
- except UnicodeError:
- raise Exception("Incorrect encoding detected in {}, please "
- "rebuild the dataset".format(f))
- d = cls()
- for line in f.readlines():
- idx = line.rfind(' ')
- if idx == -1:
- raise ValueError("Incorrect dictionary format, expected '<token> <cnt>'")
- word = line[:idx]
- count = int(line[idx+1:])
- d.indices[word] = len(d.symbols)
- d.symbols.append(word)
- d.count.append(count)
- return d
- def save(self, f):
- """Stores dictionary into a text file"""
- if isinstance(f, str):
- os.makedirs(os.path.dirname(f), exist_ok=True)
- with open(f, 'w', encoding='utf-8') as fd:
- return self.save(fd)
- for symbol, count in zip(self.symbols[self.nspecial:], self.count[self.nspecial:]):
- print('{} {}'.format(symbol, count), file=f)
- def dummy_sentence(self, length):
- t = torch.Tensor(length).uniform_(self.nspecial + 1, len(self)).long()
- t[-1] = self.eos()
- return t
- class TruncatedDictionary(object):
- def __init__(self, wrapped_dict, length):
- self.__class__ = type(
- wrapped_dict.__class__.__name__,
- (self.__class__, wrapped_dict.__class__),
- {}
- )
- self.__dict__ = wrapped_dict.__dict__
- self.wrapped_dict = wrapped_dict
- self.length = min(len(self.wrapped_dict), length)
- def __len__(self):
- return self.length
- def __getitem__(self, i):
- if i < self.length:
- return self.wrapped_dict[i]
- return self.wrapped_dict.unk()
|