-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdhead.cpp
349 lines (300 loc) · 8.19 KB
/
stdhead.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "stdhead.h"
void parse(int argc, char** argv, vector<argparse>& param, string& path) {
char name;
for (int i = 1; i < argc; ) {
if ((name = argv[i][1]) == 'f' || (name = argv[i][1]) == 'c') {
param.emplace_back(name, "");
i += 1;
}
else if ((name = argv[i][1]) == 'd') {
if (strcmp(argv[i + 1], "-s") == 0) {
param.emplace_back(name, "", true);
i += 2;
}
else {
param.emplace_back(name, "");
i += 1;
}
}
else if (argv[i][0] != '-') {
path = argv[i];
i += 1;
}
else {
param.emplace_back(name, argv[i + 1]);
i += 2;
}
}
if (path == "") {
cerr << "No path in paramters!";
exit(1);
}
}
void CharacterCount(string& text, unordered_map<string, double>& umap, int limit = 0) {
vector<double> frequency(26, 0);
vector<Freq> wf;
for (char c = 'a'; c <= 'z'; ++c) {
frequency[c - 'a'] = count(text.begin(), text.end(), c) + count(text.begin(), text.end(), c - 32);
}
double sum = 0;
for (auto i : frequency) {
sum += i;
}
for (auto& i : frequency) {
if(i!=0)
i = (long long)(i / sum * 10000) / 100.0;
}
for (int i = 0; i < 26; ++i) {
wf.emplace_back('a' + i, frequency[i]);
}
stable_sort(wf.begin(), wf.end());
if (limit != 0) {
long counter = 0;
for (auto w : wf) {
++counter;
if (counter > limit || w.freq == 0)
break;
cout << fixed << setprecision(2) << w.word << ": " << w.freq << "%" << endl;
}
}
else {
for (auto w : wf) {
if (w.freq == 0)
break;
cout << fixed << setprecision(2) << w.word << ": " << w.freq << "%" << endl;
}
}
cout.unsetf(ios::fixed);
}
inline bool m_isalpha(char c) {
return (c >= 65 && c <= 90 || c >= 97 && c <= 122);
}
void WordCount(string& text, unordered_map<string, double>& umap, bool isXExist, string stopWordsPath) {
string wordNow;
//flag=0,outside, flag=1,within
int flag = 0;
for (unsigned char c : text) {
if (flag == 0) {
if (m_isalpha(c)) {
flag = 1;
wordNow += c;
}
}
else if (flag == 1) {
if (isalnum(c)) {
wordNow += c;
}
else {
flag = 0;
umap[wordNow] += 1;
wordNow = "";
}
}
}
if (flag == 1) {
umap[wordNow] += 1;
}
if (isXExist) {
ifstream input;
input.open(stopWordsPath, ios::in);
stringstream ss;
ss << input.rdbuf();
string text(ss.str());
const char *delim = " \n\t";
char *token = strtok(const_cast<char *>(text.c_str()), delim);
while (token != NULL) {
umap[token] = 0;
token = strtok(NULL, delim);
}
}
}
void m_output(unordered_map<string, double>& umap, int limit) {
vector<WordFreq> wordList;
/*
double sum = 0;
for (auto w : umap) {
sum += w.second;
}
for (auto& w : umap) {
w.second = (long long)(w.second / sum * 10000) / 100.0;
}
*/
for (auto& w : umap) {
wordList.emplace_back(w.first, w.second);
}
stable_sort(wordList.begin(), wordList.end());
if (limit != 0) {
long counter = 0;
for (auto w : wordList) {
++counter;
if (counter > limit || w.freq == 0)
break;
cout << fixed << setprecision(2) << w.word << ": " << w.freq << endl;
}
}
else {
for (auto w : wordList) {
if (w.freq == 0)
break;
cout << fixed << setprecision(2) << w.word << ": " << w.freq << endl;
}
}
cout.unsetf(ios::fixed);
}
void DirSearch(string dirPath, bool isRecursive, string& inputString) {
vector<string> files;
for (auto& f : fs::directory_iterator(dirPath)) {
cout << f << endl;
if (is_directory(f)) {
if (isRecursive) {
DirSearch(f.path().u8string(), isRecursive, inputString);
continue;
}
}
ifstream inputFile;
inputFile.open(f.path(), ios::in);
stringstream ss;
ss << inputFile.rdbuf();
string text(ss.str());
inputString = inputString + " " + text;
}
m_tolower1(inputString);
}
void PhraseCount(string& inputString, int len, unordered_map<string, double>& umap, bool isVExist, string verbDictPath, bool isXExist, string stopwordsPath) {
//since one word who appears in stopwords may appear in verb-dict, so we firstly, change every word to
//suppose all stopwords appear in their original form
unordered_map<string, string> verbMap;
if (isVExist) {
ifstream verbDict;
verbDict.open(verbDictPath, ios::in);
string rule;
string arrow, index, value;
while (getline(verbDict, rule)) {
stringstream srule(rule);
srule >> value >> arrow >> index;
const char* delim = ",";
char* token = strtok(const_cast<char *>(index.c_str()), delim);
while (token != NULL) {
verbMap[token] = value;
token = strtok(NULL, delim);
}
}
}
unordered_set<string> stopwordsList;
if (isXExist) {
ifstream stopwordsFile;
stopwordsFile.open(stopwordsPath, ios::in);
string stopword;
while (stopwordsFile >> stopword) {
stopwordsList.insert(stopword);
}
}
vector<vector<string>> inputStringVec;
m_search(inputString, inputStringVec);
for (auto& contString : inputStringVec) {
if (isXExist) {
contString.erase(remove_if(contString.begin(), contString.end(), [&stopwordsList](string s) -> bool {return stopwordsList.find(s) != stopwordsList.end(); }), contString.end());
}
if (isVExist) {
for_each(contString.begin(), contString.end(), [&verbMap](string& s) {if (verbMap.find(s) != verbMap.end()) s = verbMap[s]; });
}
/*
for (auto it = contString.begin(); it != contString.end();) {
if (isXExist) {
if (stopwordsList.find(*it) != stopwordsList.end())
it = contString.erase(it);
else{
if (isVExist && verbMap.find(*it) != verbMap.end())
*it = verbMap[*it];
++it;
}
}
}*/
for (auto it = contString.begin(); distance(it, contString.end()) >= len; ++it) {
//string phrase = boost::algorithm::join(vector<string>(it, it+len), " ");
string phrase = m_join(vector<string>(it, it + len), " ");
umap[phrase] += 1;
}
}
}
inline string m_join(vector<string> vec, string sep) {
if (vec.size() == 0)
return "";
string s = vec[0];
for (auto it = vec.begin() + 1; it != vec.end(); ++it) {
s += (sep + *it);
}
return s;
}
void m_search(string& inputString, vector<vector<string>>& inputStringVec) {
bool isin = false;
string::iterator sbegin;
vector<string> segment;
for (auto it = inputString.begin(); it != inputString.end(); ++it) {
if (isin) {
char temp = *it;
if (!m_isalnum(temp)) {
segment.push_back(string(sbegin, it));
isin = false;
if ((temp != ' ') && (temp != '\t') && (temp != '\n')) {
inputStringVec.push_back(segment);
segment.clear();
}
}
}
else {
if (m_isalpha(*it)) {
sbegin = it;
isin = true;
}
}
}
if (isin) {
segment.push_back(string(sbegin, inputString.end()));
inputStringVec.push_back(segment);
}
}
inline bool m_isalnum(char c) {
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9';
}
void PairCount(string& inputString, string prepFilePath, string verbDictFile, unordered_map<string, double>& umap) {
ifstream prepFile(prepFilePath);
ifstream verbFile(verbDictFile);
unordered_map<string, string> verbMap;
string rule;
string arrow, index, value;
while (getline(verbFile, rule)) {
stringstream srule(rule);
srule >> value >> arrow >> index;
verbMap[value] = value;
const char* delim = ",";
char* token = strtok(const_cast<char *>(index.c_str()), delim);
while (token != NULL) {
verbMap[token] = value;
token = strtok(NULL, delim);
}
}
string prep;
unordered_set<string> prepList;
while (getline(prepFile, prep)) {
prepList.insert(prep);
}
vector<vector<string>> inputStringVec;
m_search(inputString, inputStringVec);
for (auto& contString : inputStringVec) {
for (auto it = contString.begin(); distance(it, contString.end()) >= 2; it += 2) {
auto it1 = verbMap.find(*it);
if (it1 != verbMap.end() && prepList.find(*(it + 1)) != prepList.end()) {
auto test = verbMap.find(*it);
string vp = m_join(vector<string>{it1->second, *(it + 1)}, " ");
umap[vp] += 1;
}
}
}
}
void m_tolower1(string &s) {
for (char& c : s) {
if (c >= 65 && c <= 90)
c += 32;
}
}