FST  openfst-1.8.4
OpenFst Library
pdtreverse-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 // Reverses a PDT.
19 
20 #include <cstdint>
21 #include <cstring>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #include <fst/flags.h>
28 #include <fst/log.h>
30 #include <fst/util.h>
31 #include <fst/script/fst-class.h>
32 
33 DECLARE_string(pdt_parentheses);
34 
35 int pdtreverse_main(int argc, char **argv) {
36  namespace s = fst::script;
37  using fst::ReadLabelPairs;
40 
41  std::string usage = "Reverse a PDT.\n\n Usage: ";
42  usage += argv[0];
43  usage += " in.pdt [out.fst]\n";
44 
45  SET_FLAGS(usage.c_str(), &argc, &argv, true);
46  if (argc > 3) {
47  ShowUsage();
48  return 1;
49  }
50 
51  const std::string in_name =
52  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
53  const std::string out_name =
54  (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : "";
55 
56  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
57  if (!ifst) return 1;
58 
59  if (FST_FLAGS_pdt_parentheses.empty()) {
60  LOG(ERROR) << argv[0] << ": No PDT parenthesis label pairs provided";
61  return 1;
62  }
63 
64  std::vector<std::pair<int64_t, int64_t>> parens;
65  if (!ReadLabelPairs(FST_FLAGS_pdt_parentheses, &parens)) return 1;
66 
67  VectorFstClass ofst(ifst->ArcType());
68 
69  s::Reverse(*ifst, parens, &ofst);
70 
71  return !ofst.Write(out_name);
72 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
void Reverse(const FstClass &ifst, const std::vector< std::pair< int64_t, int64_t >> &parens, std::vector< int64_t > *assignments, MutableFstClass *ofst)
Definition: mpdtscript.cc:69
#define LOG(type)
Definition: log.h:53
DECLARE_string(pdt_parentheses)
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
int pdtreverse_main(int argc, char **argv)
bool ReadLabelPairs(std::string_view source, std::vector< std::pair< Label, Label >> *pairs)
Definition: util.h:423