FST  openfst-1.8.1
OpenFst Library
pdtinfo-main.cc
Go to the documentation of this file.
1 // Copyright 2005-2020 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 a PDT such as number of states, arcs,
19 // and parentheses.
20 
21 #include <cstring>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #include <fst/flags.h>
28 #include <fst/types.h>
29 #include <fst/log.h>
31 #include <fst/util.h>
32 
33 DECLARE_string(pdt_parentheses);
34 
35 int pdtinfo_main(int argc, char **argv) {
36  namespace s = fst::script;
37  using fst::ReadLabelPairs;
39 
40  std::string usage = "Prints out information about a PDT.\n\n Usage: ";
41  usage += argv[0];
42  usage += " in.pdt\n";
43 
44  std::set_new_handler(FailedNewHandler);
45  SET_FLAGS(usage.c_str(), &argc, &argv, true);
46  if (argc > 2) {
47  ShowUsage();
48  return 1;
49  }
50 
51  const std::string in_name =
52  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
53 
54  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
55  if (!ifst) return 1;
56 
57  if (FST_FLAGS_pdt_parentheses.empty()) {
58  LOG(ERROR) << argv[0] << ": No PDT parenthesis label pairs provided";
59  return 1;
60  }
61 
62  std::vector<std::pair<int64, int64>> parens;
63  if (!ReadLabelPairs(FST_FLAGS_pdt_parentheses, &parens, false))
64  return 1;
65 
66  s::PrintPdtInfo(*ifst, parens);
67 
68  return 0;
69 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:123
DECLARE_string(pdt_parentheses)
#define LOG(type)
Definition: log.h:46
void FailedNewHandler()
Definition: compat.cc:22
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:222
bool ReadLabelPairs(const std::string &source, std::vector< std::pair< Label, Label >> *pairs, bool allow_negative=false)
Definition: util.h:372
void PrintPdtInfo(const FstClass &ifst, const std::vector< std::pair< int64, int64 >> &parens)
Definition: pdtscript.cc:112
int pdtinfo_main(int argc, char **argv)
Definition: pdtinfo-main.cc:35