rand-fst.h
4.92 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// fstext/rand-fst.h
// Copyright 2009-2011 Microsoft Corporation
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#ifndef KALDI_FSTEXT_RAND_FST_H_
#define KALDI_FSTEXT_RAND_FST_H_
#include <sstream>
#include <string>
#include <fst/fstlib.h>
#include <fst/fst-decl.h>
#include "base/kaldi-math.h"
namespace fst {
// Note: all weights are constructed from nonnegative floats.
// (so no "negative costs").
struct RandFstOptions {
size_t n_syms;
size_t n_states;
size_t n_arcs;
size_t n_final;
bool allow_empty;
bool acyclic;
float weight_multiplier;
RandFstOptions() { // Initializes the options randomly.
n_syms = 2 + kaldi::Rand() % 5;
n_states = 3 + kaldi::Rand() % 10;
n_arcs = 5 + kaldi::Rand() % 30;
n_final = 1 + kaldi::Rand()%3;
allow_empty = true;
acyclic = false;
weight_multiplier = 0.25;
}
};
/// Returns a random FST. Useful for randomized algorithm testing.
/// Only works if weight can be constructed from float.
template<class Arc> VectorFst<Arc>* RandFst(RandFstOptions opts = RandFstOptions() ) {
typedef typename Arc::StateId StateId;
typedef typename Arc::Weight Weight;
VectorFst<Arc> *fst = new VectorFst<Arc>();
start:
// Create states.
vector<StateId> all_states;
for (size_t i = 0;i < (size_t)opts.n_states;i++) {
StateId this_state = fst->AddState();
if (i == 0) fst->SetStart(i);
all_states.push_back(this_state);
}
// Set final states.
for (size_t j = 0;j < (size_t)opts.n_final;j++) {
StateId id = all_states[kaldi::Rand() % opts.n_states];
Weight weight = (Weight)(opts.weight_multiplier*(kaldi::Rand() % 5));
fst->SetFinal(id, weight);
}
// Create arcs.
for (size_t i = 0;i < (size_t)opts.n_arcs;i++) {
Arc a;
StateId start_state;
if(!opts.acyclic) { // no restriction on arcs.
start_state = all_states[kaldi::Rand() % opts.n_states];
a.nextstate = all_states[kaldi::Rand() % opts.n_states];
} else {
start_state = all_states[kaldi::Rand() % (opts.n_states-1)];
a.nextstate = start_state + 1 + (kaldi::Rand() % (opts.n_states-start_state-1));
}
a.ilabel = kaldi::Rand() % opts.n_syms;
a.olabel = kaldi::Rand() % opts.n_syms; // same input+output vocab.
a.weight = (Weight) (opts.weight_multiplier*(kaldi::Rand() % 4));
fst->AddArc(start_state, a);
}
// Trim resulting FST.
Connect(fst);
if (opts.acyclic)
assert(fst->Properties(kAcyclic, true) & kAcyclic);
if (fst->Start() == kNoStateId && !opts.allow_empty) {
goto start;
}
return fst;
}
/// Returns a random FST. Useful for randomized algorithm testing.
/// Only works if weight can be constructed from a pair of floats
template<class Arc> VectorFst<Arc>* RandPairFst(RandFstOptions opts = RandFstOptions() ) {
typedef typename Arc::StateId StateId;
typedef typename Arc::Weight Weight;
VectorFst<Arc> *fst = new VectorFst<Arc>();
start:
// Create states.
vector<StateId> all_states;
for (size_t i = 0;i < (size_t)opts.n_states;i++) {
StateId this_state = fst->AddState();
if (i == 0) fst->SetStart(i);
all_states.push_back(this_state);
}
// Set final states.
for (size_t j = 0; j < (size_t)opts.n_final;j++) {
StateId id = all_states[kaldi::Rand() % opts.n_states];
Weight weight (opts.weight_multiplier*(kaldi::Rand() % 5), opts.weight_multiplier*(kaldi::Rand() % 5));
fst->SetFinal(id, weight);
}
// Create arcs.
for (size_t i = 0;i < (size_t)opts.n_arcs;i++) {
Arc a;
StateId start_state;
if(!opts.acyclic) { // no restriction on arcs.
start_state = all_states[kaldi::Rand() % opts.n_states];
a.nextstate = all_states[kaldi::Rand() % opts.n_states];
} else {
start_state = all_states[kaldi::Rand() % (opts.n_states-1)];
a.nextstate = start_state + 1 + (kaldi::Rand() % (opts.n_states-start_state-1));
}
a.ilabel = kaldi::Rand() % opts.n_syms;
a.olabel = kaldi::Rand() % opts.n_syms; // same input+output vocab.
a.weight = Weight (opts.weight_multiplier*(kaldi::Rand() % 4), opts.weight_multiplier*(kaldi::Rand() % 4));
fst->AddArc(start_state, a);
}
// Trim resulting FST.
Connect(fst);
if (opts.acyclic)
assert(fst->Properties(kAcyclic, true) & kAcyclic);
if (fst->Start() == kNoStateId && !opts.allow_empty) {
goto start;
}
return fst;
}
} // end namespace fst.
#endif