FST  openfst-1.8.4
OpenFst Library
mpdtreverse-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 an MPDT.
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>
31 #include <fst/script/fst-class.h>
32 
33 DECLARE_string(mpdt_parentheses);
34 DECLARE_string(mpdt_new_parentheses);
35 
36 int mpdtreverse_main(int argc, char **argv) {
37  namespace s = fst::script;
42 
43  std::string usage = "Reverse an MPDT.\n\n Usage: ";
44  usage += argv[0];
45  usage += " in.pdt [out.fst]\n";
46 
47  SET_FLAGS(usage.c_str(), &argc, &argv, true);
48  if (argc > 3) {
49  ShowUsage();
50  return 1;
51  }
52 
53  const std::string in_name =
54  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
55  const std::string out_name =
56  (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : "";
57 
58  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
59  if (!ifst) return 1;
60 
61  if (FST_FLAGS_mpdt_parentheses.empty()) {
62  LOG(ERROR) << argv[0] << ": No MPDT parenthesis label pairs provided";
63  return 1;
64  }
65 
66  if (FST_FLAGS_mpdt_new_parentheses.empty()) {
67  LOG(ERROR) << argv[0] << ": No MPDT output parenthesis label file provided";
68  return 1;
69  }
70 
71  std::vector<std::pair<int64_t, int64_t>> parens;
72  std::vector<int64_t> assignments;
73  if (!ReadLabelTriples(FST_FLAGS_mpdt_parentheses, &parens,
74  &assignments)) {
75  return 1;
76  }
77 
78  VectorFstClass ofst(ifst->ArcType());
79 
80  s::Reverse(*ifst, parens, &assignments, &ofst);
81 
82  if (!ofst.Write(out_name)) return 1;
83 
84  return !WriteLabelTriples(FST_FLAGS_mpdt_new_parentheses, parens,
85  assignments);
86 }
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
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
bool ReadLabelTriples(const std::string &source, std::vector< std::pair< Label, Label >> *pairs, std::vector< Label > *assignments)
bool WriteLabelTriples(const std::string &source, const std::vector< std::pair< Label, Label >> &pairs, const std::vector< Label > &assignments)
DECLARE_string(mpdt_parentheses)
int mpdtreverse_main(int argc, char **argv)