Skip to content

Instantly share code, notes, and snippets.

@wincentbalin
Created May 6, 2019 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wincentbalin/4a14a831c1373995b92826df8178b47d to your computer and use it in GitHub Desktop.
Save wincentbalin/4a14a831c1373995b92826df8178b47d to your computer and use it in GitHub Desktop.
batch_tester.cc patch for export3.tgz @ http://www.openfst.org/twiki/bin/view/Contrib/ThraxContrib
--- export/batch_tester/batch_tester.cc.orig 2016-02-09 12:38:34.000000000 +0000
+++ export/batch_tester/batch_tester.cc 2019-04-27 20:04:32.358891300 +0000
@@ -37,11 +37,11 @@
using thrax::GrmManager;
using thrax::InputBuffer;
using thrax::OpenOrDie;
-using thrax::Split;
+//using thrax::Split;
-typedef StringCompiler<StdArc> Compiler;
-typedef StringPrinter<StdArc> Printer;
-typedef VectorFst<StdArc> Transducer;
+typedef fst::StringCompiler<fst::StdArc> Compiler;
+typedef fst::StringPrinter<fst::StdArc> Printer;
+typedef fst::VectorFst<fst::StdArc> Transducer;
DEFINE_string(far, "", "Path to the FAR.");
DEFINE_string(input_mode, "byte", "Either \"byte\", \"utf8\", or the path to a "
@@ -55,14 +55,14 @@
enum TokenType { SYMBOL = 1, BYTE = 2, UTF8 = 3 };
-bool ReadInput(string* s) {
- cout << "Input string: ";
- return static_cast<bool>(getline(cin, *s));
+bool ReadInput(std::string* s) {
+ std::cout << "Input string: ";
+ return static_cast<bool>(getline(std::cin, *s));
}
bool RewriteOutput(Printer* printer, Transducer* fst,
- string* output) {
- GrmManager::StringifyFst(fst);
+ std::string* output) {
+ thrax::GrmManager::StringifyFst(fst);
return printer->operator()(*fst, output);
}
@@ -70,19 +70,19 @@
std::set_new_handler(FailedNewHandler);
SetFlags(argv[0], &argc, &argv, true);
- GrmManager grm;
+ thrax::GrmManager grm;
CHECK(grm.LoadArchive(FLAGS_far));
Compiler* compiler = NULL;
SymbolTable* input_symtab = NULL;
if (FLAGS_input_mode == "byte") {
- compiler = new Compiler(Compiler::BYTE);
+ compiler = new Compiler(fst::StringTokenType::BYTE);
} else if (FLAGS_input_mode == "utf8") {
- compiler = new Compiler(Compiler::UTF8);
+ compiler = new Compiler(fst::StringTokenType::UTF8);
} else {
input_symtab = SymbolTable::ReadText(FLAGS_input_mode);
CHECK(input_symtab);
- compiler = new Compiler(Compiler::SYMBOL, input_symtab);
+ compiler = new Compiler(fst::StringTokenType::SYMBOL, input_symtab);
}
Printer* printer = NULL;
@@ -94,33 +94,33 @@
// set the input's symbol tables appropriately.
if (FLAGS_output_mode == "byte") {
type = BYTE;
- printer = new Printer(Printer::BYTE);
+ printer = new Printer(fst::StringTokenType::BYTE);
} else if (FLAGS_output_mode == "utf8") {
type = UTF8;
- printer = new Printer(Printer::UTF8);
+ printer = new Printer(fst::StringTokenType::UTF8);
} else {
type = SYMBOL;
output_symtab = SymbolTable::ReadText(FLAGS_output_mode);
CHECK(output_symtab);
- printer = new Printer(Printer::SYMBOL, output_symtab);
+ printer = new Printer(fst::StringTokenType::SYMBOL, output_symtab);
}
int exit_status = 0;
File* fp = OpenOrDie(FLAGS_testdata, "r");
- string line;
+ std::string line;
int linenum;
const fst::SymbolTable* byte_symtab = NULL;
const fst::SymbolTable* utf8_symtab = NULL;
for (InputBuffer ibuf(fp); ibuf.ReadLine(&line);
/* ReadLine() automatically increments */) {
- vector<string> fields = Split(line, FLAGS_separator.c_str());
+ std::vector<std::string> fields = thrax::StringSplit(line, FLAGS_separator.c_str());
if (fields.size() != 3) continue;
- string rule = fields[0];
- string input = fields[1];
- string output = fields[2];
+ std::string rule = fields[0];
+ std::string input = fields[1];
+ std::string output = fields[2];
if (rule.empty() || rule[0] == '#') continue;
Transducer input_fst, output_fst;
- string test_output;
+ std::string test_output;
const fst::Fst<StdArc>* fst = grm.GetFst(rule);
if (!fst) {
LOG(FATAL) << "grm.GetFst() must be non NULL: " << rule;
@@ -143,7 +143,7 @@
}
}
if (!compiler->operator()(input, &input_fst)) {
- cout << "Unable to parse input string: " << input << endl;
+ std::cout << "Unable to parse input string: " << input << std::endl;
exit_status = 1;
continue;
}
@@ -161,13 +161,13 @@
if (!grm.Rewrite(rule, input_fst, &output_fst) ||
!RewriteOutput(printer, &output_fst, &test_output) ||
test_output != output) {
- cout << "Match failed:\t" << line << endl;
- cout << "Actual:\t" << test_output << endl;
- cout << "Expected:\t" << output << endl;
+ std::cout << "Match failed:\t" << line << std::endl;
+ std::cout << "Actual:\t" << test_output << std::endl;
+ std::cout << "Expected:\t" << output << std::endl;
exit_status = 1;
}
}
- if (!exit_status) cout << "PASS" << endl;
+ if (!exit_status) std::cout << "PASS" << std::endl;
delete compiler;
delete printer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment