FST  openfst-1.8.3
OpenFst Library
verify.h
Go to the documentation of this file.
1 // Copyright 2005-2024 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 <cstddef>
24 #include <cstdint>
25 
26 #include <fst/log.h>
27 #include <fst/expanded-fst.h>
28 #include <fst/fst.h>
29 #include <fst/properties.h>
30 #include <fst/test-properties.h>
31 
32 namespace fst {
33 
34 // Verifies that an Fst's contents are sane.
35 template <class Arc>
36 bool Verify(const Fst<Arc> &fst, bool allow_negative_labels = false) {
37  const auto start = fst.Start();
38  const auto *isyms = fst.InputSymbols();
39  const auto *osyms = fst.OutputSymbols();
40  const auto ns = CountStates(fst);
41  if (start == kNoStateId && ns > 0) {
42  LOG(ERROR) << "Verify: FST start state ID not set";
43  return false;
44  } else if (start >= ns) {
45  LOG(ERROR) << "Verify: FST start state ID exceeds number of states";
46  return false;
47  }
48  for (StateIterator<Fst<Arc>> siter(fst); !siter.Done(); siter.Next()) {
49  auto state = siter.Value();
50  size_t na = 0;
51  for (ArcIterator<Fst<Arc>> aiter(fst, state); !aiter.Done(); aiter.Next()) {
52  const auto &arc = aiter.Value();
53  if (!allow_negative_labels && arc.ilabel < 0) {
54  LOG(ERROR) << "Verify: FST input label ID of arc at position " << na
55  << " of state " << state << " is negative";
56  return false;
57  } else if (isyms && !isyms->Member(arc.ilabel)) {
58  LOG(ERROR) << "Verify: FST input label ID " << arc.ilabel
59  << " of arc at position " << na << " of state " << state
60  << " is missing from input symbol table \"" << isyms->Name()
61  << "\"";
62  return false;
63  } else if (!allow_negative_labels && arc.olabel < 0) {
64  LOG(ERROR) << "Verify: FST output label ID of arc at position " << na
65  << " of state " << state << " is negative";
66  return false;
67  } else if (osyms && !osyms->Member(arc.olabel)) {
68  LOG(ERROR) << "Verify: FST output label ID " << arc.olabel
69  << " of arc at position " << na << " of state " << state
70  << " is missing from output symbol table \"" << osyms->Name()
71  << "\"";
72  return false;
73  } else if (!arc.weight.Member()) {
74  LOG(ERROR) << "Verify: FST weight of arc at position " << na
75  << " of state " << state << " is invalid";
76  return false;
77  } else if (arc.nextstate < 0) {
78  LOG(ERROR) << "Verify: FST destination state ID of arc at position "
79  << na << " of state " << state << " is negative";
80  return false;
81  } else if (arc.nextstate >= ns) {
82  LOG(ERROR) << "Verify: FST destination state ID of arc at position "
83  << na << " of state " << state
84  << " exceeds number of states";
85  return false;
86  }
87  ++na;
88  }
89  if (!fst.Final(state).Member()) {
90  LOG(ERROR) << "Verify: FST final weight of state " << state
91  << " is invalid";
92  return false;
93  }
94  }
95  const auto fst_props = fst.Properties(kFstProperties, /*test=*/false);
96  if (fst_props & kError) {
97  LOG(ERROR) << "Verify: FST error property is set";
98  return false;
99  }
100  uint64_t known_props;
101  uint64_t test_props =
102  internal::ComputeProperties(fst, kFstProperties, &known_props);
103  if (!internal::CompatProperties(fst_props, test_props)) {
104  LOG(ERROR) << "Verify: Stored FST properties incorrect "
105  << "(props1 = stored props, props2 = tested)";
106  return false;
107  } else {
108  return true;
109  }
110 }
111 
112 } // namespace fst
113 
114 #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:507
constexpr uint64_t kError
Definition: properties.h:52
#define LOG(type)
Definition: log.h:53
virtual Weight Final(StateId) const =0
constexpr int kNoStateId
Definition: fst.h:196
virtual StateId Start() const =0
bool Verify(const Fst< Arc > &fst, bool allow_negative_labels=false)
Definition: verify.h:36
constexpr uint64_t kFstProperties
Definition: properties.h:326
virtual const SymbolTable * InputSymbols() const =0
Arc::StateId CountStates(const Fst< Arc > &fst)
Definition: expanded-fst.h:179
virtual const SymbolTable * OutputSymbols() const =0