FST  openfst-1.7.3
OpenFst Library
fstequivalent-main.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Two DFAs are equivalent iff their exit status is zero.
5 
6 #include <cstring>
7 #include <memory>
8 #include <string>
9 
10 #include <fst/flags.h>
11 #include <fst/log.h>
12 #include <fst/script/equivalent.h>
13 #include <fst/script/getters.h>
15 
16 DECLARE_double(delta);
17 DECLARE_bool(random);
18 DECLARE_int32(max_length);
19 DECLARE_int32(npath);
20 DECLARE_int32(seed);
21 DECLARE_string(select);
22 
23 int fstequivalent_main(int argc, char **argv) {
24  namespace s = fst::script;
25  using fst::RandGenOptions;
27 
28  std::string usage =
29  "Two DFAs are equivalent iff the exit status is zero.\n\n"
30  " Usage: ";
31  usage += argv[0];
32  usage += " in1.fst in2.fst\n";
33 
34  std::set_new_handler(FailedNewHandler);
35  SET_FLAGS(usage.c_str(), &argc, &argv, true);
36  if (argc != 3) {
37  ShowUsage();
38  return 1;
39  }
40 
41  const std::string in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
42  const std::string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
43 
44  if (in1_name.empty() && in2_name.empty()) {
45  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input";
46  return 1;
47  }
48 
49  std::unique_ptr<FstClass> ifst1(FstClass::Read(in1_name));
50  if (!ifst1) return 1;
51 
52  std::unique_ptr<FstClass> ifst2(FstClass::Read(in2_name));
53  if (!ifst2) return 1;
54 
55  if (!FLAGS_random) {
56  bool result = s::Equivalent(*ifst1, *ifst2, FLAGS_delta);
57  if (!result) VLOG(1) << "FSTs are not equivalent";
58  return result ? 0 : 2;
59  } else {
61  if (!s::GetRandArcSelection(FLAGS_select, &ras)) {
62  LOG(ERROR) << argv[0] << ": Unknown or unsupported select type "
63  << FLAGS_select;
64  return 1;
65  }
66  const RandGenOptions<s::RandArcSelection> opts(ras, FLAGS_max_length);
67  bool result = s::RandEquivalent(*ifst1, *ifst2, FLAGS_npath, FLAGS_delta,
68  FLAGS_seed, opts);
69  if (!result) VLOG(1) << "FSTs are not equivalent";
70  return result ? 0 : 2;
71  }
72 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:121
DECLARE_bool(random)
bool RandEquivalent(const Fst< Arc > &fst1, const Fst< Arc > &fst2, int32 num_paths, float delta, const RandGenOptions< ArcSelector > &opts, bool *error=nullptr)
bool GetRandArcSelection(const std::string &str, RandArcSelection *ras)
Definition: getters.cc:88
#define LOG(type)
Definition: log.h:46
void FailedNewHandler()
Definition: compat.cc:20
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:209
DECLARE_int32(max_length)
#define VLOG(level)
Definition: log.h:47
DECLARE_double(delta)
int fstequivalent_main(int argc, char **argv)
bool Equivalent(const Fst< Arc > &fst1, const Fst< Arc > &fst2, float delta=kDelta, bool *error=nullptr)
Definition: equivalent.h:114
DECLARE_string(select)