FST  openfst-1.8.3
OpenFst Library
fstintersect-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 // Intersects two FSTs.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 
24 #include <fst/flags.h>
25 #include <fst/log.h>
26 #include <fst/compose.h>
27 #include <fst/intersect.h>
28 #include <fst/script/fst-class.h>
29 #include <fst/script/getters.h>
30 #include <fst/script/intersect.h>
31 
32 DECLARE_string(compose_filter);
33 DECLARE_bool(connect);
34 
35 int fstintersect_main(int argc, char **argv) {
36  namespace s = fst::script;
37  using fst::ComposeFilter;
41 
42  std::string usage = "Intersects two FSAs.\n\n Usage: ";
43  usage += argv[0];
44  usage += " in1.fst in2.fst [out.fst]\n";
45  usage += " Flags: connect\n";
46 
47  SET_FLAGS(usage.c_str(), &argc, &argv, true);
48  if (argc < 3 || argc > 4) {
49  ShowUsage();
50  return 1;
51  }
52 
53  const std::string in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
54  const std::string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
55  const std::string out_name =
56  argc > 3 && strcmp(argv[3], "-") != 0 ? argv[3] : "";
57 
58  if (in1_name.empty() && in2_name.empty()) {
59  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input";
60  return 1;
61  }
62 
63  std::unique_ptr<FstClass> ifst1(FstClass::Read(in1_name));
64  if (!ifst1) return 1;
65  std::unique_ptr<FstClass> ifst2(FstClass::Read(in2_name));
66  if (!ifst2) return 1;
67 
68  VectorFstClass ofst(ifst1->ArcType());
69 
70  ComposeFilter compose_filter;
71  if (!s::GetComposeFilter(FST_FLAGS_compose_filter,
72  &compose_filter)) {
73  LOG(ERROR) << argv[0] << ": Unknown or unsupported compose filter type: "
74  << FST_FLAGS_compose_filter;
75  return 1;
76  }
77 
78  const IntersectOptions opts(FST_FLAGS_connect, compose_filter);
79 
80  s::Intersect(*ifst1, *ifst2, &ofst, opts);
81 
82  return !ofst.Write(out_name);
83 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
int fstintersect_main(int argc, char **argv)
#define LOG(type)
Definition: log.h:53
void Intersect(const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, const IntersectOptions &opts=IntersectOptions())
Definition: intersect.h:150
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
DECLARE_bool(connect)
bool GetComposeFilter(std::string_view str, ComposeFilter *compose_filter)
Definition: getters.cc:77
DECLARE_string(compose_filter)
ComposeFilter
Definition: compose.h:953
ComposeOptions IntersectOptions
Definition: intersect.h:43