FST  openfst-1.8.3
OpenFst Library
fstinfo-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 various information about an FST such as number of states
19 // and arcs and property values (see properties.h).
20 
21 #include <cstring>
22 #include <ios>
23 #include <iostream>
24 #include <memory>
25 #include <string>
26 
27 #include <fst/flags.h>
28 #include <fst/log.h>
29 #include <fstream>
30 #include <fst/fst.h>
32 #include <fst/script/fst-class.h>
33 #include <fst/script/getters.h>
34 #include <fst/script/info-impl.h>
35 #include <fst/script/info.h>
36 
37 DECLARE_string(arc_filter);
38 DECLARE_string(info_type);
39 DECLARE_bool(test_properties);
40 DECLARE_bool(fst_verify);
41 
42 namespace {
43 // Prints info using only the header of the FST with path `in_name`.
44 // Returns true on success.
45 bool PrintHeaderInfo(const std::string &in_name) {
46  fst::FstHeader header;
47  if (in_name.empty()) {
48  if (!header.Read(std::cin, "stdin")) {
49  LOG(ERROR) << "fstinfo: Unable to read header from stdin";
50  return false;
51  }
52  } else {
53  std::ifstream istrm;
54  istrm.open(in_name, std::ios_base::in | std::ios_base::binary);
55  if (!istrm) {
56  LOG(ERROR) << "fstinfo: Unable to open " << in_name;
57  return false;
58  }
59  if (!header.Read(istrm, in_name)) {
60  LOG(ERROR) << "fstinfo: Unable to read header from " << in_name;
61  return false;
62  }
63  if (!istrm) {
64  LOG(ERROR) << "fstinfo: Unable to close " << in_name;
65  return false;
66  }
67  }
68 
69  fst::PrintHeader(std::cout, header);
70  return true;
71 }
72 } // namespace
73 
74 int fstinfo_main(int argc, char **argv) {
75  namespace s = fst::script;
77 
78  std::string usage = "Prints out information about an FST.\n\n Usage: ";
79  usage += argv[0];
80  usage += " [in.fst]\n";
81 
82  SET_FLAGS(usage.c_str(), &argc, &argv, true);
83  if (argc > 2) {
84  ShowUsage();
85  return 1;
86  }
87 
88  const std::string in_name =
89  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
90 
91  if (FST_FLAGS_info_type == "fast") {
92  if (!PrintHeaderInfo(in_name)) return 1;
93  } else {
94  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
95  if (!ifst) return 1;
96 
97  s::ArcFilterType arc_filter;
98  if (!s::GetArcFilterType(FST_FLAGS_arc_filter, &arc_filter)) {
99  LOG(ERROR) << argv[0] << ": Unknown or unsupported arc filter type "
100  << FST_FLAGS_arc_filter;
101  return 1;
102  }
103  s::Info(*ifst, FST_FLAGS_test_properties, arc_filter,
104  FST_FLAGS_info_type, FST_FLAGS_fst_verify);
105  }
106 
107  return 0;
108 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
bool Read(std::istream &strm, const std::string &source, bool rewind=false)
Definition: fst.cc:56
int fstinfo_main(int argc, char **argv)
Definition: fstinfo-main.cc:74
#define LOG(type)
Definition: log.h:53
void PrintHeader(std::ostream &ostrm, const FstHeader &header)
Definition: info-impl.cc:143
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
void Info(const std::vector< std::string > &sources, const std::string &arc_type, const std::string &begin_key, const std::string &end_key, bool list_fsts)
Definition: farscript.cc:145
DECLARE_bool(test_properties)
DECLARE_string(arc_filter)
bool GetArcFilterType(std::string_view str, ArcFilterType *arc_filter_type)
Definition: getters.cc:40