1 // fstdeterminize.cc
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 // Copyright 2005-2010 Google, Inc.
16 // Author: riley@google.com (Michael Riley)
17 // Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
18 //
19 // \file
20 // Determinizes an FST.
21 //
22
23 #include <fst/script/determinize.h>
24
25 DEFINE_double(delta, fst::kDelta, "Comparison/quantization delta");
26 DEFINE_int64(nstate, fst::kNoStateId, "State number threshold");
27 DEFINE_string(weight, "", "Weight threshold");
28 DEFINE_int64(subsequential_label, 0,
29 "Input label of arc corresponding to residual final output when"
30 " producing a subsequential transducer");
31 DEFINE_bool(disambiguate_output, false,
32 "Keep only the min of ambiguous output");
33
main(int argc,char ** argv)34 int main(int argc, char **argv) {
35 namespace s = fst::script;
36 using fst::script::FstClass;
37 using fst::script::MutableFstClass;
38 using fst::script::VectorFstClass;
39 using fst::script::WeightClass;
40
41 string usage = "Determinizes an FST.\n\n Usage: ";
42 usage += argv[0];
43 usage += " [in.fst [out.fst]]\n";
44
45 std::set_new_handler(FailedNewHandler);
46 SET_FLAGS(usage.c_str(), &argc, &argv, true);
47 if (argc > 3) {
48 ShowUsage();
49 return 1;
50 }
51
52 string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
53 string out_name = argc > 2 ? argv[2] : "";
54
55 FstClass *ifst = FstClass::Read(in_name);
56 if (!ifst) return 1;
57
58 VectorFstClass ofst(ifst->ArcType());
59
60 s::DeterminizeOptions opts(
61 FLAGS_delta, FLAGS_weight.empty() ?
62 WeightClass::Zero() : WeightClass(ifst->WeightType(), FLAGS_weight),
63 FLAGS_nstate, FLAGS_subsequential_label, FLAGS_disambiguate_output);
64
65 s::Determinize(*ifst, &ofst, opts);
66
67 ofst.Write(out_name);
68
69 return 0;
70 }
71