1 // fstequivalent.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 // Two DFAs are equivalent iff their exit status is zero.
21 //
22
23 #include <fst/script/equivalent.h>
24 #include <fst/script/randequivalent.h>
25
26 DEFINE_double(delta, fst::kDelta, "Comparison/quantization delta");
27 DEFINE_bool(random, false,
28 "Test equivalence by randomly selecting paths in the input FSTs");
29 DEFINE_int32(max_length, INT_MAX, "Maximum path length");
30 DEFINE_int32(npath, 1, "Number of paths to generate");
31 DEFINE_int32(seed, time(0), "Random seed");
32 DEFINE_string(select, "uniform", "Selection type: one of: "
33 " \"uniform\", \"log_prob (when appropriate),"
34 " \"fast_log_prob\" (when appropriate)");
35
main(int argc,char ** argv)36 int main(int argc, char **argv) {
37 namespace s = fst::script;
38 using fst::script::FstClass;
39
40 string usage = "Two DFAs are equivalent iff the exit status is zero.\n\n"
41 " Usage: ";
42 usage += argv[0];
43 usage += " in1.fst in2.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 in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
53 string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
54
55 if (in1_name.empty() && in2_name.empty()) {
56 LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input.";
57 return 1;
58 }
59
60 FstClass *ifst1 = FstClass::Read(in1_name);
61 if (!ifst1) return 1;
62
63 FstClass *ifst2 = FstClass::Read(in2_name);
64 if (!ifst2) return 1;
65
66 if (!FLAGS_random) {
67 return s::Equivalent(*ifst1, *ifst2, FLAGS_delta) ? 0 : 2;
68 } else {
69 s::RandArcSelection ras;
70
71 if (FLAGS_select == "uniform") {
72 ras = s::UNIFORM_ARC_SELECTOR;
73 } else if (FLAGS_select == "log_prob") {
74 ras = s::LOG_PROB_ARC_SELECTOR;
75 } else if (FLAGS_select == "fast_log_prob") {
76 ras = s::FAST_LOG_PROB_ARC_SELECTOR;
77 } else {
78 LOG(ERROR) << argv[0] << ": Unknown selection type \""
79 << FLAGS_select << "\"\n";
80 return 1;
81 }
82
83 return s::RandEquivalent(
84 *ifst1, *ifst2,
85 FLAGS_seed,
86 FLAGS_npath,
87 FLAGS_delta,
88 fst::RandGenOptions<s::RandArcSelection>(
89 ras, FLAGS_max_length)) ? 0 : 2;
90 }
91 }
92