FST  openfst-1.6.1
OpenFst Library
randmod.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 // Generates a random FST according to a class-specific transition model.
5 
6 #ifndef FST_EXTENSIONS_COMPRESS_RANDMOD_H_
7 #define FST_EXTENSIONS_COMPRESS_RANDMOD_H_
8 
9 #include <vector>
10 
11 #include <fst/compat.h>
12 #include <fst/mutable-fst.h>
13 
14 namespace fst {
15 
16 template <class Arc, class G>
17 class RandMod {
18  public:
19  typedef typename Arc::StateId StateId;
20  typedef typename Arc::Label Label;
21  typedef typename Arc::Weight Weight;
22 
23  // Generates random FST with 'nstates' with 'nclasses' in the probability
24  // generation model, and 'nlabels' in the alphabet. If 'trans' = true, then
25  // a transducer is generated; iff 'generate_' is non-null, the output is
26  // randomly weighted.
27  RandMod(StateId nstates, StateId nclasses, Label nlabels, bool trans,
28  const G *generate)
29  : nstates_(nstates),
30  nclasses_(nclasses),
31  nlabels_(nlabels),
32  trans_(trans),
33  generate_(generate) {
34  for (StateId s = 0; s < nstates; ++s) {
35  classes_.push_back(rand() % nclasses); // NOLINT
36  }
37  }
38 
39  // Generates a random FST according to a class-specific transition model
41  StateId start = rand() % nstates_; // NOLINT
42  fst->DeleteStates();
43  for (StateId s = 0; s < nstates_; ++s) {
44  fst->AddState();
45  if (s == start) fst->SetStart(start);
46  for (StateId n = 0; n <= nstates_; ++n) {
47  Arc arc;
48  StateId d = n == nstates_ ? kNoStateId : n;
49  if (!RandArc(s, d, &arc)) continue;
50  if (d == kNoStateId) { // A super-final transition?
51  fst->SetFinal(s, arc.weight);
52  } else {
53  fst->AddArc(s, arc);
54  }
55  }
56  }
57  }
58 
59  private:
60  // Generates a transition from s to d. If d == kNoStateId, a superfinal
61  // transition is generated. Returns false if no transition generated.
62  bool RandArc(StateId s, StateId d, Arc *arc) {
63  StateId sclass = classes_[s];
64  StateId dclass = d != kNoStateId ? classes_[d] : 0;
65 
66  int r = sclass + dclass + 2;
67  if ((rand() % r) != 0) // NOLINT
68  return false;
69 
70  arc->nextstate = d;
71 
72  Label ilabel = kNoLabel;
73  Label olabel = kNoLabel;
74  if (d != kNoStateId) {
75  ilabel = (dclass % nlabels_) + 1;
76  if (trans_)
77  olabel = (sclass % nlabels_) + 1;
78  else
79  olabel = ilabel;
80  }
81 
82  Weight weight = Weight::One();
83  if (generate_) weight = (*generate_)();
84 
85  arc->ilabel = ilabel;
86  arc->olabel = olabel;
87  arc->weight = weight;
88  return true;
89  }
90 
91  StateId nstates_;
92  StateId nclasses_;
93  Label nlabels_;
94  bool trans_;
95  const G *generate_;
96  std::vector<StateId> classes_;
97 };
98 
99 } // namespace fst
100 
101 #endif // FST_EXTENSIONS_COMPRESS_RANDMOD_H_
Arc::StateId StateId
Definition: randmod.h:19
constexpr int kNoLabel
Definition: fst.h:180
void Generate(StdMutableFst *fst)
Definition: randmod.h:40
virtual void AddArc(StateId, const Arc &arc)=0
virtual void SetStart(StateId)=0
constexpr int kNoStateId
Definition: fst.h:179
virtual void SetFinal(StateId, Weight)=0
Arc::Label Label
Definition: randmod.h:20
virtual StateId AddState()=0
virtual void DeleteStates(const std::vector< StateId > &)=0
Arc::Weight Weight
Definition: randmod.h:21
RandMod(StateId nstates, StateId nclasses, Label nlabels, bool trans, const G *generate)
Definition: randmod.h:27