FST  openfst-1.7.1
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 
8 #include <memory>
9 #include <string>
10 
11 #include <fst/flags.h>
12 #include <fst/log.h>
13 #include <fst/script/equivalent.h>
14 #include <fst/script/getters.h>
16 
17 DECLARE_double(delta);
18 DECLARE_bool(random);
19 DECLARE_int32(max_length);
20 DECLARE_int32(npath);
21 DECLARE_int32(seed);
22 DECLARE_string(select);
23 
24 int fstequivalent_main(int argc, char **argv) {
25  namespace s = fst::script;
26  using fst::RandGenOptions;
28 
29  string usage =
30  "Two DFAs are equivalent iff the exit status is zero.\n\n"
31  " Usage: ";
32  usage += argv[0];
33  usage += " in1.fst in2.fst\n";
34 
35  std::set_new_handler(FailedNewHandler);
36  SET_FLAGS(usage.c_str(), &argc, &argv, true);
37  if (argc != 3) {
38  ShowUsage();
39  return 1;
40  }
41 
42  const string in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
43  const string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
44 
45  if (in1_name.empty() && in2_name.empty()) {
46  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input";
47  return 1;
48  }
49 
50  std::unique_ptr<FstClass> ifst1(FstClass::Read(in1_name));
51  if (!ifst1) return 1;
52 
53  std::unique_ptr<FstClass> ifst2(FstClass::Read(in2_name));
54  if (!ifst2) return 1;
55 
56  if (!FLAGS_random) {
57  bool result = s::Equivalent(*ifst1, *ifst2, FLAGS_delta);
58  if (!result) VLOG(1) << "FSTs are not equivalent";
59  return result ? 0 : 2;
60  } else {
62  if (!s::GetRandArcSelection(FLAGS_select, &ras)) {
63  LOG(ERROR) << argv[0] << ": Unknown or unsupported select type "
64  << FLAGS_select;
65  return 1;
66  }
67  const RandGenOptions<s::RandArcSelection> opts(ras, FLAGS_max_length);
68  bool result = s::RandEquivalent(*ifst1, *ifst2, FLAGS_npath, FLAGS_delta,
69  FLAGS_seed, opts);
70  if (!result) VLOG(1) << "FSTs are not equivalent";
71  return result ? 0 : 2;
72  }
73 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
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)
#define LOG(type)
Definition: log.h:48
bool GetRandArcSelection(const string &str, RandArcSelection *ras)
Definition: getters.cc:86
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
DECLARE_int32(max_length)
#define VLOG(level)
Definition: log.h:49
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)