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