FST  openfst-1.8.3
OpenFst Library
mpdtcompose-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 // Composes an MPDT and 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>
34 #include <fst/script/arg-packs.h>
35 #include <fst/script/fst-class.h>
36 
37 DECLARE_string(mpdt_parentheses);
38 DECLARE_bool(left_mpdt);
39 DECLARE_bool(connect);
40 DECLARE_string(compose_filter);
41 
42 int mpdtcompose_main(int argc, char **argv) {
43  namespace s = fst::script;
49 
50  std::string usage = "Compose an MPDT and an FST.\n\n Usage: ";
51  usage += argv[0];
52  usage += " in.pdt in.fst [out.mpdt]\n";
53  usage += " in.fst in.pdt [out.mpdt]\n";
54 
55  SET_FLAGS(usage.c_str(), &argc, &argv, true);
56  if (argc < 3 || argc > 4) {
57  ShowUsage();
58  return 1;
59  }
60 
61  const std::string in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
62  const std::string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
63  const std::string out_name =
64  argc > 3 && strcmp(argv[3], "-") != 0 ? argv[3] : "";
65 
66  if (in1_name.empty() && in2_name.empty()) {
67  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input.";
68  return 1;
69  }
70 
71  std::unique_ptr<FstClass> ifst1(FstClass::Read(in1_name));
72  if (!ifst1) return 1;
73  std::unique_ptr<FstClass> ifst2(FstClass::Read(in2_name));
74  if (!ifst2) return 1;
75 
76  if (FST_FLAGS_mpdt_parentheses.empty()) {
77  LOG(ERROR) << argv[0] << ": No MPDT parenthesis label pairs provided";
78  return 1;
79  }
80 
81  std::vector<std::pair<int64_t, int64_t>> parens;
82  std::vector<int64_t> assignments;
83  if (!ReadLabelTriples(FST_FLAGS_mpdt_parentheses, &parens,
84  &assignments)) {
85  return 1;
86  }
87 
88  VectorFstClass ofst(ifst1->ArcType());
89 
90  PdtComposeFilter compose_filter;
91  if (!s::GetPdtComposeFilter(FST_FLAGS_compose_filter,
92  &compose_filter)) {
93  LOG(ERROR) << argv[0] << ": Unknown or unsupported compose filter type: "
94  << FST_FLAGS_compose_filter;
95  return 1;
96  }
97 
98  const MPdtComposeOptions opts(FST_FLAGS_connect, compose_filter);
99 
100  s::Compose(*ifst1, *ifst2, parens, assignments, &ofst, opts,
101  FST_FLAGS_left_mpdt);
102 
103  return !ofst.Write(out_name);
104 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
int mpdtcompose_main(int argc, char **argv)
DECLARE_string(mpdt_parentheses)
#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(left_mpdt)
bool GetPdtComposeFilter(std::string_view str, PdtComposeFilter *cf)
Definition: getters.cc:29
void Compose(const FstClass &ifst1, const FstClass &ifst2, const std::vector< std::pair< int64_t, int64_t >> &parens, const std::vector< int64_t > &assignments, MutableFstClass *ofst, const MPdtComposeOptions &copts, bool left_pdt)
Definition: mpdtscript.cc:41
PdtComposeFilter
Definition: compose.h:466