Repository navigation
Expand file tree
/
Copy pathStringUtils.h
More file actions
203 lines (182 loc) · 5.92 KB
/
Copy pathStringUtils.h
File metadata and controls
203 lines (182 loc) · 5.92 KB
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
#ifndef _STRING_UTILS_H_
#define _STRING_UTILS_H_
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <set>
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
typedef std::string string;
typedef std::stringstream stringstream;
class StringUtils {
public:
// trim from start
static inline std::string <rim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static inline std::string &RTrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
static inline std::string &Trim(std::string &s) {
return LTrim(RTrim(s));
}
static void Lowercase(string &data) {
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
}
// string split
static void SplitString(const string& s, char delim, std::vector<string>& elems) {
stringstream ss(s);
string item;
while(getline(ss, item, delim)) {
if(item.size() == 0) { continue; }
elems.push_back(item);
}
}
static void RSplitString(const string& s, char delim, std::vector<string>& elems) {
stringstream ss;
std::vector<string> splitted;
StringUtils::SplitString(s, delim, splitted);
auto last = splitted.end();
last--;
for(auto it = splitted.begin(); it != last; it++) {
if(it == splitted.begin()) {
ss << *it;
} else {
ss << delim << *it;
}
}
elems.push_back(ss.str());
elems.push_back(*last);
}
// read int tokens
static void ReadIntTokens(const string& sentence, std::vector<int>& intTokens) {
std::vector<string> stringTokens;
SplitString(sentence, ' ', stringTokens);
for (std::vector<string>::iterator tokensIter = stringTokens.begin();
tokensIter < stringTokens.end(); tokensIter++) {
int intToken;
stringstream stringToken(*tokensIter);
stringToken >> intToken;
intTokens.push_back(intToken);
}
}
// read int token set
static void ReadIntTokens(const string& sentence, std::set<int>& intTokens) {
std::vector<string> stringTokens;
SplitString(sentence, ' ', stringTokens);
for (std::vector<string>::iterator tokensIter = stringTokens.begin();
tokensIter < stringTokens.end(); tokensIter++) {
int intToken;
stringstream stringToken(*tokensIter);
stringToken >> intToken;
intTokens.insert(intToken);
}
}
// compute a measure of orthographic similarity between two words
static double ComputeOrthographicSimilarity(const std::string& srcWord, const std::string& tgtWord) {
if(srcWord.length() == 0 || tgtWord.length() == 0) {
return 0.0;
}
unsigned levenshteinDistance = LevenshteinDistance(srcWord, tgtWord);
if(levenshteinDistance > (srcWord.length() + tgtWord.length()) / 2) {
return 0.0;
} else {
double similarity = (srcWord.length() + tgtWord.length()) / ((double)levenshteinDistance + 1);
return similarity;
}
}
// levenshtein distance
static unsigned LevenshteinDistance(const std::string& x, const std::string& y) {
if(x.length() == 0 && y.length() == 0) {
return 0;
}
if(x.length() == 0) {
return y.length();
} else if (y.length() == 0) {
return x.length();
} else {
unsigned cost = x[0] != y[0]? 1 : 0;
std::string xSuffix = x.substr(1);
std::string ySuffix = y.substr(1);
return std::min( std::min( LevenshteinDistance(xSuffix, y) + 1,
LevenshteinDistance(x, ySuffix) + 1),
LevenshteinDistance(xSuffix, ySuffix) + cost);
}
}
static std::string IntVectorToString(const std::vector<int> &numbers) {
std::stringstream ss("");
for(unsigned i = 0; i < numbers.size(); i++) {
ss << numbers[i] << " ";
}
return ss.str();
}
// 'tokens' is a vector of sentences
// each sentence is a vector of space-separated tokens
static void ReadTokens(const std::string &filename, std::vector<std::vector<std::string> > &tokens) {
std::ifstream textFile(filename.c_str(), std::ios::in);
std::string line;
// for each line
while(getline(textFile, line)) {
// skip empty lines
if(line.size() == 0) {
continue;
}
std::vector<string> splits;
SplitString(line, ' ', splits);
tokens.push_back(splits);
}
textFile.close();
}
// 'tokens' is a vector of sentences
// each sentence is a vector of space-separated tokens
static void ReadTokens(const std::string &filename, std::vector<std::vector<int> > &tokens) {
std::ifstream textFile(filename.c_str(), std::ios::in);
std::string line;
// for each line
while(getline(textFile, line)) {
// skip empty lines
if(line.size() == 0) {
continue;
}
std::vector<int> splits;
ReadIntTokens(line, splits);
tokens.push_back(splits);
}
textFile.close();
}
static void WriteTokens(const std::string &filename, std::vector<std::vector<int> > &tokens,
std::map<int, string> &intToStringMap) {
std::ofstream textFile(filename.c_str(), std::ios::out);
for(unsigned i = 0 ; i < tokens.size(); i++) {
if(tokens[i].size() != 0) {
textFile << intToStringMap[tokens[i][0]];
}
for(unsigned j = 1 ; j < tokens[i].size(); j++) {
textFile << " " << intToStringMap[tokens[i][j]];
}
textFile << "\n";
}
textFile.close();
}
static void WriteTokens(const std::string &filename, std::vector<std::vector<int> > &tokens) {
std::ofstream textFile(filename.c_str(), std::ios::out);
for(unsigned i = 0 ; i < tokens.size(); i++) {
if(tokens[i].size() != 0) {
textFile << tokens[i][0];
}
for(unsigned j = 1 ; j < tokens[i].size(); j++) {
textFile << " " << tokens[i][j];
}
textFile << "\n";
}
textFile.close();
}
};
#endif