FST  openfst-1.8.2
OpenFst Library
verify.h
Go to the documentation of this file.
1 // Copyright 2005-2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Function to verify an FST's contents.
19 
20 #ifndef FST_VERIFY_H_
21 #define FST_VERIFY_H_
22 
23 #include <cstdint>
24 
25 #include <fst/log.h>
26 
27 #include <fst/fst.h>
28 #include <fst/test-properties.h>
29 
30 
31 namespace fst {
32 
33 // Verifies that an Fst's contents are sane.
34 template <class Arc>
35 bool Verify(const Fst<Arc> &fst, bool allow_negative_labels = false) {
36  const auto start = fst.Start();
37  const auto *isyms = fst.InputSymbols();
38  const auto *osyms = fst.OutputSymbols();
39  const auto ns = CountStates(fst);
40  if (start == kNoStateId && ns > 0) {
41  LOG(ERROR) << "Verify: FST start state ID not set";
42  return false;
43  } else if (start >= ns) {
44  LOG(ERROR) << "Verify: FST start state ID exceeds number of states";
45  return false;
46  }
47  for (StateIterator<Fst<Arc>> siter(fst); !siter.Done(); siter.Next()) {
48  auto state = siter.Value();
49  size_t na = 0;
50  for (ArcIterator<Fst<Arc>> aiter(fst, state); !aiter.Done(); aiter.Next()) {
51  const auto &arc = aiter.Value();
52  if (!allow_negative_labels && arc.ilabel < 0) {
53  LOG(ERROR) << "Verify: FST input label ID of arc at position " << na
54  << " of state " << state << " is negative";
55  return false;
56  } else if (isyms && !isyms->Member(arc.ilabel)) {
57  LOG(ERROR) << "Verify: FST input label ID " << arc.ilabel
58  << " of arc at position " << na << " of state " << state
59  << " is missing from input symbol table \"" << isyms->Name()
60  << "\"";
61  return false;
62  } else if (!allow_negative_labels && arc.olabel < 0) {
63  LOG(ERROR) << "Verify: FST output label ID of arc at position " << na
64  << " of state " << state << " is negative";
65  return false;
66  } else if (osyms && !osyms->Member(arc.olabel)) {
67  LOG(ERROR) << "Verify: FST output label ID " << arc.olabel
68  << " of arc at position " << na << " of state " << state
69  << " is missing from output symbol table \"" << osyms->Name()
70  << "\"";
71  return false;
72  } else if (!arc.weight.Member()) {
73  LOG(ERROR) << "Verify: FST weight of arc at position " << na
74  << " of state " << state << " is invalid";
75  return false;
76  } else if (arc.nextstate < 0) {
77  LOG(ERROR) << "Verify: FST destination state ID of arc at position "
78  << na << " of state " << state << " is negative";
79  return false;
80  } else if (arc.nextstate >= ns) {
81  LOG(ERROR) << "Verify: FST destination state ID of arc at position "
82  << na << " of state " << state
83  << " exceeds number of states";
84  return false;
85  }
86  ++na;
87  }
88  if (!fst.Final(state).Member()) {
89  LOG(ERROR) << "Verify: FST final weight of state " << state
90  << " is invalid";
91  return false;
92  }
93  }
94  const auto fst_props = fst.Properties(kFstProperties, /*test=*/false);
95  if (fst_props & kError) {
96  LOG(ERROR) << "Verify: FST error property is set";
97  return false;
98  }
99  uint64_t known_props;
100  uint64_t test_props =
101  internal::ComputeProperties(fst, kFstProperties, &known_props);
102  if (!internal::CompatProperties(fst_props, test_props)) {
103  LOG(ERROR) << "Verify: Stored FST properties incorrect "
104  << "(props1 = stored props, props2 = tested)";
105  return false;
106  } else {
107  return true;
108  }
109 }
110 
111 } // namespace fst
112 
113 #endif // FST_VERIFY_H_
uint64_t ComputeProperties(const Fst< Arc > &fst, uint64_t mask, uint64_t *known)
virtual uint64_t Properties(uint64_t mask, bool test) const =0
bool CompatProperties(uint64_t props1, uint64_t props2)
Definition: properties.h:506
constexpr uint64_t kError
Definition: properties.h:51
#define LOG(type)
Definition: log.h:49
virtual Weight Final(StateId) const =0
constexpr int kNoStateId
Definition: fst.h:202
virtual StateId Start() const =0
bool Verify(const Fst< Arc > &fst, bool allow_negative_labels=false)
Definition: verify.h:35
constexpr uint64_t kFstProperties
Definition: properties.h:325
virtual const SymbolTable * InputSymbols() const =0
Arc::StateId CountStates(const Fst< Arc > &fst)
Definition: expanded-fst.h:169
virtual const SymbolTable * OutputSymbols() const =0