FST  openfst-1.7.3
OpenFst Library
isomorphic.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_ISOMORPHIC_H_
5 #define FST_EXTENSIONS_FAR_ISOMORPHIC_H_
6 
7 #include <memory>
8 #include <string>
9 
10 #include <fst/extensions/far/far.h>
11 #include <fst/isomorphic.h>
12 
13 namespace fst {
14 
15 template <class Arc>
16 bool FarIsomorphic(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) << "FarIsomorphic: Cannot 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) << "FarIsomorphic: Cannot 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  VLOG(1) << "FarIsomorphic: 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) return true;
47  if (key1 != key2) {
48  LOG(ERROR) << "FarIsomorphic: Mismatched keys " << key1 << " and "
49  << key2;
50  return false;
51  }
52  if (!Isomorphic(*(reader1->GetFst()), *(reader2->GetFst()), delta)) {
53  LOG(ERROR) << "FarIsomorphic: FSTs for key " << key1
54  << " are not isomorphic";
55  return false;
56  }
57  }
58  if (!reader1->Done() || !reader2->Done()) {
59  LOG(ERROR) << "FarIsomorphic: Key "
60  << (reader1->Done() ? reader2->GetKey() : reader1->GetKey())
61  << " missing form " << (reader2->Done() ? "first" : "second")
62  << " archive";
63  return false;
64  }
65  return true;
66 }
67 
68 } // namespace fst
69 
70 #endif // FST_EXTENSIONS_FAR_ISOMORPHIC_H_
bool FarIsomorphic(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: isomorphic.h:16
bool Isomorphic(const Fst< Arc > &fst1, const Fst< Arc > &fst2, float delta=kDelta)
Definition: isomorphic.h:229
#define LOG(type)
Definition: log.h:46
#define VLOG(level)
Definition: log.h:47
constexpr float kDelta
Definition: weight.h:109