-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVocabulary.cpp
142 lines (120 loc) · 4.2 KB
/
Vocabulary.cpp
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
#include "Vocabulary.hpp"
#include <Utils.hpp>
#include <fstream>
#include <iostream>
struct sort_pred {
bool operator()(const Vocabulary::Token* left, const Vocabulary::Token* right) {
return left->count > right->count;
}
};
Vocabulary::Vocabulary(const std::string& trainFile, const int tokenFreqThreshold){
std::ifstream ifs(trainFile.c_str());
std::vector<std::string> tokens;
std::unordered_map<std::string, int> tokenCount;
int unkCount = 0;
int eosCount = 0;
for (std::string line; std::getline(ifs, line); ){
++eosCount;
Utils::split(line, tokens);
for (auto it = tokens.begin(); it != tokens.end(); ++it){
auto it2 = tokenCount.find(*it);
if (it2 == tokenCount.end()){
tokenCount[*it] = 1;
} else {
it2->second += 1;
}
}
}
for (auto it = tokenCount.begin(); it != tokenCount.end(); ++it){
if (it->second >= tokenFreqThreshold){
this->tokenList.push_back(new Vocabulary::Token(it->first, it->second));
}
else {
unkCount += it->second;
}
}
std::sort(this->tokenList.begin(), this->tokenList.end(), sort_pred());
for (int i = 0; i < (int)this->tokenList.size(); ++i){
this->tokenIndex[this->tokenList[i]->str] = i;
}
this->eosIndex = this->tokenList.size();
this->tokenList.push_back(new Vocabulary::Token("*EOS*", eosCount));
this->unkIndex = this->eosIndex+1;
this->tokenList.push_back(new Vocabulary::Token("*UNK*", unkCount));
}
Vocabulary::Vocabulary(const std::string& trainFile, const int tokenFreqThreshold, const bool useSubword){
std::ifstream ifs(trainFile.c_str());
std::vector<std::string> tokens;
std::unordered_map<std::string, int> tokenCount;
int unkCount = 0;
int eosCount = 0;
for (std::string line; std::getline(ifs, line); ){
++eosCount;
Utils::split(line, tokens);
for (auto it = tokens.begin(); it != tokens.end(); ++it){
auto it2 = tokenCount.find(*it);
if (it2 == tokenCount.end()){
tokenCount[*it] = 1;
} else {
it2->second += 1;
}
}
}
for (auto it = tokenCount.begin(); it != tokenCount.end(); ++it){
if (it->second >= tokenFreqThreshold){
auto subword = (it->first).find("@@");
if (subword != std::string::npos) {
this->tokenList.push_back(new Vocabulary::Token(it->first, it->second, true));
} else {
this->tokenList.push_back(new Vocabulary::Token(it->first, it->second, false));
}
}
else {
unkCount += it->second;
}
}
std::sort(this->tokenList.begin(), this->tokenList.end(), sort_pred());
for (int i = 0; i < (int)this->tokenList.size(); ++i){
this->tokenIndex[this->tokenList[i]->str] = i;
}
this->eosIndex = this->tokenList.size();
this->tokenList.push_back(new Vocabulary::Token("*EOS*", eosCount));
this->unkIndex = this->eosIndex+1;
this->tokenList.push_back(new Vocabulary::Token("*UNK*", unkCount));
}
Vocabulary::Vocabulary(const std::string& trainFile, const bool NMTRNNG){
std::ifstream ifs(trainFile.c_str());
std::vector<std::string> tokens;
std::unordered_map<std::string, int> tokenCount;
int eosCount = 0;
for (std::string line; std::getline(ifs, line); ){
++eosCount;
Utils::split(line, tokens);
for (auto it = tokens.begin(); it != tokens.end(); ++it){
auto it2 = tokenCount.find(*it);
if (it2 == tokenCount.end()){
tokenCount[*it] = 1;
} else {
it2->second += 1;
}
}
}
for (auto it = tokenCount.begin(); it != tokenCount.end(); ++it){
auto act0 = (it->first).find("SHIFT"); // 0
auto act1 = (it->first).find("LEFT"); // 1
auto act2 = (it->first).find("RIGHT"); // 2
if (act0 != std::string::npos) {
this->tokenList.push_back(new Vocabulary::Token(it->first, it->second, 0));
} else if (act1 != std::string::npos) {
this->tokenList.push_back(new Vocabulary::Token(it->first, it->second, 1));
} else if (act2 != std::string::npos) {
this->tokenList.push_back(new Vocabulary::Token(it->first, it->second, 2));
} else {
std::cout << "Error: Non shift/reduce word." << std::endl;
}
}
std::sort(this->tokenList.begin(), this->tokenList.end(), sort_pred());
for (int i = 0; i < (int)this->tokenList.size(); ++i){
this->tokenIndex[this->tokenList[i]->str] = i;
}
}