FST  openfst-1.7.2
OpenFst Library
verify.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 // Function to verify an FST's contents.
5 
6 #ifndef FST_VERIFY_H_
7 #define FST_VERIFY_H_
8 
9 #include <fst/log.h>
10 
11 #include <fst/fst.h>
12 #include <fst/test-properties.h>
13 
14 
15 namespace fst {
16 
17 // Verifies that an Fst's contents are sane.
18 template <class Arc>
19 bool Verify(const Fst<Arc> &fst, bool allow_negative_labels = false) {
20  using StateId = typename Arc::StateId;
21  const auto start = fst.Start();
22  const auto *isyms = fst.InputSymbols();
23  const auto *osyms = fst.OutputSymbols();
24  // Count states
25  StateId ns = 0;
26  for (StateIterator<Fst<Arc>> siter(fst); !siter.Done(); siter.Next()) ++ns;
27  if (start == kNoStateId && ns > 0) {
28  LOG(ERROR) << "Verify: FST start state ID not set";
29  return false;
30  } else if (start >= ns) {
31  LOG(ERROR) << "Verify: FST start state ID exceeds number of states";
32  return false;
33  }
34  for (StateIterator<Fst<Arc>> siter(fst); !siter.Done(); siter.Next()) {
35  auto state = siter.Value();
36  size_t na = 0;
37  for (ArcIterator<Fst<Arc>> aiter(fst, state); !aiter.Done(); aiter.Next()) {
38  const auto &arc = aiter.Value();
39  if (!allow_negative_labels && arc.ilabel < 0) {
40  LOG(ERROR) << "Verify: FST input label ID of arc at position " << na
41  << " of state " << state << " is negative";
42  return false;
43  } else if (isyms && isyms->Find(arc.ilabel).empty()) {
44  LOG(ERROR) << "Verify: FST input label ID " << arc.ilabel
45  << " of arc at position " << na << " of state " << state
46  << " is missing from input symbol table \"" << isyms->Name()
47  << "\"";
48  return false;
49  } else if (!allow_negative_labels && arc.olabel < 0) {
50  LOG(ERROR) << "Verify: FST output label ID of arc at position " << na
51  << " of state " << state << " is negative";
52  return false;
53  } else if (osyms && osyms->Find(arc.olabel).empty()) {
54  LOG(ERROR) << "Verify: FST output label ID " << arc.olabel
55  << " of arc at position " << na << " of state " << state
56  << " is missing from output symbol table \"" << osyms->Name()
57  << "\"";
58  return false;
59  } else if (!arc.weight.Member()) {
60  LOG(ERROR) << "Verify: FST weight of arc at position " << na
61  << " of state " << state << " is invalid";
62  return false;
63  } else if (arc.nextstate < 0) {
64  LOG(ERROR) << "Verify: FST destination state ID of arc at position "
65  << na << " of state " << state << " is negative";
66  return false;
67  } else if (arc.nextstate >= ns) {
68  LOG(ERROR) << "Verify: FST destination state ID of arc at position "
69  << na << " of state " << state
70  << " exceeds number of states";
71  return false;
72  }
73  ++na;
74  }
75  if (!fst.Final(state).Member()) {
76  LOG(ERROR) << "Verify: FST final weight of state " << state
77  << " is invalid";
78  return false;
79  }
80  }
81  const auto fst_props = fst.Properties(kFstProperties, false);
82  if (fst_props & kError) {
83  LOG(ERROR) << "Verify: FST error property is set";
84  return false;
85  }
86  uint64 known_props;
87  uint64 test_props =
88  ComputeProperties(fst, kFstProperties, &known_props, false);
89  if (!CompatProperties(fst_props, test_props)) {
90  LOG(ERROR) << "Verify: Stored FST properties incorrect "
91  << "(props1 = stored props, props2 = tested)";
92  return false;
93  } else {
94  return true;
95  }
96 }
97 
98 } // namespace fst
99 
100 #endif // FST_VERIFY_H_
uint64_t uint64
Definition: types.h:32
#define LOG(type)
Definition: log.h:48
virtual Weight Final(StateId) const =0
bool CompatProperties(uint64 props1, uint64 props2)
constexpr uint64 kFstProperties
Definition: properties.h:301
constexpr int kNoStateId
Definition: fst.h:180
uint64 ComputeProperties(const Fst< Arc > &fst, uint64 mask, uint64 *known, bool use_stored)
virtual uint64 Properties(uint64 mask, bool test) const =0
virtual StateId Start() const =0
bool Verify(const Fst< Arc > &fst, bool allow_negative_labels=false)
Definition: verify.h:19
virtual const SymbolTable * InputSymbols() const =0
constexpr uint64 kError
Definition: properties.h:33
virtual const SymbolTable * OutputSymbols() const =0