FST  openfst-1.8.3
OpenFst Library
mpdtexpand-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 // Expands a (bounded-stack) MPDT as an FST.
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>
32 #include <fst/script/arg-packs.h>
33 #include <fst/script/fst-class.h>
34 
35 DECLARE_string(mpdt_parentheses);
36 DECLARE_bool(connect);
37 DECLARE_bool(keep_parentheses);
38 
39 int mpdtexpand_main(int argc, char **argv) {
40  namespace s = fst::script;
45 
46  std::string usage = "Expand a (bounded-stack) MPDT as an FST.\n\n Usage: ";
47  usage += argv[0];
48  usage += " in.pdt [out.fst]\n";
49 
50  SET_FLAGS(usage.c_str(), &argc, &argv, true);
51  if (argc > 3) {
52  ShowUsage();
53  return 1;
54  }
55 
56  const std::string in_name =
57  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
58  const std::string out_name =
59  (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : "";
60 
61  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
62  if (!ifst) return 1;
63 
64  if (FST_FLAGS_mpdt_parentheses.empty()) {
65  LOG(ERROR) << argv[0] << ": No MPDT parenthesis label pairs provided";
66  return 1;
67  }
68 
69  std::vector<std::pair<int64_t, int64_t>> parens;
70  std::vector<int64_t> assignments;
71  if (!ReadLabelTriples(FST_FLAGS_mpdt_parentheses, &parens,
72  &assignments)) {
73  return 1;
74  }
75 
76  VectorFstClass ofst(ifst->ArcType());
77 
78  const MPdtExpandOptions opts(FST_FLAGS_connect,
79  FST_FLAGS_keep_parentheses);
80 
81  s::Expand(*ifst, parens, assignments, &ofst, opts);
82 
83  return !ofst.Write(out_name);
84 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
#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)
DECLARE_bool(connect)
void Expand(const FstClass &ifst, const std::vector< std::pair< int64_t, int64_t >> &parens, const std::vector< int64_t > &assignments, MutableFstClass *ofst, const MPdtExpandOptions &opts)
Definition: mpdtscript.cc:55
DECLARE_string(mpdt_parentheses)
int mpdtexpand_main(int argc, char **argv)