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