-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtreebankparser.cpp
272 lines (242 loc) · 8.75 KB
/
treebankparser.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
/**
* \file treebankparser.cpp
*
* \brief Main treebank-parser class.
*
* \author Damir Cavar <dcavar@iu.edu>
*
* \version 0.2
*
* \date 2018/10/12 14:18:00
*
* \date Created on: Mon September 10 16:20:00 2016
*
* \copyright Copyright 2016-2018 by Damir Cavar
*
* \license{Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.}
*
* \note This needs some more coding.
*
* \bug None
*/
#include "treebankparser.h"
TreebankParser::TreebankParser() {
grammarfile = "";
rootsymbol = "ROOT";
}
TreebankParser::~TreebankParser() = default;
void TreebankParser::processFiles(vector<string> treebankfiles) {
// load grammar
ifstream ipf;
ipf.exceptions(ifstream::failbit | ifstream::badbit);
stringstream strbuffer;
for (auto &filename : treebankfiles) {
try {
ipf.open(filename.c_str(), ios::in);
if (ipf.is_open()) {
while (!ipf.eof()) {
ipf >> strbuffer.rdbuf();
}
}
} catch (ifstream::failure &e) {
cout << "Cannot read file " << filename << "! Skipping..." << endl;
}
ipf.close();
if (!parseBrackets(strbuffer.str())) {
cout << "Error: Bracket mismatch in file: " << filename << "! Skipping..." << endl;
}
}
// keep track of terminal rules
tagTerminalRules();
}
void TreebankParser::loadGrammar(string fname) {
ifstream ipf;
ipf.exceptions(ifstream::failbit | ifstream::badbit);
stringstream strbuffer;
try {
ipf.open(fname.c_str(), ios::in);
if (ipf.is_open()) {
while (!ipf.eof()) {
ipf >> strbuffer.rdbuf();
}
}
} catch (ifstream::failure &e) {
cout << "Cannot read grammar file " << fname << "! Skipping..." << endl;
}
ipf.close();
}
void TreebankParser::tagTerminalRules() {
set<string> lhssymbols;
for (auto const &rule : rules) {
lhssymbols.insert(rule.first[0]);
}
bool lhssymfound = false;
for (auto const &rule : rules) {
lhssymfound = false;
for (unsigned long i = 1; i < rule.first.size(); ++i) {
if (lhssymbols.find(rule.first[i]) != lhssymbols.end()) {
lhssymfound = true;
break;
}
}
if (!lhssymfound) { // save to set of terminal rules
terminalRules.insert(rule.first);
}
}
}
bool TreebankParser::parseBrackets(string content) {
// keep list of opening and closing brackets as they match
vector<unsigned long> opening;
// storage buffer for local levels
map<int, vector<string>> levels;
for (string::size_type i = 0; i < content.size(); ++i) {
if (content[i] == '(') {
opening.push_back(i);
continue;
}
if (content[i] == ')') {
const unsigned long count = opening.size();
if (count > 0) {
const unsigned long from = opening[count - 1];
string subtree = content.substr(from + 1, i - from - 1);
// store level information for each parsed bracket,
// when hitting closing check whether it is closing with bracketed structure inside,
// fetch all children from the level below and generate RHS
// if there is no ( or ) this is a terminal
const unsigned long pos = subtree.find('(');
if (pos != string::npos) {
// the node label is LHS and all daughters from level +1 are RHS
unsigned long level = opening.size();
// get the string between the ( and the next (
vector<string> strs;
string res = subtree.substr(0, pos);
boost::trim(res);
boost::split(strs, res, boost::is_any_of(" "));
// if root level and no symbol, use rootsymbol from command line or default
if ((level == 1) & (strs.size() == 1) & strs[0].empty()) {
strs = vector<string>({rootsymbol});
}
auto const j = levels.find((int)level + 1);
if (j != levels.end()) {
vector<string> tmp = j->second;
for (auto const &rhs : tmp) {
strs.push_back(rhs);
}
}
levels.erase((int)level + 1); // remove the daughters
vector<string> tmp;
tmp.push_back(strs[0]);
// push back the daughter for this node onto the level
levels[level] = tmp;
// push on rules map
auto const ruleIt = rules.find(strs);
unsigned long val = 0;
if (ruleIt != rules.end())
val = ruleIt->second;
rules[strs] = ++val;
} else {
// this is a tag with terminal
vector<string> strs;
boost::split(strs, subtree, boost::is_any_of(" "));
if (!strs.empty()) {
// add to rules and count
auto const ruleIt = rules.find(strs);
unsigned long val = 0;
if (ruleIt != rules.end())
val = ruleIt->second;
// save the rule
rules[strs] = ++val;
vector<string> tmp;
unsigned long level = opening.size();
auto const j = levels.find((int)level);
if (j != levels.end())
tmp = j->second;
// add only the left-hand-side symbol to the buffer
tmp.push_back(strs[0]);
levels[level] = tmp;
}
}
opening.pop_back(); // pop matching element
} else { // mismatch of brackets detected
return (false);
}
}
}
return (true);
}
void TreebankParser::saveToFile(string fname) {
// create backup of old grammar file
if (boost::filesystem::exists(fname)) {
if (boost::filesystem::is_regular_file(fname))
boost::filesystem::rename(fname, fname + ".bak");
}
try {
ofstream fout;
fout.open(fname);
ostream *fp = &fout;
printToStream(*fp);
fout.flush();
fout.close();
} catch (ifstream::failure &e) {
cout << "Error writing to file " << fname << endl;
}
}
void TreebankParser::printToStdout() {
ostream *fp = &cout;
printToStream(*fp);
}
void TreebankParser::printToStream(ostream &buf) {
map<vector<string>, unsigned long> prules;
for (auto const &rule : rules) {
if (skipterminals) {
if (terminalRules.find(rule.first) == terminalRules.end()) {
prules.insert(rule);
}
} else {
prules.insert(rule);
}
}
const double total = accumulate(begin(prules),
end(prules),
0,
[](const unsigned long previous,
const pair<vector<string>, unsigned long> &rule) {
return previous + rule.second;
});
unsigned long len;
for (
auto const &rule :
prules) {
if (relcounts) // print double using Boost lexical_cast
buf <<
boost::lexical_cast<string>(rule
.second / total);
else
buf << rule.
second;
buf << "\t" << rule.first[0] << " --> ";
len = rule.first.size();
if (len > 2) {
for (
unsigned long i = 1;
i < len - 1; ++i) {
buf << rule.first[i] << " ";
}
buf << rule.first[len - 1] <<
endl;
} else {
buf << rule.first[1] <<
endl;
}
}
}