FST  openfst-1.8.3
OpenFst Library
fstreplace-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 // Performs the dynamic replacement of arcs in one FST with another FST,
19 // allowing for the definition of FSTs analogous to RTNs.
20 
21 #include <cstdint>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #include <fst/flags.h>
28 #include <fst/log.h>
29 #include <fst/replace-util.h>
30 #include <fst/script/fst-class.h>
31 #include <fst/script/getters.h>
32 #include <fst/script/replace.h>
33 #include <fst/script/script-impl.h>
34 
35 DECLARE_string(call_arc_labeling);
36 DECLARE_string(return_arc_labeling);
37 DECLARE_int64(return_label);
38 DECLARE_bool(epsilon_on_replace);
39 
40 int fstreplace_main(int argc, char **argv) {
41  namespace s = fst::script;
45 
46  std::string usage =
47  "Recursively replaces FST arcs with other FST(s).\n\n"
48  " Usage: ";
49  usage += argv[0];
50  usage += " root.fst rootlabel [rule1.fst label1 ...] [out.fst]\n";
51 
52  SET_FLAGS(usage.c_str(), &argc, &argv, true);
53  if (argc < 4) {
54  ShowUsage();
55  return 1;
56  }
57 
58  const std::string out_name = argc % 2 == 0 ? argv[argc - 1] : "";
59 
60  std::vector<std::pair<int64_t, std::unique_ptr<const FstClass>>> pairs;
61  for (auto i = 1; i < argc - 1; i += 2) {
62  std::unique_ptr<const FstClass> ifst(FstClass::Read(argv[i]));
63  if (!ifst) return 1;
64  // Note that if the root label is beyond the range of the underlying FST's
65  // labels, truncation will occur.
66  const auto label = atoll(argv[i + 1]);
67  pairs.emplace_back(label, std::move(ifst));
68  }
69 
70  ReplaceLabelType call_label_type;
71  if (!s::GetReplaceLabelType(FST_FLAGS_call_arc_labeling,
72  FST_FLAGS_epsilon_on_replace,
73  &call_label_type)) {
74  LOG(ERROR) << argv[0] << ": Unknown or unsupported call arc replace "
75  << "label type: " << FST_FLAGS_call_arc_labeling;
76  }
77  ReplaceLabelType return_label_type;
78  if (!s::GetReplaceLabelType(FST_FLAGS_return_arc_labeling,
79  FST_FLAGS_epsilon_on_replace,
80  &return_label_type)) {
81  LOG(ERROR) << argv[0] << ": Unknown or unsupported return arc replace "
82  << "label type: " << FST_FLAGS_return_arc_labeling;
83  }
84  if (pairs.empty()) {
85  LOG(ERROR) << argv[0] << "At least one replace pair must be provided.";
86  return 1;
87  }
88  const auto root = pairs.front().first;
89  const s::ReplaceOptions opts(root, call_label_type, return_label_type,
90  FST_FLAGS_return_label);
91 
92  VectorFstClass ofst(pairs.back().second->ArcType());
93  s::Replace(s::BorrowPairs(pairs), &ofst, opts);
94 
95  return !ofst.Write(out_name);
96 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
DECLARE_string(call_arc_labeling)
DECLARE_int64(return_label)
void Replace(const std::vector< std::pair< int64_t, const FstClass * >> &pairs, MutableFstClass *ofst, std::vector< std::pair< int64_t, int64_t >> *parens, int64_t root, PdtParserType parser_type, int64_t start_paren_labels, const std::string &left_paren_prefix, const std::string &right_paren_prefix)
Definition: pdtscript.cc:75
ReplaceLabelType
Definition: replace-util.h:48
#define LOG(type)
Definition: log.h:53
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
bool GetReplaceLabelType(std::string_view str, bool epsilon_on_replace, ReplaceLabelType *rlt)
Definition: getters.cc:203
int fstreplace_main(int argc, char **argv)
std::vector< std::pair< int64_t, const FstClass * > > BorrowPairs(const std::vector< std::pair< int64_t, std::unique_ptr< const FstClass >>> &pairs)
Definition: script-impl.h:229
DECLARE_bool(epsilon_on_replace)