FST  openfst-1.7.2
OpenFst Library
pdtexpand.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Expands a (bounded-stack) PDT as an FST.
5 
6 #include <cstring>
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include <fst/flags.h>
13 #include <fst/log.h>
14 
16 #include <fst/util.h>
17 
18 DEFINE_string(pdt_parentheses, "", "PDT parenthesis label pairs");
19 DEFINE_bool(connect, true, "Trim output?");
20 DEFINE_bool(keep_parentheses, false, "Keep PDT parentheses in result?");
21 DEFINE_string(weight, "", "Weight threshold");
22 
23 int main(int argc, char **argv) {
24  namespace s = fst::script;
28  using fst::ReadLabelPairs;
29 
30  string usage = "Expand a (bounded-stack) PDT as an FST.\n\n Usage: ";
31  usage += argv[0];
32  usage += " in.pdt [out.fst]\n";
33 
34  std::set_new_handler(FailedNewHandler);
35  SET_FLAGS(usage.c_str(), &argc, &argv, true);
36  if (argc > 3) {
37  ShowUsage();
38  return 1;
39  }
40 
41  const string in_name =
42  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
43  const string out_name = argc > 2 ? argv[2] : "";
44 
45  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
46  if (!ifst) return 1;
47 
48  if (FLAGS_pdt_parentheses.empty()) {
49  LOG(ERROR) << argv[0] << ": No PDT parenthesis label pairs provided";
50  return 1;
51  }
52 
53  std::vector<s::LabelPair> parens;
54  if (!ReadLabelPairs(FLAGS_pdt_parentheses, &parens, false)) return 1;
55 
56  const auto weight_threshold =
57  FLAGS_weight.empty() ? WeightClass::Zero(ifst->WeightType())
58  : WeightClass(ifst->WeightType(), FLAGS_weight);
59 
60  VectorFstClass ofst(ifst->ArcType());
61  s::PdtExpand(*ifst, parens, &ofst,
62  s::PdtExpandOptions(FLAGS_connect, FLAGS_keep_parentheses,
63  weight_threshold));
64 
65  ofst.Write(out_name);
66 
67  return 0;
68 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
bool ReadLabelPairs(const string &filename, std::vector< std::pair< Label, Label >> *pairs, bool allow_negative=false)
Definition: util.h:331
#define LOG(type)
Definition: log.h:48
DEFINE_string(pdt_parentheses,"","PDT parenthesis label pairs")
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
int main(int argc, char **argv)
Definition: pdtexpand.cc:23
void PdtExpand(const FstClass &ifst, const std::vector< LabelPair > &parens, MutableFstClass *ofst, const PdtExpandOptions &opts)
Definition: pdtscript.cc:35
DEFINE_bool(connect, true,"Trim output?")