GRM-SFST  sfst-1.2.1
OpenGrm SFst Library
sfstintersect.cc
Go to the documentation of this file.
1 // Copyright 2018-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 // Intersects two canonical stochastic FSAs.
15 // The second FSA must be input-epsilon free (when phi_label != 0).
16 
17 #include <cstring>
18 #include <string>
19 
20 #include <fst/flags.h>
21 #include <fst/log.h>
22 #include <fst/fst.h>
23 #include <fst/vector-fst.h>
24 #include <sfst/intersect.h>
25 #include <sfst/trim.h>
26 
27 DEFINE_int64(phi_label, fst::kNoLabel,
28  "Specifies failure label (default: none)");
29 DEFINE_bool(trim, true,
30  "Removes useless states and transitions");
31 DEFINE_string(trim_type, "needed_final", "Trim type, one of: "
32  "\"needed_trim\", \"needed_final\", "
33  "\"needed_nonfinal");
34 
35 int main(int argc, char **argv) {
36  namespace f = fst;
37  std::string usage = "Intersects two canonical stochastic FSAs.\n\n Usage: ";
38  usage += argv[0];
39  usage += " in1.fst in2.fst [out.fst]\n";
40 
41  SET_FLAGS(usage.c_str(), &argc, &argv, true);
42  if (argc > 4) {
43  ShowUsage();
44  return 1;
45  }
46 
47  const std::string in1_name = strcmp(argv[1], "-") != 0 ? argv[1] : "";
48  const std::string in2_name =
49  (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : "";
50  const std::string out_name = argc > 3 ? argv[3] : "";
51 
52  if (in1_name.empty() && in2_name.empty()) {
53  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input";
54  return 1;
55  }
56 
57  f::StdFst *ifst1 = f::StdFst::Read(in1_name);
58  if (!ifst1) return 1;
59 
60  f::StdFst *ifst2 = f::StdFst::Read(in2_name);
61  if (!ifst2) return 1;
62 
63  sfst::TrimType trim_type;
64  if (FST_FLAGS_trim_type == "needed_trim") {
65  trim_type = sfst::TRIM_NEEDED_TRIM;
66  } else if (FST_FLAGS_trim_type == "needed_final") {
67  trim_type = sfst::TRIM_NEEDED_FINAL;
68  } else if (FST_FLAGS_trim_type == "needed_nonfinal") {
69  trim_type = sfst::TRIM_NEEDED_NONFINAL;
70  } else {
71  LOG(ERROR) << argv[0]
72  << ": Bad trim type: " << FST_FLAGS_trim_type;
73  return 1;
74  }
75 
76  f::StdVectorFst ofst;
77  if (!sfst::Intersect(*ifst1, *ifst2, &ofst, FST_FLAGS_phi_label,
78  FST_FLAGS_trim, trim_type))
79  return 1;
80 
81  if (!ofst.Write(out_name))
82  return 1;
83 
84  return 0;
85 }
Definition: perplexity.h:32
DEFINE_bool(trim, true,"Removes useless states and transitions")
bool Intersect(const fst::Fst< Arc > &ifst1, const fst::Fst< Arc > &ifst2, fst::MutableFst< Arc > *ofst, typename Arc::Label phi_label=fst::kNoLabel, bool trim=true, TrimType trim_type=TRIM_NEEDED_FINAL)
Definition: intersect.h:33
DEFINE_string(trim_type,"needed_final","Trim type, one of: ""\"needed_trim\", \"needed_final\", ""\"needed_nonfinal")
TrimType
Definition: trim.h:431
int main(int argc, char **argv)
DEFINE_int64(phi_label, fst::kNoLabel,"Specifies failure label (default: none)")