FST  openfst-1.8.3
OpenFst Library
pdtreplace-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 // Converts an RTN represented by FSTs and non-terminal labels into a PDT.
19 
20 #include <cstdint>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include <fst/flags.h>
27 #include <fst/log.h>
31 #include <fst/util.h>
32 #include <fst/script/fst-class.h>
33 #include <fst/script/script-impl.h>
34 
35 DECLARE_string(pdt_parentheses);
36 DECLARE_string(pdt_parser_type);
37 DECLARE_int64(start_paren_labels);
38 DECLARE_string(left_paren_prefix);
39 DECLARE_string(right_paren_prefix);
40 
41 int pdtreplace_main(int argc, char **argv) {
42  namespace s = fst::script;
43  using fst::PdtParserType;
47 
48  std::string usage = "Converts an RTN represented by FSTs";
49  usage += " and non-terminal labels into PDT.\n\n Usage: ";
50  usage += argv[0];
51  usage += " root.fst rootlabel [rule1.fst label1 ...] [out.fst]\n";
52 
53  SET_FLAGS(usage.c_str(), &argc, &argv, true);
54  if (argc < 4) {
55  ShowUsage();
56  return 1;
57  }
58 
59  const std::string out_name = argc % 2 == 0 ? argv[argc - 1] : "";
60 
61  PdtParserType parser_type;
62  if (!s::GetPdtParserType(FST_FLAGS_pdt_parser_type,
63  &parser_type)) {
64  LOG(ERROR) << argv[0] << ": Unknown PDT parser type: "
65  << FST_FLAGS_pdt_parser_type;
66  return 1;
67  }
68 
69  std::vector<std::pair<int64_t, std::unique_ptr<const FstClass>>> pairs;
70  for (auto i = 1; i < argc - 1; i += 2) {
71  std::unique_ptr<const FstClass> ifst(FstClass::Read(argv[i]));
72  if (!ifst) return 1;
73  // Note that if the root label is beyond the range of the underlying FST's
74  // labels, truncation will occur.
75  const auto label = atoll(argv[i + 1]);
76  pairs.emplace_back(label, std::move(ifst));
77  }
78 
79  if (pairs.empty()) {
80  LOG(ERROR) << argv[0] << "At least one replace pair must be provided.";
81  return 1;
82  }
83  const auto root = pairs.front().first;
84  VectorFstClass ofst(pairs.back().second->ArcType());
85  std::vector<std::pair<int64_t, int64_t>> parens;
86  s::Replace(s::BorrowPairs(pairs), &ofst, &parens, root, parser_type,
87  FST_FLAGS_start_paren_labels,
88  FST_FLAGS_left_paren_prefix,
89  FST_FLAGS_right_paren_prefix);
90 
91  if (!FST_FLAGS_pdt_parentheses.empty() &&
92  !WriteLabelPairs(FST_FLAGS_pdt_parentheses, parens)) {
93  return 1;
94  }
95 
96  return !ofst.Write(out_name);
97 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
bool GetPdtParserType(std::string_view str, PdtParserType *pt)
Definition: getters.cc:42
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
bool WriteLabelPairs(std::string_view source, const std::vector< std::pair< Label, Label >> &pairs)
Definition: util.h:428
#define LOG(type)
Definition: log.h:53
int pdtreplace_main(int argc, char **argv)
PdtParserType
Definition: replace.h:69
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
DECLARE_string(pdt_parentheses)
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_int64(start_paren_labels)