FST  openfst-1.8.1
OpenFst Library
pdtreplace-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 // Converts an RTN represented by FSTs and non-terminal labels into a PDT.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include <fst/flags.h>
27 #include <fst/types.h>
30 #include <fst/util.h>
31 #include <fst/vector-fst.h>
32 
33 DECLARE_string(pdt_parentheses);
34 DECLARE_string(pdt_parser_type);
35 DECLARE_int64(start_paren_labels);
36 DECLARE_string(left_paren_prefix);
37 DECLARE_string(right_paren_prefix);
38 
39 int pdtreplace_main(int argc, char **argv) {
40  namespace s = fst::script;
41  using fst::PdtParserType;
45 
46  std::string usage = "Converts an RTN represented by FSTs";
47  usage += " and non-terminal labels into PDT.\n\n Usage: ";
48  usage += argv[0];
49  usage += " root.fst rootlabel [rule1.fst label1 ...] [out.fst]\n";
50 
51  std::set_new_handler(FailedNewHandler);
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  PdtParserType parser_type;
61  if (!s::GetPdtParserType(FST_FLAGS_pdt_parser_type,
62  &parser_type)) {
63  LOG(ERROR) << argv[0] << ": Unknown PDT parser type: "
64  << FST_FLAGS_pdt_parser_type;
65  return 1;
66  }
67 
68  std::vector<std::pair<int64, std::unique_ptr<const FstClass>>> pairs;
69  for (auto i = 1; i < argc - 1; i += 2) {
70  std::unique_ptr<const FstClass> ifst(FstClass::Read(argv[i]));
71  if (!ifst) return 1;
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, std::move(ifst));
76  }
77 
78  if (pairs.empty()) {
79  LOG(ERROR) << argv[0] << "At least one replace pair must be provided.";
80  return 1;
81  }
82  const auto root = pairs.front().first;
83  VectorFstClass ofst(pairs.back().second->ArcType());
84  std::vector<std::pair<int64, int64>> parens;
85  s::PdtReplace(s::BorrowPairs(pairs), &ofst, &parens, root, parser_type,
86  FST_FLAGS_start_paren_labels,
87  FST_FLAGS_left_paren_prefix,
88  FST_FLAGS_right_paren_prefix);
89 
90  if (!FST_FLAGS_pdt_parentheses.empty()) {
91  if (!WriteLabelPairs(FST_FLAGS_pdt_parentheses, parens))
92  return 1;
93  }
94 
95  return !ofst.Write(out_name);
96 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:123
PdtParserType
Definition: replace.h:59
#define LOG(type)
Definition: log.h:46
int pdtreplace_main(int argc, char **argv)
void PdtReplace(const std::vector< std::pair< int64, const FstClass * >> &pairs, MutableFstClass *ofst, std::vector< std::pair< int64, int64 >> *parens, int64 root, PdtParserType parser_type, int64 start_paren_labels, const std::string &left_paren_prefix, const std::string &right_paren_prefix)
Definition: pdtscript.cc:72
bool GetPdtParserType(const std::string &str, PdtParserType *pt)
Definition: getters.cc:36
std::vector< std::pair< int64, const FstClass * > > BorrowPairs(const std::vector< std::pair< int64, std::unique_ptr< const FstClass >>> &pairs)
Definition: script-impl.h:224
void FailedNewHandler()
Definition: compat.cc:22
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:222
DECLARE_string(pdt_parentheses)
DECLARE_int64(start_paren_labels)
bool WriteLabelPairs(const std::string &source, const std::vector< std::pair< Label, Label >> &pairs)
Definition: util.h:379