Home | History | Annotate | Download | only in bin
      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 (at) google.com (Michael Riley)
     17 // Modified: jpr (at) 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 
     25 DEFINE_bool(acceptor, false, "Input in acceptor format");
     26 DEFINE_string(isymbols, "", "Input label symbol table");
     27 DEFINE_string(osymbols, "", "Output label symbol table");
     28 DEFINE_string(ssymbols, "", "State label symbol table");
     29 DEFINE_bool(numeric, false, "Print numeric labels");
     30 DEFINE_string(save_isymbols, "", "Save input symbol table to file");
     31 DEFINE_string(save_osymbols, "", "Save output symbol table to file");
     32 DEFINE_bool(show_weight_one, false,
     33             "Print/draw arc weights and final weights equal to Weight::One()");
     34 DEFINE_bool(allow_negative_labels, false,
     35             "Allow negative labels (not recommended; may cause conflicts)");
     36 
     37 int 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   SET_FLAGS(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 = &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   fst::SymbolTableTextOptions opts;
     74   opts.allow_negative = FLAGS_allow_negative_labels;
     75 
     76   if (!FLAGS_isymbols.empty() && !FLAGS_numeric) {
     77     isyms = SymbolTable::ReadText(FLAGS_isymbols, opts);
     78     if (!isyms) exit(1);
     79   }
     80 
     81   if (!FLAGS_osymbols.empty() && !FLAGS_numeric) {
     82     osyms = SymbolTable::ReadText(FLAGS_osymbols, opts);
     83     if (!osyms) exit(1);
     84   }
     85 
     86   if (!FLAGS_ssymbols.empty() && !FLAGS_numeric) {
     87     ssyms = SymbolTable::ReadText(FLAGS_ssymbols);
     88     if (!ssyms) exit(1);
     89   }
     90 
     91   if (!isyms && !FLAGS_numeric)
     92     isyms = fst->InputSymbols();
     93   if (!osyms && !FLAGS_numeric)
     94     osyms = fst->OutputSymbols();
     95 
     96   s::PrintFst(*fst, *ostrm, dest, isyms, osyms, ssyms,
     97               FLAGS_acceptor, FLAGS_show_weight_one);
     98 
     99   if (isyms && !FLAGS_save_isymbols.empty())
    100     isyms->WriteText(FLAGS_save_isymbols);
    101 
    102   if (osyms && !FLAGS_save_osymbols.empty())
    103     osyms->WriteText(FLAGS_save_osymbols);
    104 
    105   if (ostrm != &cout)
    106     delete ostrm;
    107   return 0;
    108 }
    109