FST  openfst-1.8.3
OpenFst Library
fstisomorphic-main.cc
Go to the documentation of this file.
1 // Copyright 2005-2024 Google LLC
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 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Two FSTS are isomorphic (equal up to state and arc re-ordering) iff their
19 // exit status is zero. FSTs should be deterministic when viewed as unweighted
20 // automata.
21 
22 #include <cstring>
23 #include <memory>
24 #include <string>
25 
26 #include <fst/flags.h>
27 #include <fst/log.h>
28 #include <fst/script/fst-class.h>
29 #include <fst/script/isomorphic.h>
30 
31 DECLARE_double(delta);
32 
33 int fstisomorphic_main(int argc, char **argv) {
34  namespace s = fst::script;
36 
37  std::string usage =
38  "Two FSTs are isomorphic iff the exit status is zero.\n\n Usage: ";
39  usage += argv[0];
40  usage += " in1.fst in2.fst\n";
41 
42  SET_FLAGS(usage.c_str(), &argc, &argv, true);
43  if (argc != 3) {
44  ShowUsage();
45  return 1;
46  }
47 
48  const std::string in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
49  const std::string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
50 
51  if (in1_name.empty() && in2_name.empty()) {
52  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input";
53  return 1;
54  }
55 
56  std::unique_ptr<FstClass> ifst1(FstClass::Read(in1_name));
57  if (!ifst1) return 1;
58 
59  std::unique_ptr<FstClass> ifst2(FstClass::Read(in2_name));
60  if (!ifst2) return 1;
61 
62  const bool result = s::Isomorphic(*ifst1, *ifst2, FST_FLAGS_delta);
63  if (!result) VLOG(1) << "FSTs are not isomorphic";
64 
65  return result ? 0 : 2;
66 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
int fstisomorphic_main(int argc, char **argv)
bool Isomorphic(FarReaderClass &reader1, FarReaderClass &reader2, float delta, std::string_view begin_key, std::string_view end_key)
Definition: farscript.cc:154
DECLARE_double(delta)
#define LOG(type)
Definition: log.h:53
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
#define VLOG(level)
Definition: log.h:54