FST  openfst-1.7.2
OpenFst Library
equal.h
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 #ifndef FST_EXTENSIONS_FAR_EQUAL_H_
5 #define FST_EXTENSIONS_FAR_EQUAL_H_
6 
7 #include <memory>
8 #include <string>
9 
10 #include <fst/extensions/far/far.h>
11 #include <fst/equal.h>
12 
13 namespace fst {
14 
15 template <class Arc>
16 bool FarEqual(const string &filename1, const string &filename2,
17  float delta = kDelta, const string &begin_key = string(),
18  const string &end_key = string()) {
19  std::unique_ptr<FarReader<Arc>> reader1(FarReader<Arc>::Open(filename1));
20  if (!reader1) {
21  LOG(ERROR) << "FarEqual: Could not open FAR file " << filename1;
22  return false;
23  }
24  std::unique_ptr<FarReader<Arc>> reader2(FarReader<Arc>::Open(filename2));
25  if (!reader2) {
26  LOG(ERROR) << "FarEqual: Could not open FAR file " << filename2;
27  return false;
28  }
29  if (!begin_key.empty()) {
30  bool find_begin1 = reader1->Find(begin_key);
31  bool find_begin2 = reader2->Find(begin_key);
32  if (!find_begin1 || !find_begin2) {
33  bool ret = !find_begin1 && !find_begin2;
34  if (!ret) {
35  LOG(ERROR) << "FarEqual: Key " << begin_key << " missing from "
36  << (find_begin1 ? "second" : "first") << " archive";
37  }
38  return ret;
39  }
40  }
41  for (; !reader1->Done() && !reader2->Done();
42  reader1->Next(), reader2->Next()) {
43  const auto &key1 = reader1->GetKey();
44  const auto &key2 = reader2->GetKey();
45  if (!end_key.empty() && end_key < key1 && end_key < key2) {
46  return true;
47  }
48  if (key1 != key2) {
49  LOG(ERROR) << "FarEqual: Mismatched keys " << key1 << " and " << key2;
50  return false;
51  }
52  if (!Equal(*(reader1->GetFst()), *(reader2->GetFst()), delta)) {
53  LOG(ERROR) << "FarEqual: FSTs for key " << key1 << " are not equal";
54  return false;
55  }
56  }
57  if (!reader1->Done() || !reader2->Done()) {
58  LOG(ERROR) << "FarEqual: Key "
59  << (reader1->Done() ? reader2->GetKey() : reader1->GetKey())
60  << " missing from " << (reader2->Done() ? "first" : "second")
61  << " archive";
62  return false;
63  }
64  return true;
65 }
66 
67 } // namespace fst
68 
69 #endif // FST_EXTENSIONS_FAR_EQUAL_H_
#define LOG(type)
Definition: log.h:48
bool FarEqual(const string &filename1, const string &filename2, float delta=kDelta, const string &begin_key=string(), const string &end_key=string())
Definition: equal.h:16
bool Equal(const Fst< Arc > &fst1, const Fst< Arc > &fst2, WeightEqual weight_equal, uint32 etype=kEqualFsts)
Definition: equal.h:43
constexpr float kDelta
Definition: weight.h:109