-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.cc
More file actions
42 lines (33 loc) · 845 Bytes
/
dictionary.cc
File metadata and controls
42 lines (33 loc) · 845 Bytes
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
#include "dictionary.h"
#include <cassert>
const string Dictionary::NULL_WORD = "__NULL__";
const int Dictionary::NULL_WORD_ID = 0;
Dictionary::Dictionary() {
Initialize();
}
Dictionary::Dictionary(ifstream& fin) {
Initialize();
string word;
int word_id, word_counts;
while (fin >> word_id >> word >> word_counts) {
assert(word_id == (int) tokens.size());
tokens.push_back(word);
tokens_index[word] = word_id;
}
}
void Dictionary::Initialize() {
tokens.push_back(NULL_WORD);
tokens_index[NULL_WORD] = NULL_WORD_ID;
}
int Dictionary::GetIndex(const string& token) {
if (tokens_index.count(token)) {
return tokens_index[token];
}
int index = tokens.size();
tokens_index[token] = index;
tokens.push_back(token);
return index;
}
string Dictionary::GetToken(int index) {
return tokens[index];
}