-
Notifications
You must be signed in to change notification settings - Fork 40
/
vocab.py
89 lines (71 loc) · 2.45 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
# vocab object from harvardnlp/opennmt-py
class Vocab(object):
def __init__(self, filename=None, data=None, lower=False):
self.idxToLabel = {}
self.labelToIdx = {}
self.lower = lower
# Special entries will not be pruned.
self.special = []
if data is not None:
self.addSpecials(data)
if filename is not None:
self.loadFile(filename)
def size(self):
return len(self.idxToLabel)
# Load entries from a file.
def loadFile(self, filename):
idx = 0
for line in open(filename):
token = line.rstrip('\n')
self.add(token)
idx += 1
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
# Mark this `label` and `idx` as special
def addSpecial(self, label, idx=None):
idx = self.add(label)
self.special += [idx]
# Mark all labels in `labels` as specials
def addSpecials(self, labels):
for label in labels:
self.addSpecial(label)
# 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)
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 convertToIdx(self, labels, unkWord, bosWord=None, eosWord=None):
vec = []
if bosWord is not None:
vec += [self.getIndex(bosWord)]
unk = self.getIndex(unkWord)
vec += [self.getIndex(label, default=unk) for label in labels]
if eosWord is not None:
vec += [self.getIndex(eosWord)]
return vec
# Convert `idx` to labels. If index `stop` is reached, convert it and return.
def convertToLabels(self, idx, stop):
labels = []
for i in idx:
labels += [self.getLabel(i)]
if i == stop:
break
return labels