-
Notifications
You must be signed in to change notification settings - Fork 10
/
vocab.py
67 lines (54 loc) · 1.85 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
# reference: vocab object from harvardnlp/opennmt-py
class Vocab(object):
def __init__(self, filename=None, lower=False, unkWord='unk'):
self.idxToLabel = {}
self.labelToIdx = {}
self.lower = lower
if filename is not None:
self.loadFile(filename)
# have only 'unk' as special word
idx = self.add(unkWord)
self.unk = idx
def size(self):
return len(self.idxToLabel)
# Load entries from a file.
def loadFile(self, filename):
for line in open(filename):
token = line.rstrip('\n')
self.add(token)
def getIndex(self, key, default=None):
if self.lower:
key = key.lower()
try:
return self.labelToIdx[key]
except KeyError:
return default
def getLabel(self, idx, default=None):
try:
return self.idxToLabel[idx]
except KeyError:
return default
# Add `label` in the dictionary. Use `idx` as its index if given.
def add(self, label):
if self.lower:
label = label.lower()
if label in self.labelToIdx:
idx = self.labelToIdx[label]
else:
idx = len(self.idxToLabel) + 1
self.idxToLabel[idx] = label
self.labelToIdx[label] = idx
return idx
# Convert `labels` to indices. Use `unkWord` if not found.
# Optionally insert `bosWord` at the beginning and `eosWord` at the .
def toIdx(self, labels):
vec = [self.getIndex(label, default=self.unk) for label in labels]
return vec
# Convert `idx` to labels. If index `stop` is reached, convert it and return.
def toLabels(self, idx, stop):
labels = []
for i in idx:
labels += [self.getLabel(i)]
if i == stop:
break
return labels