1
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6//     http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13//
14// Copyright 2005-2010 Google, Inc.
15// Author: allauzen@google.com (Cyril Allauzen)
16
17#ifndef FST_EXTENSIONS_FAR_EQUAL_H_
18#define FST_EXTENSIONS_FAR_EQUAL_H_
19
20#include <string>
21
22#include <fst/extensions/far/far.h>
23#include <fst/equal.h>
24
25namespace fst {
26
27template <class Arc>
28bool FarEqual(const string &filename1,
29             const string &filename2,
30             float delta = kDelta,
31             const string &begin_key = string(),
32             const string &end_key = string()) {
33
34  FarReader<Arc> *reader1 = FarReader<Arc>::Open(filename1);
35  FarReader<Arc> *reader2 = FarReader<Arc>::Open(filename2);
36  if (!reader1 || !reader2) {
37    delete reader1;
38    delete reader2;
39    VLOG(1) << "FarEqual: cannot open input Far file(s)";
40    return false;
41  }
42
43  if (!begin_key.empty()) {
44    bool find_begin1 = reader1->Find(begin_key);
45    bool find_begin2 = reader2->Find(begin_key);
46    if (!find_begin1 || !find_begin2) {
47      bool ret = !find_begin1 && !find_begin2;
48      if (!ret) {
49        VLOG(1) << "FarEqual: key \"" << begin_key << "\" missing from "
50                << (find_begin1 ? "second" : "first") << " archive.";
51      }
52      delete reader1;
53      delete reader2;
54      return ret;
55    }
56  }
57
58  for(; !reader1->Done() && !reader2->Done();
59      reader1->Next(), reader2->Next()) {
60    const string key1 = reader1->GetKey();
61    const string key2 = reader2->GetKey();
62    if (!end_key.empty() && end_key < key1 && end_key < key2) {
63      delete reader1;
64      delete reader2;
65      return true;
66    }
67    if (key1 != key2) {
68      VLOG(1) << "FarEqual: mismatched keys \""
69              << key1 << "\" <> \"" << key2 << "\".";
70      delete reader1;
71      delete reader2;
72      return false;
73    }
74    if (!Equal(reader1->GetFst(), reader2->GetFst(), delta)) {
75      VLOG(1) << "FarEqual: Fsts for key \"" << key1 << "\" are not equal.";
76      delete reader1;
77      delete reader2;
78      return false;
79    }
80  }
81
82  if (!reader1->Done() || !reader2->Done()) {
83    VLOG(1) << "FarEqual: key \""
84            << (reader1->Done() ? reader2->GetKey() : reader1->GetKey())
85            << "\" missing form " << (reader2->Done() ? "first" : "second")
86            << " archive.";
87    delete reader1;
88    delete reader2;
89    return false;
90  }
91
92  delete reader1;
93  delete reader2;
94  return true;
95}
96
97}  // namespace fst
98
99#endif  // FST_EXTENSIONS_FAR_EQUAL_H_
100