FST  openfst-1.7.1
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 Label = typename Arc::Label;
21  using StateId = typename Arc::StateId;
22  using Weight = typename Arc::Weight;
23  const auto start = fst.Start();
24  const auto *isyms = fst.InputSymbols();
25  const auto *osyms = fst.OutputSymbols();
26  // Count states
27  StateId ns = 0;
28  for (StateIterator<Fst<Arc>> siter(fst); !siter.Done(); siter.Next()) ++ns;
29  if (start == kNoStateId && ns > 0) {
30  LOG(ERROR) << "Verify: FST start state ID not set";
31  return false;
32  } else if (start >= ns) {
33  LOG(ERROR) << "Verify: FST start state ID exceeds number of states";
34  return false;
35  }
36  for (StateIterator<Fst<Arc>> siter(fst); !siter.Done(); siter.Next()) {
37  auto state = siter.Value();
38  size_t na = 0;
39  for (ArcIterator<Fst<Arc>> aiter(fst, state); !aiter.Done(); aiter.Next()) {
40  const auto &arc = aiter.Value();
41  if (!allow_negative_labels && arc.ilabel < 0) {
42  LOG(ERROR) << "Verify: FST input label ID of arc at position " << na
43  << " of state " << state << " is negative";
44  return false;
45  } else if (isyms && isyms->Find(arc.ilabel) == "") {
46  LOG(ERROR) << "Verify: FST input label ID " << arc.ilabel
47  << " of arc at position " << na << " of state " << state
48  << " is missing from input symbol table \"" << isyms->Name()
49  << "\"";
50  return false;
51  } else if (!allow_negative_labels && arc.olabel < 0) {
52  LOG(ERROR) << "Verify: FST output label ID of arc at position " << na
53  << " of state " << state << " is negative";
54  return false;
55  } else if (osyms && osyms->Find(arc.olabel) == "") {
56  LOG(ERROR) << "Verify: FST output label ID " << arc.olabel
57  << " of arc at position " << na << " of state " << state
58  << " is missing from output symbol table \"" << osyms->Name()
59  << "\"";
60  return false;
61  } else if (!arc.weight.Member()) {
62  LOG(ERROR) << "Verify: FST weight of arc at position " << na
63  << " of state " << state << " is invalid";
64  return false;
65  } else if (arc.nextstate < 0) {
66  LOG(ERROR) << "Verify: FST destination state ID of arc at position "
67  << na << " of state " << state << " is negative";
68  return false;
69  } else if (arc.nextstate >= ns) {
70  LOG(ERROR) << "Verify: FST destination state ID of arc at position "
71  << na << " of state " << state
72  << " exceeds number of states";
73  return false;
74  }
75  ++na;
76  }
77  if (!fst.Final(state).Member()) {
78  LOG(ERROR) << "Verify: FST final weight of state " << state
79  << " is invalid";
80  return false;
81  }
82  }
83  const auto fst_props = fst.Properties(kFstProperties, false);
84  if (fst_props & kError) {
85  LOG(ERROR) << "Verify: FST error property is set";
86  return false;
87  }
88  uint64 known_props;
89  uint64 test_props =
90  ComputeProperties(fst, kFstProperties, &known_props, false);
91  if (!CompatProperties(fst_props, test_props)) {
92  LOG(ERROR) << "Verify: Stored FST properties incorrect "
93  << "(props1 = stored props, props2 = tested)";
94  return false;
95  } else {
96  return true;
97  }
98 }
99 
100 } // namespace fst
101 
102 #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