-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringUtils.h
143 lines (130 loc) · 4.17 KB
/
StringUtils.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
#ifndef _STRING_UTILS_H_
#define _STRING_UTILS_H_
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <set>
typedef std::string string;
typedef std::stringstream stringstream;
class StringUtils {
public:
// string split
static void SplitString(const string& s, char delim, std::vector<string>& elems) {
stringstream ss(s);
string item;
while(getline(ss, item, delim)) {
elems.push_back(item);
}
}
// 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;
}
int 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 int 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 {
int 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(int 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::ofstream textFile(filename.c_str(), std::ios::out);
for(int i = 0 ; i < tokens.size(); i++) {
if(tokens[i].size() != 0) {
textFile << tokens[i][0];
}
for(int j = 1 ; j < tokens[i].size(); j++) {
textFile << " " << tokens[i][j];
}
textFile << "\n";
}
textFile.close();
}
};
#endif