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