FST  openfst-1.7.1
OpenFst Library
fstisomorphic-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 FSTS are isomorphic (equal up to state and arc re-ordering) iff their
5 // exit status is zero. FSTs should be deterministic when viewed as unweighted
6 // automata.
7 
8 #include <cstring>
9 
10 #include <memory>
11 #include <string>
12 
13 #include <fst/flags.h>
14 #include <fst/log.h>
15 #include <fst/script/isomorphic.h>
16 
17 DECLARE_double(delta);
18 
19 int fstisomorphic_main(int argc, char **argv) {
20  namespace s = fst::script;
22 
23  string usage =
24  "Two FSTs are isomorphic iff the exit status is zero.\n\n Usage: ";
25  usage += argv[0];
26  usage += " in1.fst in2.fst\n";
27 
28  std::set_new_handler(FailedNewHandler);
29  SET_FLAGS(usage.c_str(), &argc, &argv, true);
30  if (argc != 3) {
31  ShowUsage();
32  return 1;
33  }
34 
35  const string in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
36  const string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
37 
38  if (in1_name.empty() && in2_name.empty()) {
39  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input";
40  return 1;
41  }
42 
43  std::unique_ptr<FstClass> ifst1(FstClass::Read(in1_name));
44  if (!ifst1) return 1;
45 
46  std::unique_ptr<FstClass> ifst2(FstClass::Read(in2_name));
47  if (!ifst2) return 1;
48 
49  bool result = s::Isomorphic(*ifst1, *ifst2, FLAGS_delta);
50  if (!result) VLOG(1) << "FSTs are not isomorphic";
51 
52  return result ? 0 : 2;
53 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
int fstisomorphic_main(int argc, char **argv)
DECLARE_double(delta)
bool Isomorphic(const Fst< Arc > &fst1, const Fst< Arc > &fst2, float delta=kDelta)
Definition: isomorphic.h:169
#define LOG(type)
Definition: log.h:48
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
#define VLOG(level)
Definition: log.h:49