FST  openfst-1.7.1
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 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) << "FarIsomorphic: Cannot 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) << "FarIsomorphic: Cannot 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  VLOG(1) << "FarIsomorphic: 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) return true;
46  if (key1 != key2) {
47  LOG(ERROR) << "FarIsomorphic: Mismatched keys " << key1 << " and "
48  << key2;
49  return false;
50  }
51  if (!Isomorphic(*(reader1->GetFst()), *(reader2->GetFst()), delta)) {
52  LOG(ERROR) << "FarIsomorphic: FSTs for key " << key1
53  << " are not isomorphic";
54  return false;
55  }
56  }
57  if (!reader1->Done() || !reader2->Done()) {
58  LOG(ERROR) << "FarIsomorphic: Key "
59  << (reader1->Done() ? reader2->GetKey() : reader1->GetKey())
60  << " missing form " << (reader2->Done() ? "first" : "second")
61  << " archive";
62  return false;
63  }
64  return true;
65 }
66 
67 } // namespace fst
68 
69 #endif // FST_EXTENSIONS_FAR_ISOMORPHIC_H_
bool Isomorphic(const Fst< Arc > &fst1, const Fst< Arc > &fst2, float delta=kDelta)
Definition: isomorphic.h:169
#define LOG(type)
Definition: log.h:48
#define VLOG(level)
Definition: log.h:49
constexpr float kDelta
Definition: weight.h:109
bool FarIsomorphic(const string &filename1, const string &filename2, float delta=kDelta, const string &begin_key=string(), const string &end_key=string())
Definition: isomorphic.h:16