-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
184 lines (144 loc) · 5.84 KB
/
Copy pathParser.cpp
File metadata and controls
184 lines (144 loc) · 5.84 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
#include <future>
#include <fstream>
#include <vector>
#include "utils/split.hpp"
#include "Parser.hpp"
void Parser::parse(const std::string &text, std::function<void(std::vector<std::string> &)> callback, std::function<void(void)> done, bool debug) {
std::vector<std::future<std::vector<std::string>>> pool{};
// Splitting text for parsing
if (debug) {
std::cout << "Splitting text into chunks..." << std::endl;
}
std::regex re("[\n]{2,}"); // Two or more line returns should not split sentences ;)
std::sregex_token_iterator first{text.cbegin(), text.cend(), re, -1};
std::sregex_token_iterator last;
std::vector<std::string> chunks{first, last};
unsigned long numChunks = chunks.size();
if (debug) {
std::cout << "Threading " << numChunks << " chunk parsers..." << std::endl;
}
unsigned long processedChunks = 0;
for (auto &chunk:chunks) {
pool.push_back(std::async(std::launch::async, parseChunk, chunk, false));
//std::vector<std::string> result = parseChunk(chunk, false);
//callback(result);
}
if (debug) {
std::cout << "Waiting on futures..." << std::endl;
}
while (processedChunks < numChunks) {
for (auto &fut:pool) {
if (fut.valid()) {
auto w = fut.get();
if (debug) {
std::cout << " Future " << processedChunks + 1 << " of " << numChunks << " done, ingesting... ";
}
callback(w);
if (debug) {
std::cout << "Done!" << std::endl;
}
//words.insert( words.end(), w.begin(), w.end() );
++processedChunks;
}
}
}
if (debug) {
std::cout << "Done!" << std::endl;
}
done();
}
//void saveIntermediate(std::string name, std::string buff) {
// std::string file = "output-" + name + ".txt";
// std::ofstream output(file.c_str());
// output << buff << "\n";
//}
std::vector<std::string> Parser::parseChunk(std::string textBuffer, bool debug) {
// Remove crap, double space replace in order to eliminate crap for quotes to quotes not being replaced byt their regex
if (debug) {
std::cout << "Removing crap..." << std::endl;
}
textBuffer = std::regex_replace(textBuffer, std::regex("[\\x00-\\x1F_]"), " ");
textBuffer = std::regex_replace(textBuffer, std::regex("-{2,}"), " ");
textBuffer = std::regex_replace(textBuffer, std::regex("http[s]?:\\/\\/(?:.+?) "), " ");
// Make punctuation actual words
if (debug) {
std::cout << "Separating punctuation..." << std::endl;
}
textBuffer = std::regex_replace(textBuffer, std::regex("\\.\\.\\."), " … "); // elipsis
textBuffer = std::regex_replace(textBuffer, std::regex("([\\.\\,\\:\\;\\!\\?\\(\\)\"“”«»])"), " $1 ");
// Before starting to add markers, start by removing anything resembling one
if (debug) {
std::cout << "Removing marker-like structures..." << std::endl;
}
textBuffer = std::regex_replace(textBuffer, std::regex("<(.+?)>"), "($1)");
textBuffer = std::regex_replace(textBuffer, std::regex("[<>]"), " ");
// Extract quotes
if (debug) {
std::cout << "Extracting quotes..." << std::endl;
}
std::vector<std::pair<std::string, std::string>> quoteMarkerPairs{
std::make_pair("\"", "\""),
std::make_pair("“", "”"),
std::make_pair("‘", "’"),
std::make_pair("«", "»")
};
for (auto marker : quoteMarkerPairs) {
textBuffer = std::regex_replace(textBuffer, std::regex(marker.first + "(.+?)" + marker.second), " <q> $1 </q> ");
}
// Special case with spaces not to catch real apostrophes
// We don't want it to be in the stray marker removal also
//textBuffer = std::regex_replace(textBuffer, std::regex("(?:^| )'(.+?)'(?:$| )"), "<q> $1 </q>");
textBuffer = std::regex_replace(textBuffer, std::regex("([^A-Za-z0-9])'(.+?)'([^A-Za-z0-9])"), "$1 <q> $2 </q> $3");
// Experiment: try to put apostrophes as words
textBuffer = std::regex_replace(textBuffer, std::regex("'"), " ' ");
// Extract parens
if (debug) {
std::cout << "Extracting parens..." << std::endl;
}
std::vector<std::pair<std::string, std::string>> parensMarkerPairs{
std::make_pair("\\(", "\\)"),
std::make_pair("\\[", "\\]"),
std::make_pair("\\{", "\\}")
};
for (auto marker : parensMarkerPairs) {
textBuffer = std::regex_replace(textBuffer, std::regex(marker.first + "(.+?)" + marker.second), " <p> $1 </p> ");
}
// Remove stray markers
if (debug) {
std::cout << "Removing stray markers..." << std::endl;
}
std::string strayMarkers = "";
for (auto marker : quoteMarkerPairs) {
strayMarkers += marker.first + marker.second;
}
for (auto marker : parensMarkerPairs) {
strayMarkers += marker.first + marker.second;
}
textBuffer = std::regex_replace(textBuffer, std::regex("[" + strayMarkers + "]"), " ");
// Remove double spaces
if (debug) {
std::cout << "Removing double spaces..." << std::endl;
}
std::regex doubleSpace_re(" ");
while (textBuffer.find(" ") != std::string::npos) {
textBuffer = std::regex_replace(textBuffer, doubleSpace_re, " ");
}
// To lowercase
if (debug) {
std::cout << "Lowercasing..." << std::endl;
}
std::transform(textBuffer.begin(), textBuffer.end(), textBuffer.begin(), ::tolower);
if (debug) {
std::cout << "Vectorizing..." << std::endl;
}
std::vector<std::string> words{};
std::stringstream ss;
ss.str(textBuffer);
std::string word;
while (std::getline(ss, word, ' ')) {
if (!word.empty()) {
words.push_back(word);
}
}
return words;
}