-
Notifications
You must be signed in to change notification settings - Fork 0
/
VocabEncoder.h
301 lines (256 loc) · 7.46 KB
/
VocabEncoder.h
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#ifndef _VOCAB_ENCODER_H_
#define _VOCAB_ENCODER_H_
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <map>
#include <assert.h>
#include <limits.h>
#include "StringUtils.h"
using namespace std;
class VocabEncoder {
public:
int firstId, nextId;
map<string, int> tokenToInt;
map<int, string> intToToken;
std::string UNK;
bool useUnk;
std::set<int> closedVocab;
public:
VocabEncoder() {
firstId = 2;
nextId = firstId;
UNK = "_unk_";
// encode unk
tokenToInt[UNK] = nextId;
intToToken[nextId++] = UNK;
}
// copy constructor (deep)
VocabEncoder(const VocabEncoder &original) {
this->firstId = original.firstId;
this->nextId = original.nextId;
this->tokenToInt = original.tokenToInt;
this->intToToken = original.intToToken;
this->UNK = original.UNK;
this->useUnk = original.useUnk;
this->closedVocab = original.closedVocab;
}
VocabEncoder(const std::string& textFilename, unsigned firstId = 2) {
useUnk = false;
this->firstId = firstId;
nextId = firstId;
UNK = "_unk_";
// encode unk
tokenToInt[UNK] = nextId;
intToToken[nextId++] = UNK;
// create token-int correspondnence
std::ifstream textFile(textFilename.c_str(), std::ios::in);
std::string line;
while(getline(textFile, line)) {
if(line.size() == 0) {
continue;
}
std::vector<string> splits;
StringUtils::SplitString(line, ' ', splits);
for(std::vector<string>::const_iterator tokenIter = splits.begin();
tokenIter != splits.end();
tokenIter++) {
if(tokenToInt.count(*tokenIter) == 0) {
tokenToInt[*tokenIter] = nextId;
intToToken[nextId++] = *tokenIter;
assert(nextId != INT_MAX);
}
}
}
useUnk = true;
}
bool IsClosedVocab(int wordId) const {
return (closedVocab.find(wordId) != closedVocab.end());
}
void AddToClosedVocab(std::string &word) {
int code = Encode(word, false);
closedVocab.insert(code);
}
int UnkInt() const {
return tokenToInt.find(UNK)->second;
}
string UnkString() {
return UNK;
}
// a constant version of the encode function which guarantees that the underlying object state does not change
// i.e. you cannot add new words to the vocab using this method
int ConstEncode(const string &token) const {
if(tokenToInt.count(token) == 0) {
return tokenToInt.find(UNK)->second;
} else {
return tokenToInt.find(token)->second;
}
}
int Encode(const string& token, bool explicitUseUnk) {
if(tokenToInt.count(token) == 0) {
if(explicitUseUnk) {
return tokenToInt[UNK];
} else {
tokenToInt[token] = nextId;
intToToken[nextId++] = token;
assert(nextId != INT_MAX);
return tokenToInt[token];
}
} else {
return tokenToInt[token];
}
}
int Encode(const string& token) {
return Encode(token, useUnk);
}
void Encode(const std::vector<std::string>& tokens, vector<int>& ids) {
assert(ids.size() == 0);
for(vector<string>::const_iterator tokenIter = tokens.begin();
tokenIter != tokens.end();
tokenIter++) {
ids.push_back(Encode(*tokenIter));
}
assert(ids.size() == tokens.size());
}
// if nullToken is of length > 0, this token is inserted at position 0 for each src sentence.
void ReadParallelCorpus(const std::string &textFilename, vector<vector<int> > &srcSents, vector<vector<int> > &tgtSents, const string &nullToken="") {
assert(srcSents.size() == 0 && tgtSents.size() == 0);
// open data file
std::ifstream textFile(textFilename.c_str(), std::ios::in);
// for each line
std::string line;
int lineNumber = -1;
while(getline(textFile, line)) {
// skip empty lines
if(line.size() == 0) {
continue;
}
lineNumber++;
// split tokens
std::vector<string> splits;
StringUtils::SplitString(line, ' ', splits);
// encode tokens
srcSents.resize(lineNumber+1);
tgtSents.resize(lineNumber+1);
vector<int> temp;
Encode(splits, temp);
assert(splits.size() == temp.size());
// src sent is written before tgt sent
bool src = true;
if(nullToken.size() > 0) {
// insert null token at the beginning of src sentence
srcSents[lineNumber].push_back(Encode(nullToken, false));
}
for(unsigned i = 0; i < temp.size(); i++) {
if(splits[i] == "|||") {
// done with src sent.
src = false;
// will now read tgt sent.
continue;
}
if(src) {
srcSents[lineNumber].push_back(temp[i]);
} else {
tgtSents[lineNumber].push_back(temp[i]);
}
}
}
}
// read each line in the text file, encodes each sentence into vector<int> and appends it into 'data'
// assumptions: data is empty
void Read(const std::string &textFilename, vector<vector<int> > &data) {
assert(data.size() == 0);
// open data file
std::ifstream textFile(textFilename.c_str(), std::ios::in);
// for each line
std::string line;
int lineNumber = -1;
while(getline(textFile, line)) {
// skip empty lines
if(line.size() == 0) {
continue;
}
lineNumber++;
// split tokens
std::vector<string> splits;
StringUtils::SplitString(line, ' ', splits);
// encode tokens
data.resize(lineNumber+1);
Encode(splits, data[lineNumber]);
}
}
void PersistVocab(string filename) {
std::ofstream vocabFile(filename.c_str(), std::ios::out);
for(map<int, string>::const_iterator intToTokenIter = intToToken.begin(); intToTokenIter != intToToken.end(); intToTokenIter++) {
bool inClosedVocab = closedVocab.find(intToTokenIter->first) != closedVocab.end();
// c for closed, o for open
vocabFile << intToTokenIter->first << " " << intToTokenIter->second << " " << (inClosedVocab? "c" : "o") << endl;
}
vocabFile.close();
}
const std::string& Decode(int wordId) const {
if(intToToken.count(wordId) == 0) {
return this->UNK;
} else {
return intToToken.find(wordId)->second;
}
}
};
class VocabDecoder {
public:
std::map<int, std::string> vocab;
std::string UNK;
std::set<int> closedVocab;
public:
VocabDecoder(const VocabDecoder& another) {
vocab = another.vocab;
UNK = another.UNK;
closedVocab = another.closedVocab;
}
VocabDecoder(VocabDecoder& another) {
vocab = another.vocab;
UNK = another.UNK;
closedVocab = another.closedVocab;
}
VocabDecoder(const std::string& vocabFilename) {
std::ifstream vocabFile(vocabFilename.c_str(), std::ios::in);
std::string line;
UNK = "_unk_";
while(getline(vocabFile, line)) {
if(line.size() == 0) {
continue;
}
std::vector<std::string> splits;
StringUtils::SplitString(line, ' ', splits);
stringstream ss(splits[0]);
int wordId;
ss >> wordId;
vocab[wordId].assign(splits[1]);
if(splits[2] == string("c")) {
closedVocab.insert(wordId);
} else if(splits[2] == string("o")) {
// do nothing
} else {
// format error!
assert(false);
}
}
vocabFile.close();
vocab[1] = "_null_";
vocab[-1] = "_<s>_";
//vocab[0] = "_zero_"; // shouldn't happen!
}
const std::string& Decode(int wordId) const {
if(vocab.find(wordId) == vocab.end()) {
return this->UNK;
} else {
return vocab.find(wordId)->second;
}
}
bool IsClosedVocab(int wordId) const {
bool x = (closedVocab.find(wordId) != closedVocab.end());
return x;
}
};
#endif