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