FST  openfst-1.8.4
OpenFst Library
fstequivalent-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 DFAs are equivalent iff their exit status is zero.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 
24 #include <fst/flags.h>
25 #include <fst/log.h>
26 #include <fst/randgen.h>
27 #include <fst/script/equivalent.h>
28 #include <fst/script/fst-class.h>
29 #include <fst/script/getters.h>
31 #include <fst/script/script-impl.h>
32 
33 DECLARE_double(delta);
34 DECLARE_bool(random);
35 DECLARE_int32(max_length);
36 DECLARE_int32(npath);
37 DECLARE_uint64(seed);
38 DECLARE_string(select);
39 
40 int fstequivalent_main(int argc, char **argv) {
41  namespace s = fst::script;
42  using fst::RandGenOptions;
44 
45  std::string usage =
46  "Two DFAs are equivalent iff the exit status is zero.\n\n"
47  " Usage: ";
48  usage += argv[0];
49  usage += " in1.fst in2.fst\n";
50 
51  SET_FLAGS(usage.c_str(), &argc, &argv, true);
52  if (argc != 3) {
53  ShowUsage();
54  return 1;
55  }
56 
57  const std::string in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
58  const std::string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
59 
60  if (in1_name.empty() && in2_name.empty()) {
61  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input";
62  return 1;
63  }
64 
65  std::unique_ptr<FstClass> ifst1(FstClass::Read(in1_name));
66  if (!ifst1) return 1;
67 
68  std::unique_ptr<FstClass> ifst2(FstClass::Read(in2_name));
69  if (!ifst2) return 1;
70 
71  bool result;
72  if (FST_FLAGS_random) {
74  if (!s::GetRandArcSelection(FST_FLAGS_select, &ras)) {
75  LOG(ERROR) << argv[0] << ": Unknown or unsupported select type "
76  << FST_FLAGS_select;
77  return 1;
78  }
79  const RandGenOptions<s::RandArcSelection> opts(
80  ras, FST_FLAGS_max_length);
81  const auto seed = s::GetSeed(FST_FLAGS_seed);
82  VLOG(1) << argv[0] << ": Seed = " << seed;
83  result = s::RandEquivalent(*ifst1, *ifst2, FST_FLAGS_npath, opts,
84  FST_FLAGS_delta, seed);
85  } else {
86  result = s::Equivalent(*ifst1, *ifst2, FST_FLAGS_delta);
87  }
88 
89  if (!result) VLOG(1) << "FSTs are not equivalent";
90 
91  return result ? 0 : 2;
92 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
bool RandEquivalent(const Fst< Arc > &fst1, const Fst< Arc > &fst2, int32_t npath, const RandGenOptions< ArcSelector > &opts, float delta=kDelta, uint64_t seed=std::random_device()(), bool *error=nullptr)
DECLARE_bool(random)
bool GetRandArcSelection(std::string_view str, RandArcSelection *ras)
Definition: getters.cc:171
#define LOG(type)
Definition: log.h:53
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
uint64_t GetSeed(uint64_t seed)
Definition: getters.cc:230
DECLARE_int32(max_length)
#define VLOG(level)
Definition: log.h:54
DECLARE_uint64(seed)
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:127
DECLARE_string(select)