-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
253 lines (204 loc) · 9.96 KB
/
main.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
#include "src/align.h"
#include "src/utils/common.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
std::vector<std::string> GetMandatoryHeaderFields(const std::vector<std::string> &split_metadata_headers) {
std::vector<std::string> header_mandatory_values = {"src_url", "trg_url", "src_text", "trg_text", "src_translated"};
if (split_metadata_headers.size() != 0) {
header_mandatory_values.push_back("src_metadata");
header_mandatory_values.push_back("trg_metadata");
}
return header_mandatory_values;
}
std::unordered_map<std::string, int> ProcessHeader(std::istream &in, bool print_sent_hash,
const std::vector<std::string> &split_metadata_headers) {
std::string line;
std::vector<std::string> split_line;
// Read header
getline(in, line);
utils::SplitString(split_line, line, '\t');
std::unordered_map<std::string, int> header;
std::vector<std::string> header_mandatory_values = GetMandatoryHeaderFields(split_metadata_headers);
for (size_t i = 0; i < split_line.size(); ++i) {
// Get all fields
header[split_line[i]] = i;
// Check out if it is a mandatory field
auto find_result = std::find(header_mandatory_values.begin(), header_mandatory_values.end(), split_line[i]);
if (find_result != std::end(header_mandatory_values)) {
header_mandatory_values.erase(find_result);
}
}
if (header_mandatory_values.size() != 0) {
// Not all mandatory fields were provided
std::stringstream error;
error << "Mandatory fields not found in header:";
for (std::string h : header_mandatory_values) {
error << ' ' << h;
}
throw std::runtime_error(error.str());
}
// Print output header
std::cout << "src_url\ttrg_url\tsrc_text\ttrg_text\tbleualign_score";
if (print_sent_hash)
std::cout << "\tsrc_deferred_hash\ttrg_deferred_hash";
for (const std::string &metadata_header_field : split_metadata_headers) {
std::cout << "\tsrc_" << metadata_header_field << "\ttrg_" << metadata_header_field;
}
std::cout << "\n";
return header;
}
void Process(std::istream &in, float bleu_threshold, bool print_sent_hash, std::string metadata_headers) {
utils::DocumentPair doc_pair;
std::string line;
std::vector<std::string> split_line;
std::vector<std::string> split_metadata_headers;
utils::SplitString(split_metadata_headers, metadata_headers, ',');
std::unordered_map<std::string, int> header_idxs = ProcessHeader(in, print_sent_hash, split_metadata_headers);
std::vector<std::string> header_mandatory_fields = GetMandatoryHeaderFields(split_metadata_headers);
size_t n = 0;
size_t columns = 0;
bool metadata = split_metadata_headers.size() != 0 ? true : false;
while(getline(in, line)) {
++n;
utils::SplitString(split_line, line, '\t');
if (columns == 0) {
// Initialize the expected number of fields for all the lines
columns = split_line.size();
}
// Expect at least 5 (maybe 6 or more if metadata is present) columns
if (split_line.size() < header_mandatory_fields.size()) {
std::stringstream error;
error << "Not enough fields on line " << n << " mandatory header fields are:";
for (const std::string &field : header_mandatory_fields) {
error << " " << field;
}
throw std::runtime_error(error.str());
}
// Check that the number of fields is the expected, since all the lines should contain the same number of fields
if (columns != split_line.size()) {
std::stringstream error;
error << "Different number of fields obtained on line " << n;
throw std::runtime_error(error.str());
}
doc_pair.url1 = split_line[header_idxs["src_url"]];
doc_pair.url2 = split_line[header_idxs["trg_url"]];
utils::DecodeAndSplit(doc_pair.text1, split_line[header_idxs["src_text"]], '\n', true);
utils::DecodeAndSplit(doc_pair.text2, split_line[header_idxs["trg_text"]], '\n', true);
// Process metadata, if provided
if (metadata) {
std::vector<std::string> metadata1, metadata2;
utils::DecodeAndSplit(metadata1, split_line[header_idxs["src_metadata"]], '\n', true);
utils::DecodeAndSplit(metadata2, split_line[header_idxs["trg_metadata"]], '\n', true);
if (doc_pair.text1.size() != metadata1.size()) {
std::stringstream error;
error << "On line " << n << " column " << header_idxs["src_text"] + 1 << " and "
<< header_idxs["src_metadata"] + 1 << " don't have an equal number of lines "
<< "(" << doc_pair.text1.size() << " vs " << metadata1.size() << ")";
throw std::runtime_error(error.str());
}
if (doc_pair.text2.size() != metadata2.size()) {
std::stringstream error;
error << "On line " << n << " column " << header_idxs["trg_text"] + 1 << " and "
<< header_idxs["trg_metadata"] + 1 << " don't have an equal number of lines "
<< "(" << doc_pair.text2.size() << " vs " << metadata2.size() << ")";
throw std::runtime_error(error.str());
}
if (doc_pair.text1metadata.size() < doc_pair.text1.size()) {
doc_pair.text1metadata.resize(doc_pair.text1.size());
}
if (doc_pair.text2metadata.size() < doc_pair.text2.size()) {
doc_pair.text2metadata.resize(doc_pair.text2.size());
}
for (size_t i = 0; i < metadata1.size(); ++i) {
utils::SplitString(doc_pair.text1metadata[i], metadata1[i], '\t');
if (doc_pair.text1metadata[i].size() != split_metadata_headers.size()) {
std::stringstream error;
error << "On line " << n << " column " << header_idxs["src_metadata"] + 1 << " "
<< "has " << doc_pair.text1metadata[i].size() << " fields, but "
<< split_metadata_headers.size() << " were provided";
throw std::runtime_error(error.str());
}
}
for (size_t i = 0; i < metadata2.size(); ++i) {
utils::SplitString(doc_pair.text2metadata[i], metadata2[i], '\t');
if (doc_pair.text2metadata[i].size() != split_metadata_headers.size()) {
std::stringstream error;
error << "On line " << n << " column " << header_idxs["trg_metadata"] + 1 << " "
<< "has " << doc_pair.text2metadata[i].size() << " fields, but "
<< split_metadata_headers.size() << " were provided";
throw std::runtime_error(error.str());
}
}
}
// Processed version of text 1 (i.e. translated to match language text 2)
utils::DecodeAndSplit(doc_pair.text1translated, split_line[header_idxs["src_translated"]], '\n', true);
if (doc_pair.text1.size() != doc_pair.text1translated.size()) {
std::stringstream error;
error << "On line " << n << " column " << header_idxs["src_text"] + 1 << " and "
<< header_idxs["src_translated"] + 1 << " don't have an equal number of lines "
<< "(" << doc_pair.text1.size() << " vs " << doc_pair.text1translated.size() << ")";
throw std::runtime_error(error.str());
}
// Optionally sixth column with processed version of text 2 (i.e. to better
// match with the processed version of text 1)
if (header_idxs.find("trg_translated") == header_idxs.end()) {
doc_pair.text2translated = doc_pair.text2;
} else {
utils::DecodeAndSplit(doc_pair.text2translated, split_line[header_idxs["trg_translated"]], '\n', true);
if (doc_pair.text2.size() != doc_pair.text2translated.size()) {
std::stringstream error;
error << "On line " << n << " column " << header_idxs["trg_text"] + 1 << " and "
<< header_idxs["trg_translated"] + 1 << " don't have an equal number of lines "
<< "(" << doc_pair.text2.size() << " vs " << doc_pair.text2translated.size() << ")";
throw std::runtime_error(error.str());
}
}
align::AlignDocument(doc_pair, bleu_threshold, print_sent_hash);
std::cout << std::flush;
}
}
int main(int argc, char *argv[]) {
float bleu_threshold = 0.0f;
bool print_sent_hash = false;
std::string metadata_header_fields;
std::vector<std::string> filenames;
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("bleu-threshold", po::value(&bleu_threshold), "BLEU threshold for matched sentences")
("print-sent-hash", po::bool_switch(&print_sent_hash)->default_value(false), "print Murmurhash hashes of the output sentences")
("metadata-header-fields", po::value(&metadata_header_fields), "language agnostic header fields, comma separated")
("input-file", po::value(&filenames));
po::positional_options_description positional;
positional.add("input-file", -1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).positional(positional).run(), vm);
po::notify(vm);
if (vm.count("help")) {
std::cerr << "Reads matched documents from input-files or stdin of none specified, outputs aligned sentences to stdout\n" <<
"Tab-separated fields of the input are url1, url2, text1_base64, text2_base64, text1translated_base64 [ , text2translated_base64 ]\n"
"[ , text1metadata_base64, text2metadata_base64 ]\n" <<
"Tab-separated fields of the output are url1, url2, sent1, sent2, score [ , murmurhash_text1, murmurhash_text2 ]\n"
"[ , metadata1_text1, metadata1_text2 ...] \n\n" <<
"Usage: " << argv[0] << " [--help] [--bleu-threshold <threshold>] [--print-sent-hash] [--metadata-header-fields <field1>,...]\n"
"[<input-file>...]\n\n" <<
desc << std::endl;
return 1;
}
if (filenames.empty())
Process(std::cin, bleu_threshold, print_sent_hash, metadata_header_fields);
else
for (std::string const &filename : filenames) {
std::ifstream fin(filename);
Process(fin, bleu_threshold, print_sent_hash, metadata_header_fields);
}
return 0;
}