FST  openfst-1.8.2
OpenFst Library
fstdisambiguate-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 // Disambiguates an FST.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 
24 #include <fst/flags.h>
26 
27 DECLARE_double(delta);
28 DECLARE_int64(nstate);
29 DECLARE_string(weight);
30 DECLARE_int64(subsequential_label);
31 
32 int fstdisambiguate_main(int argc, char **argv) {
33  namespace s = fst::script;
37 
38  std::string usage = "Disambiguates an FST.\n\n Usage: ";
39  usage += argv[0];
40  usage += " [in.fst [out.fst]]\n";
41 
42  std::set_new_handler(FailedNewHandler);
43  SET_FLAGS(usage.c_str(), &argc, &argv, true);
44  if (argc > 3) {
45  ShowUsage();
46  return 1;
47  }
48 
49  const std::string in_name =
50  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
51  const std::string out_name =
52  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
53 
54  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
55  if (!ifst) return 1;
56 
57  VectorFstClass ofst(ifst->ArcType());
58 
59  const auto weight_threshold =
60  FST_FLAGS_weight.empty()
61  ? WeightClass::Zero(ifst->WeightType())
62  : WeightClass(ifst->WeightType(), FST_FLAGS_weight);
63 
64  const s::DisambiguateOptions opts(
65  FST_FLAGS_delta, weight_threshold, FST_FLAGS_nstate,
66  FST_FLAGS_subsequential_label);
67 
68  s::Disambiguate(*ifst, &ofst, opts);
69 
70  return !ofst.Write(out_name);
71 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:132
DECLARE_string(weight)
int fstdisambiguate_main(int argc, char **argv)
void FailedNewHandler()
Definition: compat.cc:26
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:224
DECLARE_int64(nstate)
void Disambiguate(const Fst< Arc > &ifst, MutableFst< Arc > *ofst, const DisambiguateOptions< Arc > &opts=DisambiguateOptions< Arc >())
Definition: disambiguate.h:573
DECLARE_double(delta)