FST  openfst-1.8.3
OpenFst Library
farequal-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 // Tests whether two FAR files contains the same (key, fst) pairs.
19 
20 #include <memory>
21 #include <string>
22 
23 #include <fst/flags.h>
24 #include <fst/log.h>
28 #include <fst/util.h>
29 #include <fst/script/arg-packs.h>
30 
31 DECLARE_string(begin_key);
32 DECLARE_string(end_key);
33 DECLARE_double(delta);
34 
35 int farequal_main(int argc, char **argv) {
36  namespace s = fst::script;
38 
39  std::string usage = "Compares two FST archives for equality.\n\n Usage: ";
40  usage += argv[0];
41  usage += " in1.far in2.far";
42 
43  SET_FLAGS(usage.c_str(), &argc, &argv, true);
44  s::ExpandArgs(argc, argv, &argc, &argv);
45  if (argc != 3) {
46  ShowUsage();
47  return 1;
48  }
49 
50  std::unique_ptr<FarReaderClass> reader1(FarReaderClass::Open(argv[1]));
51  if (!reader1) return 1;
52 
53  std::unique_ptr<FarReaderClass> reader2(FarReaderClass::Open(argv[2]));
54  if (!reader2) return 1;
55 
56  const bool result =
57  s::Equal(*reader1, *reader2, FST_FLAGS_delta,
58  FST_FLAGS_begin_key, FST_FLAGS_end_key);
59 
60  if (reader1->Error()) {
61  FSTERROR() << "Error reading FAR: " << argv[1];
62  return 1;
63  }
64  if (reader2->Error()) {
65  FSTERROR() << "Error reading FAR: " << argv[2];
66  return 1;
67  }
68 
69  if (!result) VLOG(1) << "FARs are not equal";
70 
71  return result ? 0 : 2;
72 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
DECLARE_double(delta)
bool Equal(FarReaderClass &reader1, FarReaderClass &reader2, float delta, std::string_view begin_key, std::string_view end_key)
Definition: farscript.cc:112
void ExpandArgs(int argc, char **argv, int *argcp, char ***argvp)
Definition: getters.cc:60
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
#define FSTERROR()
Definition: util.h:56
#define VLOG(level)
Definition: log.h:54
int farequal_main(int argc, char **argv)
DECLARE_string(begin_key)