FST  openfst-1.8.1
OpenFst Library
pdtcompose-main.cc
Go to the documentation of this file.
1 // Copyright 2005-2020 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 a PDT and an FST.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include <fst/flags.h>
27 #include <fst/types.h>
28 #include <fst/log.h>
31 #include <fst/util.h>
32 
33 DECLARE_string(pdt_parentheses);
34 DECLARE_bool(left_pdt);
35 DECLARE_bool(connect);
36 DECLARE_string(compose_filter);
37 
38 int pdtcompose_main(int argc, char **argv) {
39  namespace s = fst::script;
42  using fst::ReadLabelPairs;
45 
46  std::string usage = "Compose a PDT and an FST.\n\n Usage: ";
47  usage += argv[0];
48  usage += " in.pdt in.fst [out.pdt]\n";
49  usage += " in.fst in.pdt [out.pdt]\n";
50 
51  std::set_new_handler(FailedNewHandler);
52  SET_FLAGS(usage.c_str(), &argc, &argv, true);
53  if (argc < 3 || argc > 4) {
54  ShowUsage();
55  return 1;
56  }
57 
58  const std::string in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
59  const std::string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
60  const std::string out_name =
61  argc > 3 && strcmp(argv[3], "-") != 0 ? argv[3] : "";
62 
63  if (in1_name.empty() && in2_name.empty()) {
64  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input.";
65  return 1;
66  }
67 
68  std::unique_ptr<FstClass> ifst1(FstClass::Read(in1_name));
69  if (!ifst1) return 1;
70  std::unique_ptr<FstClass> ifst2(FstClass::Read(in2_name));
71  if (!ifst2) return 1;
72 
73  if (FST_FLAGS_pdt_parentheses.empty()) {
74  LOG(ERROR) << argv[0] << ": No PDT parenthesis label pairs provided";
75  return 1;
76  }
77 
78  std::vector<std::pair<int64, int64>> parens;
79  if (!ReadLabelPairs(FST_FLAGS_pdt_parentheses, &parens, false))
80  return 1;
81 
82  VectorFstClass ofst(ifst1->ArcType());
83 
84  PdtComposeFilter compose_filter;
85  if (!s::GetPdtComposeFilter(FST_FLAGS_compose_filter,
86  &compose_filter)) {
87  LOG(ERROR) << argv[0] << ": Unknown or unsupported compose filter type: "
88  << FST_FLAGS_compose_filter;
89  return 1;
90  }
91 
92  const PdtComposeOptions copts(FST_FLAGS_connect, compose_filter);
93 
94  s::PdtCompose(*ifst1, *ifst2, parens, &ofst, copts,
95  FST_FLAGS_left_pdt);
96 
97  return !ofst.Write(out_name);
98 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:123
DECLARE_string(pdt_parentheses)
PdtComposeFilter
Definition: compose.h:451
#define LOG(type)
Definition: log.h:46
DECLARE_bool(left_pdt)
bool GetPdtComposeFilter(const std::string &str, PdtComposeFilter *cf)
Definition: getters.cc:23
void FailedNewHandler()
Definition: compat.cc:22
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:222
void PdtCompose(const FstClass &ifst1, const FstClass &ifst2, const std::vector< std::pair< int64, int64 >> &parens, MutableFstClass *ofst, const PdtComposeOptions &copts, bool left_pdt)
Definition: pdtscript.cc:41
bool ReadLabelPairs(const std::string &source, std::vector< std::pair< Label, Label >> *pairs, bool allow_negative=false)
Definition: util.h:372
int pdtcompose_main(int argc, char **argv)