FST  openfst-1.8.2
OpenFst Library
fstdeterminize-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 // Determinizes an FST.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 
24 #include <fst/flags.h>
25 #include <fst/script/determinize.h>
26 #include <fst/script/getters.h>
27 
28 DECLARE_double(delta);
29 DECLARE_string(weight);
30 DECLARE_int64(nstate);
31 DECLARE_int64(subsequential_label);
32 DECLARE_string(det_type);
33 DECLARE_bool(increment_subsequential_label);
34 
35 int fstdeterminize_main(int argc, char **argv) {
36  namespace s = fst::script;
41 
42  std::string usage = "Determinizes an FST.\n\n Usage: ";
43  usage += argv[0];
44  usage += " [in.fst [out.fst]]\n";
45 
46  std::set_new_handler(FailedNewHandler);
47  SET_FLAGS(usage.c_str(), &argc, &argv, true);
48  if (argc > 3) {
49  ShowUsage();
50  return 1;
51  }
52 
53  DeterminizeType det_type;
54  if (!s::GetDeterminizeType(FST_FLAGS_det_type, &det_type)) {
55  LOG(ERROR) << argv[0] << ": Unknown or unsupported determinization type: "
56  << FST_FLAGS_det_type;
57  return 1;
58  }
59 
60  const std::string in_name =
61  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
62  const std::string out_name =
63  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
64 
65  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
66  if (!ifst) return 1;
67 
68  VectorFstClass ofst(ifst->ArcType());
69 
70  const auto weight_threshold =
71  FST_FLAGS_weight.empty()
72  ? WeightClass::Zero(ifst->WeightType())
73  : WeightClass(ifst->WeightType(), FST_FLAGS_weight);
74 
75  const s::DeterminizeOptions opts(
76  FST_FLAGS_delta, weight_threshold, FST_FLAGS_nstate,
77  FST_FLAGS_subsequential_label, det_type,
78  FST_FLAGS_increment_subsequential_label);
79 
80  s::Determinize(*ifst, &ofst, opts);
81 
82  return !ofst.Write(out_name);
83 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:132
DECLARE_double(delta)
DECLARE_string(weight)
void Determinize(const Fst< Arc > &ifst, MutableFst< Arc > *ofst, const DeterminizeOptions< Arc > &opts=DeterminizeOptions< Arc >())
Definition: determinize.h:1079
#define LOG(type)
Definition: log.h:49
DECLARE_bool(increment_subsequential_label)
bool GetDeterminizeType(std::string_view str, DeterminizeType *det_type)
Definition: getters.cc:83
void FailedNewHandler()
Definition: compat.cc:26
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:224
int fstdeterminize_main(int argc, char **argv)
DECLARE_int64(nstate)
DeterminizeType
Definition: determinize.h:377