/
parser.cpp
278 lines (244 loc) · 5.39 KB
/
parser.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
/* **STEPS FOR PRE-FORMATTING**
*
* this parser takes in two files: the list of english words and the formatted file of
* corresponding definitions.
*
* get rid of the space in between the word and parenthesis
* ex:
* aardvark (animal) would turn into:
* aardvark(animal)
*
* Get list of all parts of speech
*
* POS should have a period after it
* if it doesn't, change to this format : adj.
*
* find all parts of speech in the middle of a line and replace them
* with \n2 pos.
*
* then change all brackets "[]" and curly braces "{}" to parenthesis
*
* make sure there is no extra whitespace after opening parenthesis or before closing parenthesis
* make sure every opening parenthesis has a comma then a space before it (or a part of speech)
*
* do the same with everything inside parenthesis (put them on their own \n with a 2 in front) (?<!\.\ )(\()
* Also: the definitions in the parenthesis should be the only thing on that line. So put a \n after each
* close parenthesis
*
* check for errors: multiple of any special character (commas, parenthesis, quotes etc.) replace with
* just one
*
* for now, all files have to end with THIS_IS_THE_END
* add a comma to the end of every line
*/
int parenthesis_check(string s) {
if (s == "")
return -1;
for (int i = 0; i < s.length(); ++i) {
if (s[i] == '(') {
return i;
}
}
return -1;
}
int par_2check(string s) {
if (s == "")
return -1;
for (int i = 0; i < s.length(); ++i) {
if (s[i] == ')') {
return i;
}
}
return -1;
}
int comma_pos_check(string s) {
if (s == "")
return -1;
// goes backwards for speed
for (int i = s.length() - 1; i > 0; --i) {
if (s[i] == ',') {
return i;
}
}
return -1;
}
bool pos_check(string& s) {
if (s == "")
return false;
for (int i = 0; i < s.length(); ++i) {
if (s[i] == '.' && s[i + 1] != ')' && s[i + 1] != ']') {
int ind = comma_pos_check(s);
if (ind != -1) {
s.erase(ind, 1);
}
return true;
}
}
return false;
}
void commas(ifstream& in, ofstream& out) {
while (1) {
string line;
getline(in, line);
if (line == "THIS_IS_THE_END") break;
if (line == "") continue;
if (line[line.length() - 1] != ',')
out << line << "," << endl;
else out << line << endl;
}
} //hello, my name is santiago
int comma_count(stringstream& ss) {
string s;
ss >> s;
int count = 0;
for (int i = 0; i < s.length(); ++i) {
if (s[i] == ',') {
count++;
}
}
return count;
}
/*string switch_quotes(string s) {
// switches all double quotes inside of definitions to single quotes
if (s == "")
return "";
for (int i = 0; i < s.length(); ++i) {
if (s[i] == '\"'){
s[i] = '\'';
}
}
return s;
}*/
string engword(ifstream& in) {
string word, def, line;
stringstream ss;
getline(in, line);
// used as a end of file flag
if (line == "THIS_IS_THE_END") {
return "";
}
ss << line;
int num = 0;
while (1) {
//cout << ++num;
ss >> word;
int p = parenthesis_check(word);
if (word == "") break;
if (p > -1) {
word.erase(p, word.length() - 1);
if (def == "")
{
def += word;
break;
}
else {
def += " " + word;
break;
}
}
else if (def == "")
{
def += word;
}
else def += " " + word;
if (!ss.good()) break;
}
return def;
}
void pos_body(string& pos, string def, vector<string> defs, string line, string eng, ofstream& out) {
stringstream ss;
string s;
ss << line;
//ss >> s;
// get the pos if there is one
int Opar = parenthesis_check(line);
if (Opar != -1) {
int Cpar = par_2check(line);
string str;
str += line.substr(Opar, Cpar);
if (!pos_check(pos))
out << "\"" << eng << "\",\"" << str << "\"," << endl;
else
out << "\"" << eng << "\",\"" << str << "\",\"" << pos << "\"" << endl;
}
else {
ss >> pos;
if (!pos_check(pos)) {
if (pos[pos.length() - 1] == ',') {
pos.erase(pos.length() - 1, 1);
def = pos;
defs.push_back(def);
pos = "";
}
else {
getline(ss, line, ',');
def += pos + line;
defs.push_back(def);
pos = "";
}
}
// way to keep the part of speech for the lines that contain only something in parenthesis
//else { pos = s; }
// split up definitions that are split up by a ,
while (getline(ss, def, ',')) {
if (def[0] == ' ') {
// get rid of the spaces before definitions
def.erase(0, 1);
}
defs.push_back(def);
}
for (int i = 0; i < defs.size(); ++i) {
out << eng << "\t" << "\"" << defs[i] << "\"" << "\t" << pos << endl;
}
}
}
bool POS_get(ifstream& in, ifstream& in2, ofstream& out) {
// one line at a time
string def, line, line2, pos;
vector<string> defs;
getline(in, line);
if (line == "THIS_IS_THE_END") {
return false;
}
// get the word in english
string eng = engword(in2);
//line = switch_quotes(line);
pos_body(pos, def, defs, line, eng, out);
char c;
while (1) {
c = in.get();
if (c == '2') {
getline(in, line2);
line.erase(0, 2);
pos_body(pos, def, defs, line2, eng, out);
}
else {
in.unget();
break;
}
}
return true;
}
int main() {
ifstream in;
in.open("swedish2.txt");
ofstream out;
out.open("English-SwedishTEST.txt");
ifstream in2;
in2.open("words.txt");
if (!in.good()) return 0;
if (!in2.good()) return 0;
bool good = true;
while (good) {
good = POS_get(in, in2, out);
}
in2.close();
in.close();
out.close();
}