FST  openfst-1.7.5
OpenFst Library
fstreplace-main.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 // Performs the dynamic replacement of arcs in one FST with another FST,
5 // allowing for the definition of FSTs analogous to RTNs.
6 
7 #include <cstring>
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 #include <fst/flags.h>
13 #include <fst/types.h>
14 #include <fst/script/getters.h>
15 #include <fst/script/replace.h>
16 
17 DECLARE_string(call_arc_labeling);
18 DECLARE_string(return_arc_labeling);
19 DECLARE_int64(return_label);
20 DECLARE_bool(epsilon_on_replace);
21 
22 namespace fst {
23 namespace script {
24 namespace {
25 
26 void Cleanup(std::vector<std::pair<int64, const FstClass *>> *pairs) {
27  for (const auto &pair : *pairs) delete pair.second;
28  pairs->clear();
29 }
30 
31 } // namespace
32 } // namespace script
33 } // namespace fst
34 
35 int fstreplace_main(int argc, char **argv) {
36  namespace s = fst::script;
40 
41  std::string usage =
42  "Recursively replaces FST arcs with other FST(s).\n\n"
43  " Usage: ";
44  usage += argv[0];
45  usage += " root.fst rootlabel [rule1.fst label1 ...] [out.fst]\n";
46 
47  std::set_new_handler(FailedNewHandler);
48  SET_FLAGS(usage.c_str(), &argc, &argv, true);
49  if (argc < 4) {
50  ShowUsage();
51  return 1;
52  }
53 
54  const std::string in_name = argv[1];
55  const std::string out_name = argc % 2 == 0 ? argv[argc - 1] : "";
56 
57  auto *ifst = FstClass::Read(in_name);
58  if (!ifst) return 1;
59 
60  std::vector<std::pair<int64, const FstClass *>> pairs;
61  // Note that if the root label is beyond the range of the underlying FST's
62  // labels, truncation will occur.
63  const auto root = atoll(argv[2]);
64  pairs.emplace_back(root, ifst);
65 
66  for (auto i = 3; i < argc - 1; i += 2) {
67  ifst = FstClass::Read(argv[i]);
68  if (!ifst) {
69  s::Cleanup(&pairs);
70  return 1;
71  }
72  // Note that if the root label is beyond the range of the underlying FST's
73  // labels, truncation will occur.
74  const auto label = atoll(argv[i + 1]);
75  pairs.emplace_back(label, ifst);
76  }
77 
78  ReplaceLabelType call_label_type;
79  if (!s::GetReplaceLabelType(FLAGS_call_arc_labeling, FLAGS_epsilon_on_replace,
80  &call_label_type)) {
81  LOG(ERROR) << argv[0] << ": Unknown or unsupported call arc replace "
82  << "label type: " << FLAGS_call_arc_labeling;
83  }
84  ReplaceLabelType return_label_type;
85  if (!s::GetReplaceLabelType(FLAGS_return_arc_labeling,
86  FLAGS_epsilon_on_replace, &return_label_type)) {
87  LOG(ERROR) << argv[0] << ": Unknown or unsupported return arc replace "
88  << "label type: " << FLAGS_return_arc_labeling;
89  }
90 
91  s::ReplaceOptions opts(root, call_label_type, return_label_type,
92  FLAGS_return_label);
93 
94  VectorFstClass ofst(ifst->ArcType());
95  s::Replace(pairs, &ofst, opts);
96  s::Cleanup(&pairs);
97 
98  return !ofst.Write(out_name);
99 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:121
DECLARE_string(call_arc_labeling)
DECLARE_int64(return_label)
ReplaceLabelType
Definition: replace-util.h:29
#define LOG(type)
Definition: log.h:46
void Replace(const std::vector< std::pair< typename Arc::Label, const Fst< Arc > * >> &ifst_array, MutableFst< Arc > *ofst, std::vector< std::pair< typename Arc::Label, typename Arc::Label >> *parens, const PdtReplaceOptions< Arc > &opts)
Definition: replace.h:786
static FstClass * Read(const std::string &source)
Definition: fst-class.cc:69
void FailedNewHandler()
Definition: compat.cc:22
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:209
int fstreplace_main(int argc, char **argv)
DECLARE_bool(epsilon_on_replace)
bool GetReplaceLabelType(const std::string &str, bool epsilon_on_replace, ReplaceLabelType *rlt)
Definition: getters.cc:123