FST  openfst-1.8.3
OpenFst Library
fstprint-main.cc
Go to the documentation of this file.
1 // Copyright 2005-2024 Google LLC
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 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Prints out binary FSTs in simple text format used by AT&T.
19 
20 #include <cstring>
21 #include <iostream>
22 #include <memory>
23 #include <ostream>
24 #include <string>
25 
26 #include <fst/flags.h>
27 #include <fst/log.h>
28 #include <fstream>
29 #include <fst/symbol-table.h>
30 #include <fst/script/fst-class.h>
31 #include <fst/script/print.h>
32 
33 DECLARE_bool(acceptor);
34 DECLARE_string(isymbols);
35 DECLARE_string(osymbols);
36 DECLARE_string(ssymbols);
37 DECLARE_bool(numeric);
38 DECLARE_string(save_isymbols);
39 DECLARE_string(save_osymbols);
40 DECLARE_bool(show_weight_one);
41 DECLARE_string(missing_symbol);
42 
43 int fstprint_main(int argc, char **argv) {
44  namespace s = fst::script;
45  using fst::SymbolTable;
47 
48  std::string usage =
49  "Prints out binary FSTs in simple text format.\n\n Usage: ";
50  usage += argv[0];
51  usage += " [binary.fst [text.fst]]\n";
52 
53  SET_FLAGS(usage.c_str(), &argc, &argv, true);
54  if (argc > 3) {
55  ShowUsage();
56  return 1;
57  }
58 
59  const std::string in_name =
60  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
61  const std::string out_name =
62  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
63 
64  std::unique_ptr<FstClass> fst(FstClass::Read(in_name));
65  if (!fst) return 1;
66 
67  std::string dest = "standard output";
68  std::ofstream fstrm;
69  if (argc == 3) {
70  fstrm.open(argv[2]);
71  if (!fstrm) {
72  LOG(ERROR) << argv[0] << ": Open failed, file = " << argv[2];
73  return 1;
74  }
75  dest = argv[2];
76  }
77  std::ostream &ostrm = fstrm.is_open() ? fstrm : std::cout;
78  ostrm.precision(9);
79 
80  std::unique_ptr<const SymbolTable> isyms;
81  if (!FST_FLAGS_isymbols.empty() && !FST_FLAGS_numeric) {
82  isyms.reset(
83  SymbolTable::ReadText(FST_FLAGS_isymbols,
84  FST_FLAGS_fst_field_separator));
85  if (!isyms) return 1;
86  }
87 
88  std::unique_ptr<const SymbolTable> osyms;
89  if (!FST_FLAGS_osymbols.empty() && !FST_FLAGS_numeric) {
90  osyms.reset(
91  SymbolTable::ReadText(FST_FLAGS_osymbols,
92  FST_FLAGS_fst_field_separator));
93  if (!osyms) return 1;
94  }
95 
96  std::unique_ptr<const SymbolTable> ssyms;
97  if (!FST_FLAGS_ssymbols.empty() && !FST_FLAGS_numeric) {
98  ssyms.reset(
99  SymbolTable::ReadText(FST_FLAGS_ssymbols,
100  FST_FLAGS_fst_field_separator));
101  if (!ssyms) return 1;
102  }
103 
104  if (!isyms && !FST_FLAGS_numeric && fst->InputSymbols()) {
105  isyms.reset(fst->InputSymbols()->Copy());
106  }
107 
108  if (!osyms && !FST_FLAGS_numeric && fst->OutputSymbols()) {
109  osyms.reset(fst->OutputSymbols()->Copy());
110  }
111 
112  s::Print(*fst, ostrm, dest, isyms.get(), osyms.get(), ssyms.get(),
113  FST_FLAGS_acceptor, FST_FLAGS_show_weight_one,
114  FST_FLAGS_missing_symbol);
115 
116  if (isyms && !FST_FLAGS_save_isymbols.empty()) {
117  if (!isyms->WriteText(FST_FLAGS_save_isymbols)) return 1;
118  }
119 
120  if (osyms && !FST_FLAGS_save_osymbols.empty()) {
121  if (!osyms->WriteText(FST_FLAGS_save_osymbols)) return 1;
122  }
123 
124  return 0;
125 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
#define LOG(type)
Definition: log.h:53
DECLARE_string(isymbols)
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
void Print(FstPrintArgs *args)
Definition: print.h:53
int fstprint_main(int argc, char **argv)
DECLARE_bool(acceptor)