-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvocab_dict.py
216 lines (186 loc) · 5.74 KB
/
vocab_dict.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
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
import operator
import pickle
from enum import Enum
import pandas as pd
# import youtokentome as yttm
class TokenizationConstant(Enum):
SOS = "0"
EOS = "1"
OOV = "2"
PAD = "3"
class VocabDictBase:
def __init__(self, name, file_name):
self.name = name
self.file_name = file_name
def load(self):
raise NotImplementedError
def __len__(self):
raise NotImplementedError
def __str__(self):
raise NotImplementedError
def is_bpe(self):
return False
class VocabDict(VocabDictBase):
def __init__(self, name, file_name):
VocabDictBase.__init__(self, name, file_name)
self.word2index = {"SOS": 0, "EOS": 1, "OOV": 2, "PAD": 3}
self.word2count = {"SOS": 1, "EOS": 1, "OOV": 1, "PAD": 1}
self.index2word = {0: "SOS", 1: "EOS", 2: "OOV", 3: "PAD"}
self.n_words = 4 # Count SOS and EOS
def add_sentence(self, sentence):
"""
Add the word in the sentence into the dictionary
:param sentence: Sentence
:return: NIL
"""
assert type(sentence) == str
for word in sentence.split(' '):
self.add_word(word)
def add_word(self, word):
"""
Add a single word into the dictionary
:param word: Word
:return:
"""
if word not in self.word2index:
self.word2index[word] = self.n_words
self.word2count[word] = 1
self.index2word[self.n_words] = word
self.n_words += 1
else:
self.word2count[word] += 1
def get_w2i(self, word):
"""
Get the index of the word. If it does not exists, return OOV number
:param word: Targetting Word
:return: return index or OOV
"""
try:
return self.word2index[word]
except KeyError as ke:
# self.add_word(word)
# return self.word2index[word]
return self.word2index["OOV"]
def get_i2w(self, index):
"""
Get the word by using the index
:param index: Target index
:return: return word
"""
return self.index2word[index]
def load(self):
"""
Load self from self.file_name
:return:
"""
f = open(self.file_name, 'rb')
tmp_dict = pickle.load(f)
f.close()
self.__dict__.update(tmp_dict)
def save(self):
"""
Save self to self.file_name using pickle
:return:
"""
f = open(self.file_name, 'wb')
pickle.dump(self.__dict__, f, 2)
f.close()
def save2(self, path):
"""
Save self to self.file_name using pickle
:return:
"""
f = open(path, 'wb')
pickle.dump(self.__dict__, f, 2)
f.close()
def limit_vocab(self, limiting_size):
"""
Limit the vocabulary to a certain size
self.word2index = {"SOS": 0, "EOS": 1, "OOV": 2, "PAD": 3, "<EMPTY_CODE>": ??}
self.word2count = {"SOS": 1, "EOS": 1, "OOV": 1, "PAD": 1}
:param limiting_size: Limiting Size
:return: NIL, inplace method
"""
sorted_word2count = sorted(self.word2count.items(), key=operator.itemgetter(1), reverse=True)
limited_w2c = dict(sorted_word2count[:limiting_size])
limited_w2c['SOS'] = 1
limited_w2c['EOS'] = 1
limited_w2c['OOV'] = 1
limited_w2c['PAD'] = 1
limited_w2c['<EMPTY_CODE>'] = 1
assert 'SOS' in limited_w2c and 'EOS' in limited_w2c and 'OOV' in limited_w2c and \
'PAD' in limited_w2c and "<EMPTY_CODE>" in limited_w2c
self.word2count = limited_w2c
self._sync_w2i_and_w2c()
def _sync_w2i_and_w2c(self):
"""
Sync self.word2count and self.word2index
:return:
"""
new_word2index = dict()
for key, value in self.word2index.items():
if key in self.word2count:
new_word2index[key] = len(new_word2index)
self.word2index = new_word2index
assert len(self.word2index) == len(self.word2count)
def convert_ids_to_sentence(self, ids):
"""
Convert a list of IDs into a sentence
:param ids: List of IDs
:return: Return a sentence in String
"""
word_list = []
for id in ids:
word = self.get_i2w(id)
word_list.append(word)
return " ".join(word_list)
def __len__(self):
"""
Length of the Tokenizer = Length of W2i
:return:
"""
return len(self.word2index)
def __str__(self):
return str(self.word2index)
def vocab_size(self):
"""
Return the vocab size
:return:
"""
return len(self.word2index)
# class BPEVocabDict(VocabDictBase):
# def __init__(self, name, file_name):
# VocabDictBase.__init__(self, name, file_name)
# self.bpe_model = None
#
# def load(self):
# """
# Load the BPE Model
# :return:
# """
# self.bpe_model = yttm.BPE(model=self.file_name)
#
# def convert_sent_to_ids(self, sent, eos=False):
# enc_seqs = self.bpe_model.encode(sent, output_type=yttm.OutputType.ID,
# bos=eos, eos=eos)
# return enc_seqs
#
# def __len__(self):
# """
# Size of the BPE Model
# :return: Return Size of BPE Model
# """
# return self.bpe_model.vocab_size()
#
# def vocab_size(self):
# """
# Return the vocab size
# :return:
# """
# return self.bpe_model.vocab_size()
#
# def is_bpe(self):
# return True
#
# def convert_ids_to_sentence(self, seqs):
# return self.bpe_model.decode(seqs)