GRM-SFST  sfst-1.2.1
OpenGrm SFst Library
sfsttrim.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 // Removes useless states and transitions in stochastic automata.
15 
16 #include <cstring>
17 #include <string>
18 
19 #include <fst/flags.h>
20 #include <fst/log.h>
21 #include <fst/fst-decl.h>
22 #include <fst/fst.h>
23 #include <fst/mutable-fst.h>
24 #include <fst/properties.h>
25 #include <sfst/trim.h>
26 
27 DEFINE_int64(phi_label, fst::kNoLabel,
28  "Specifies failure label (default: none)");
29 DEFINE_bool(phi_trim, true,
30  "Removes inaccessible transitions due to failure labels");
31 DEFINE_bool(weight_trim, false,
32  "Removes ApproxZero() weight transitions");
33 DEFINE_bool(sum_weight_trim, false,
34  "Removes ApproxZero() weight transitions wrt the phi-summed SFST");
35 DEFINE_bool(include_phi, false,
36  "Include phi transitions when weight trimming");
37 DEFINE_bool(connect, true,
38  "Removes inaccessible/non-accessible states treating"
39  " failure labels as regular labels");
40 DEFINE_string(trim_type, "needed_final", "Trim type, one of: "
41  "\"needed_trim\", \"needed_final\", "
42  "\"needed_nonfinal");
43 DEFINE_double(weight, 99.0, "Weight threshold");
44 
45 int main(int argc, char **argv) {
46  namespace f = fst;
47  std::string usage = "Removes useless states and transitions in stochastic ";
48  usage += " automata.\n\n Usage: ";
49  usage += argv[0];
50  usage += " [in.fst [out.fst]]\n";
51 
52  SET_FLAGS(usage.c_str(), &argc, &argv, true);
53  if (argc > 3) {
54  ShowUsage();
55  return 1;
56  }
57 
58  std::string in_name =
59  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
60  std::string out_name = argc > 2 ? argv[2] : "";
61 
62  f::StdMutableFst *fst = f::StdMutableFst::Read(in_name, true);
63  if (!fst) return 1;
64 
65  sfst::TrimType trim_type;
66  if (FST_FLAGS_trim_type == "needed_trim") {
67  trim_type = sfst::TRIM_NEEDED_TRIM;
68  } else if (FST_FLAGS_trim_type == "needed_final") {
69  trim_type = sfst::TRIM_NEEDED_FINAL;
70  } else if (FST_FLAGS_trim_type == "needed_nonfinal") {
71  trim_type = sfst::TRIM_NEEDED_NONFINAL;
72  } else {
73  LOG(ERROR) << argv[0]
74  << ": Bad trim type: " << FST_FLAGS_trim_type;
75  return 1;
76  }
77 
78  sfst::Trimmer<f::StdArc> trim(fst, FST_FLAGS_phi_label, trim_type);
79 
80  if (FST_FLAGS_phi_trim) trim.PhiTrim();
81 
82  if (FST_FLAGS_weight_trim)
83  trim.WeightTrim(FST_FLAGS_include_phi,
84  FST_FLAGS_weight);
85 
86  if (FST_FLAGS_sum_weight_trim)
87  trim.SumWeightTrim(FST_FLAGS_include_phi,
88  FST_FLAGS_weight);
89 
90  if (FST_FLAGS_connect) trim.Connect();
91 
92  trim.Finalize();
93 
94  if (fst->Properties(f::kError, false)) {
95  LOG(ERROR) << argv[0] << ": trimming failed";
96  return 2;
97  }
98 
99  if (!fst->Write(out_name))
100  return 1;
101 
102  return 0;
103 }
DEFINE_bool(phi_trim, true,"Removes inaccessible transitions due to failure labels")
void Connect()
Definition: trim.h:476
void PhiTrim()
Definition: trim.h:549
Definition: perplexity.h:32
int main(int argc, char **argv)
Definition: sfsttrim.cc:45
void Finalize()
Definition: trim.h:648
void WeightTrim(bool include_phi, Weight approx_zero=ApproxZeroWeight())
Definition: trim.h:596
void SumWeightTrim(bool include_phi, Weight approx_zero=ApproxZeroWeight())
Definition: trim.h:620
TrimType
Definition: trim.h:431
DEFINE_string(trim_type,"needed_final","Trim type, one of: ""\"needed_trim\", \"needed_final\", ""\"needed_nonfinal")
DEFINE_double(weight, 99.0,"Weight threshold")
DEFINE_int64(phi_label, fst::kNoLabel,"Specifies failure label (default: none)")