-
Notifications
You must be signed in to change notification settings - Fork 1
/
vocab.py
52 lines (44 loc) · 1.63 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
PAD, UNK = '<PAD>', '<UNK>'
STR, SEP, END = '<s>', '<sep>', '</s>'
T2C, C2T = 'T2C', 'C2T'
class Vocab(object):
def __init__(self, vocab_file_name, min_occur_cnt, specials=None):
idx2token = [PAD, UNK] + (specials if specials is not None else [])
self._priority = dict()
num_tot_tokens = 0
num_vocab_tokens = 0
for line in open(vocab_file_name, 'r', encoding='utf-8').readlines():
try:
token, cnt = line.strip().split('\t')
cnt = int(cnt)
num_tot_tokens += cnt
except:
print(line)
if cnt >= min_occur_cnt:
idx2token.append(token)
num_vocab_tokens += cnt
self._priority[token] = int(cnt)
self.coverage = num_vocab_tokens/num_tot_tokens
self._token2idx = dict(zip(idx2token, range(len(idx2token))))
self._idx2token = idx2token
self._padding_idx = self._token2idx[PAD]
self._unk_idx = self._token2idx[UNK]
def priority(self, x):
return self._priority.get(x, 0)
@property
def size(self):
return len(self._idx2token)
@property
def unk_idx(self):
return self._unk_idx
@property
def padding_idx(self):
return self._padding_idx
def idx2token(self, x):
if isinstance(x, list):
return [self.idx2token(i) for i in x]
return self._idx2token[x]
def token2idx(self, x):
if isinstance(x, list):
return [self.token2idx(i) for i in x]
return self._token2idx.get(x, self.unk_idx)