FST  openfst-1.8.3
OpenFst Library
fstcompile-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 // Creates binary FSTs from simple text format used by AT&T.
19 
20 #include <cstring>
21 #include <iostream>
22 #include <istream>
23 #include <memory>
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/compile.h>
31 
32 DECLARE_bool(acceptor);
33 DECLARE_string(arc_type);
34 DECLARE_string(fst_type);
35 DECLARE_string(isymbols);
36 DECLARE_string(osymbols);
37 DECLARE_string(ssymbols);
38 DECLARE_bool(keep_isymbols);
39 DECLARE_bool(keep_osymbols);
40 DECLARE_bool(keep_state_numbering);
41 
42 int fstcompile_main(int argc, char **argv) {
43  namespace s = fst::script;
44  using fst::SymbolTable;
45 
46  std::string usage =
47  "Creates binary FSTs from simple text format.\n\n Usage: ";
48  usage += argv[0];
49  usage += " [text.fst [binary.fst]]\n";
50 
51  SET_FLAGS(usage.c_str(), &argc, &argv, true);
52  if (argc > 3) {
53  ShowUsage();
54  return 1;
55  }
56 
57  std::string source = "standard input";
58  std::ifstream fstrm;
59  if (argc > 1 && strcmp(argv[1], "-") != 0) {
60  fstrm.open(argv[1]);
61  if (!fstrm) {
62  LOG(ERROR) << argv[0] << ": Open failed, file = " << argv[1];
63  return 1;
64  }
65  source = argv[1];
66  }
67  std::istream &istrm = fstrm.is_open() ? fstrm : std::cin;
68 
69  std::unique_ptr<const SymbolTable> isyms;
70  if (!FST_FLAGS_isymbols.empty()) {
71  isyms.reset(
72  SymbolTable::ReadText(FST_FLAGS_isymbols,
73  FST_FLAGS_fst_field_separator));
74  if (!isyms) return 1;
75  }
76 
77  std::unique_ptr<const SymbolTable> osyms;
78  if (!FST_FLAGS_osymbols.empty()) {
79  osyms.reset(
80  SymbolTable::ReadText(FST_FLAGS_osymbols,
81  FST_FLAGS_fst_field_separator));
82  if (!osyms) return 1;
83  }
84 
85  std::unique_ptr<const SymbolTable> ssyms;
86  if (!FST_FLAGS_ssymbols.empty()) {
87  ssyms.reset(
88  SymbolTable::ReadText(FST_FLAGS_ssymbols,
89  FST_FLAGS_fst_field_separator));
90  if (!ssyms) return 1;
91  }
92 
93  const std::string dest = argc > 2 && strcmp(argv[2], "-") != 0 ? argv[2] : "";
94 
95  s::Compile(istrm, source, dest, FST_FLAGS_fst_type,
96  FST_FLAGS_arc_type, isyms.get(), osyms.get(),
97  ssyms.get(), FST_FLAGS_acceptor,
98  FST_FLAGS_keep_isymbols,
99  FST_FLAGS_keep_osymbols,
100  FST_FLAGS_keep_state_numbering);
101 
102  return 0;
103 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
int fstcompile_main(int argc, char **argv)
DECLARE_bool(acceptor)
#define LOG(type)
Definition: log.h:53
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
void Compile(std::istream &istrm, const std::string &source, const std::string &dest, const std::string &fst_type, const std::string &arc_type, const SymbolTable *isyms, const SymbolTable *osyms, const SymbolTable *ssyms, bool accep, bool ikeep, bool okeep, bool nkeep)
Definition: compile.cc:32
DECLARE_string(arc_type)