-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_classes.cpp
300 lines (253 loc) · 7.67 KB
/
common_classes.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
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
#include <fstream>
#include <unordered_set>
#include "common_classes.h"
#include "singleton_random.h"
using namespace std;
// Common function for reading text lines from an input file, usually used read text page
void TextFileOperation::ReadText(const string str_filename, vector<string> &vec_str_text_lines)
{
string str_line;
ifstream f_infile;
f_infile.open(str_filename);
if (!f_infile)
{
throw runtime_error("File cannot open to read!");
}
if (!vec_str_text_lines.empty())
vec_str_text_lines.clear();
unsigned int n_length;
unsigned int n_max_length = 0;
while (getline(f_infile, str_line))
{
n_length = str_line.length();
if (n_length > n_max_length) n_max_length = n_length;
vec_str_text_lines.emplace_back(str_line);
}
f_infile.close();
// Convert blank at end of each line to ascii space characters, and this is required by shredding process
auto iter = vec_str_text_lines.begin();
unsigned int n_size;
while (iter != vec_str_text_lines.end())
{
n_size = iter->size();
while (n_size < n_max_length)
{
iter->append(" ");
++n_size;
}
++iter;
}
}
// Common function for reading text lines from an input file, usually used to store dictionary
void TextFileOperation::ReadText(const string str_filename, unordered_set<string> &uset_str_text_lines)
{
string str_line;
ifstream f_infile;
f_infile.open(str_filename);
if (!f_infile)
{
throw runtime_error("File cannot open to read!");
}
if (!uset_str_text_lines.empty())
uset_str_text_lines.clear();
while (getline(f_infile, str_line))
{
uset_str_text_lines.insert(str_line);
}
f_infile.close();
}
// Common function for writing text strips to an output file
void TextFileOperation::WriteText(const string str_filename, const vector<string> vec_str_text_lines)
{
ofstream f_outfile;
f_outfile.open (str_filename);
if (!f_outfile)
{
throw runtime_error("File cannot open to write!");
}
for (int i = 0; i < vec_str_text_lines.size(); ++i)
{
f_outfile << vec_str_text_lines[i] << endl;
}
f_outfile.close();
}
const int TextStripOperation::s_random_number_ = 40;
// Re-order the vector component - each component contain a vector to store a text strip
void TextStripOperation::Disorder(vector<vector<string>>& vec_str_input)
{
unsigned int n_temp = 0;
if (vec_str_input.size() == 0)
{
throw runtime_error("Invalid input!");
}
else
{
SingletonRandom::s_max_val_ = vec_str_input.size() - 1;
SingletonRandom& random_instance = SingletonRandom::GetInstance();
for (int i = 0; i < s_random_number_; ++i)
{
n_temp = random_instance.GenerateRandom();
vec_str_input.emplace(vec_str_input.end(), vec_str_input[n_temp]);
vec_str_input.erase(vec_str_input.begin() + n_temp);
}
}
}
// Transpose row and column(each column is for a text strip)
void TextStripOperation::Transpose(vector<vector<string>>& vec_str_input, vector<vector<string>>& vec_str_input_trans)
{
vector<string> vec_temp;
if (!vec_str_input_trans.empty())
vec_str_input_trans.clear();
if (vec_str_input.size() == 0)
{
throw runtime_error("Invalid input!");
}
for (int i = 0; i < vec_str_input[0].size(); ++i)
{
for (int j = 0; j< vec_str_input.size(); ++j)
{
vec_temp.emplace_back(vec_str_input[j][i]);
}
vec_str_input_trans.emplace_back(vec_temp);
vec_temp.clear();
}
}
// Merge multiple columns into one column
void TextStripOperation::MergeText(vector<vector<string>> & vec_str_input, vector<string>& vec_str_text)
{
string str_temp;
if (vec_str_input.empty())
{
throw runtime_error("vec_str_input is empty, no text data to merge!");
}
if (!vec_str_text.empty())
vec_str_text.clear();
for (int j = 0; j < vec_str_input.begin()->size() ; ++j)
{
for (int i = 0; i < vec_str_input.size(); ++i)
{
str_temp.append(vec_str_input[i][j]);
}
vec_str_text.emplace_back(str_temp);
str_temp.clear();
}
}
// From left hand, identify the word used to lookup in the dictionary from a string containing many words.
// Before calling this function, some delimiter such as "," or "." need to be transformed to blank
// character ' '.
// Below is the list of examples to show how this function works to extract a word (or word piece)
// used for lookup:
// a1a2a3|a4a5 |a7a8a9 --> a1a2a3a4a5
// a3|a4a5a6|a7 a9 --> a3a4a5a6a7
// |a4a5a6|a7a8a9 --> "" (empty string)
// | a5a6|a7a8 --> "" (empty string)
// a1 a3|a4 a6|a7a8a9 --> a3a4
// a1 a3| a5a6|a7a8 --> "" (empty string)
void WordStringOperation::FindLookupWordLeft(string & str_line, string & str_key, int n_column_width)
{
string str_key_t;
if (str_line.empty() || n_column_width <= 0 || str_line.size() <= n_column_width)
throw runtime_error("Invalid input to function FindLookupWordLeft()!");
int n_boundary = n_column_width;
for (int k = 0; k < str_line.size(); ++k)
{
if (' ' != str_line[k])
{
if (str_key_t.empty() && k >= n_boundary)
{
break;
}
else
{
str_key_t = str_key_t + str_line[k];
}
}
else
{
if (k > n_boundary)
{
break;
}
else
{
if (!str_key_t.empty())
str_key_t.clear();
}
}
}
str_key = str_key_t;
}
// From right hand, identify the word used to lookup in the dictionary from a string containing many words.
// Before calling this function, some delimiter such as "," or "." need to be transformed to blank
// character ' '.
// Below is the list of examples to show how this function works to extract a word (or word piece)
// used for lookup:
// a1a2a3| a5a6|a7a8a9 --> a5a6a7a8a9
// a2a3|a4a5a6|a7 --> a2a3a4a5a6a7
// a2a3|a4a5a6| --> "" (empty string)
// a1 a3|a4a5 | --> "" (empty string)
// a1a2a3|a4 a6|a7 a9 --> a6a7
// a2a3|a4a5 |a7 a9 --> "" (empty string)
void WordStringOperation::FindLookupWordRight(string & str_line, string & str_key, int n_column_width)
{
string str_key_t;
if (str_line.size() == 0 || n_column_width <= 0 || str_line.size() <= n_column_width)
throw runtime_error("Invalid input to function FindLookupWordRight()!");
int n_boundary = str_line.size() - n_column_width - 1;
for (int k = str_line.size() - 1; k >= 0; --k)
{
if (' ' != str_line[k])
{
if (str_key_t.empty() && k <= n_boundary)
{
break;
}
else
{
str_key_t = str_line[k] + str_key_t;
}
}
else
{
if (k < n_boundary)
{
break;
}
else
{
if (!str_key_t.empty())
str_key_t.clear();
}
}
}
str_key = str_key_t;
}
// Removing word suffix such as ed|ing|s|es, won't remove suffix if the number of remaining letters are
// less than 2. This function is usually for dictionary lookup.
bool WordStringOperation::RemoveWordSuffix(string &str_lookup_key)
{
vector<string> vec_suffix = {"ing", "ed", "es", "s"};
if (str_lookup_key.size() == 0)
throw runtime_error("Invalid input to function RemoveWordSuffix()!");
int n_position = str_lookup_key.size();
int n_position_t, n_length_t;
bool b_remove_suffix = false;
auto iter = vec_suffix.begin();
while (iter != vec_suffix.end())
{
n_length_t = iter->size();
n_position_t = n_position - n_length_t;
// Need to ensure the number of remaining letters are equal or greater than 2 after removing suffix
if (n_position_t >= 2)
{
if (str_lookup_key.compare(n_position_t,n_length_t,*iter) == 0)
{
str_lookup_key = str_lookup_key.substr(0, n_position_t);
b_remove_suffix = true;
break;
}
}
++iter;
}
return b_remove_suffix;
}