fstprint.cc revision f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2
1// fstprint.cc 2 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14// 15// Copyright 2005-2010 Google, Inc. 16// Author: riley@google.com (Michael Riley) 17// Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass 18// 19// \file 20// Prints out binary FSTs in simple text format used by AT&T 21// (see http://www.research.att.com/projects/mohri/fsm/doc4/fsm.5.html). 22 23#include <fst/script/print.h> 24 25DEFINE_bool(acceptor, false, "Input in acceptor format"); 26DEFINE_string(isymbols, "", "Input label symbol table"); 27DEFINE_string(osymbols, "", "Output label symbol table"); 28DEFINE_string(ssymbols, "", "State label symbol table"); 29DEFINE_bool(numeric, false, "Print numeric labels"); 30DEFINE_string(save_isymbols, "", "Save input symbol table to file"); 31DEFINE_string(save_osymbols, "", "Save output symbol table to file"); 32DEFINE_bool(show_weight_one, false, 33 "Print/draw arc weights and final weights equal to Weight::One()"); 34DEFINE_bool(allow_negative_labels, false, 35 "Allow negative labels (not recommended; may cause conflicts)"); 36 37int main(int argc, char **argv) { 38 namespace s = fst::script; 39 using fst::ostream; 40 using fst::SymbolTable; 41 42 string usage = "Prints out binary FSTs in simple text format.\n\n Usage: "; 43 usage += argv[0]; 44 usage += " [binary.fst [text.fst]]\n"; 45 46 std::set_new_handler(FailedNewHandler); 47 SetFlags(usage.c_str(), &argc, &argv, true); 48 if (argc > 3) { 49 ShowUsage(); 50 return 1; 51 } 52 53 string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : ""; 54 string out_name = argc > 2 ? argv[2] : ""; 55 56 s::FstClass *fst = s::FstClass::Read(in_name); 57 if (!fst) return 1; 58 59 ostream *ostrm = &std::cout; 60 string dest = "standard output"; 61 if (argc == 3) { 62 dest = argv[2]; 63 ostrm = new fst::ofstream(argv[2]); 64 if (!*ostrm) { 65 LOG(ERROR) << argv[0] << ": Open failed, file = " << argv[2]; 66 return 1; 67 } 68 } 69 ostrm->precision(9); 70 71 const SymbolTable *isyms = 0, *osyms = 0, *ssyms = 0; 72 73 if (!FLAGS_isymbols.empty() && !FLAGS_numeric) { 74 isyms = SymbolTable::ReadText(FLAGS_isymbols, FLAGS_allow_negative_labels); 75 if (!isyms) exit(1); 76 } 77 78 if (!FLAGS_osymbols.empty() && !FLAGS_numeric) { 79 osyms = SymbolTable::ReadText(FLAGS_osymbols, FLAGS_allow_negative_labels); 80 if (!osyms) exit(1); 81 } 82 83 if (!FLAGS_ssymbols.empty() && !FLAGS_numeric) { 84 ssyms = SymbolTable::ReadText(FLAGS_ssymbols); 85 if (!ssyms) exit(1); 86 } 87 88 if (!isyms && !FLAGS_numeric) 89 isyms = fst->InputSymbols(); 90 if (!osyms && !FLAGS_numeric) 91 osyms = fst->OutputSymbols(); 92 93 s::PrintFst(*fst, *ostrm, dest, isyms, osyms, ssyms, 94 FLAGS_acceptor, FLAGS_show_weight_one); 95 96 if (isyms && !FLAGS_save_isymbols.empty()) 97 isyms->WriteText(FLAGS_save_isymbols); 98 99 if (osyms && !FLAGS_save_osymbols.empty()) 100 osyms->WriteText(FLAGS_save_osymbols); 101 102 if (ostrm != &std::cout) 103 delete ostrm; 104 return 0; 105} 106