27 std::vector<int32> phones;
32 int32 N = 2 + rand() % 2,
35 std::vector<int32> num_pdf_classes;
39 true, &num_pdf_classes);
45 if (ctx_dep_out == NULL)
delete ctx_dep;
46 else *ctx_dep_out = ctx_dep;
51 std::vector<int32> phones(phones_in);
52 std::sort(phones.begin(), phones.end());
55 std::ostringstream topo_string;
56 topo_string <<
"<Topology>\n" 59 for (
size_t i = 0;
i < phones.size();
i++)
60 topo_string << phones[
i] <<
" ";
62 topo_string <<
"</ForPhones>\n" 63 "<State> 0 <PdfClass> 0\n" 64 "<Transition> 0 0.5\n" 65 "<Transition> 1 0.5\n" 67 "<State> 1 <PdfClass> 1 \n" 68 "<Transition> 1 0.5\n" 69 "<Transition> 2 0.5\n" 71 " <State> 2 <PdfClass> 2\n" 72 " <Transition> 2 0.5\n" 73 " <Transition> 3 0.5\n" 75 " <State> 3 </State>\n" 80 std::istringstream iss(topo_string.str());
81 topo.
Read(iss,
false);
88 const std::vector<int32> &num_pdf_classes) {
89 std::vector<int32> phones(phones_in);
90 std::sort(phones.begin(), phones.end());
93 std::ostringstream topo_string;
95 std::map<int32, std::vector<int32> > num_pdf_classes_to_phones;
96 for (
size_t i = 0;
i < phones.size();
i++) {
98 KALDI_ASSERT(static_cast<size_t>(p) < num_pdf_classes.size());
99 int32 n = num_pdf_classes[p];
100 KALDI_ASSERT(n > 0 &&
"num-pdf-classes cannot be zero.");
101 num_pdf_classes_to_phones[
n].push_back(p);
104 topo_string <<
"<Topology>\n";
105 std::map<int32, std::vector<int32> >::const_iterator
106 iter = num_pdf_classes_to_phones.begin(),
107 end = num_pdf_classes_to_phones.end();
108 for (; iter != end; ++iter) {
109 topo_string <<
"<TopologyEntry>\n" 111 int32 this_num_pdf_classes = iter->first;
112 const std::vector<int32> &phones = iter->second;
113 for (
size_t i = 0;
i < phones.size();
i++)
114 topo_string << phones[
i] <<
" ";
115 topo_string <<
"</ForPhones> ";
116 bool ergodic = (
RandInt(0, 1) == 0);
121 std::vector<int32> state_to_pdf_class;
122 state_to_pdf_class.push_back(-1);
123 for (
int32 i = 0;
i < this_num_pdf_classes;
i++) {
125 for (
int32 j = 0;
j < num_states;
j++)
126 state_to_pdf_class.push_back(
i);
128 state_to_pdf_class.push_back(-1);
131 topo_string <<
"<State> 0\n";
132 BaseFloat prob = 1.0 / (state_to_pdf_class.size() - 2);
133 for (
size_t i = 1;
i + 1 < state_to_pdf_class.size();
i++) {
134 topo_string <<
"<Transition> " <<
i <<
' ' << prob <<
'\n';
136 topo_string <<
"</State>\n";
139 for (
size_t i = 1;
i + 1 < state_to_pdf_class.size();
i++) {
140 BaseFloat prob = 1.0 / (state_to_pdf_class.size() - 1);
141 topo_string <<
"<State> " <<
i <<
" <PdfClass> " 142 << state_to_pdf_class[
i] <<
'\n';
143 for (
size_t j = 1;
j < state_to_pdf_class.size();
j++)
144 topo_string <<
"<Transition> " <<
j <<
' ' << prob <<
'\n';
145 topo_string <<
"</State>\n";
148 topo_string <<
"<State> " << (state_to_pdf_class.size() - 1) <<
" </State>\n";
152 for (
int32 pdf_class = 0; pdf_class < this_num_pdf_classes; pdf_class++) {
154 for (
int32 s = 0; s < this_num_states; s++) {
155 topo_string <<
"<State> " << cur_state <<
" <PdfClass> " << pdf_class
156 <<
"\n<Transition> " << cur_state <<
" 0.5\n<Transition> " 157 << (cur_state + 1) <<
" 0.5\n</State>\n";
162 topo_string <<
"<State> " << cur_state <<
" </State>\n";
164 topo_string <<
"</TopologyEntry>\n";
166 topo_string <<
"</Topology>\n";
169 std::istringstream iss(topo_string.str());
170 topo.
Read(iss,
false);
175 std::vector<int32> phones;
183 std::vector<int32> num_pdf_classes(phones.back() + 1, -1);
184 for (
int32 i = 0;
i < phones.size();
i++)
185 num_pdf_classes[phones[
i]] =
RandInt(1, 5);
193 std::vector<std::pair<int32, int32> > *path) {
198 int32 num_states = this_entry.size(), final_state = num_states - 1;
201 std::vector<std::pair<int32, int32> > pending_self_loops;
202 while (cur_state != final_state) {
205 transition_index =
RandInt(0, num_transitions - 1);
207 std::pair<int32, int32> pr(cur_state, transition_index);
211 bool is_self_loop = (cur_state ==
212 cur_hmm_state.
transitions[transition_index].first);
215 pending_self_loops.push_back(pr);
220 path->insert(path->end(), pending_self_loops.begin(),
221 pending_self_loops.end());
222 pending_self_loops.clear();
226 cur_state = cur_hmm_state.
transitions[transition_index].first;
235 const std::vector<int32> &phone_sequence,
236 std::vector<int32> *alignment) {
239 num_phones = phone_sequence.size();
241 for (
int32 i = 0;
i < num_phones;
i++) {
242 std::vector<int32> context_window;
243 context_window.reserve(context_width);
244 for (
int32 j =
i - central_position;
245 j <
i - central_position + context_width;
247 if (
j >= 0 &&
j < num_phones) context_window.push_back(phone_sequence[
j]);
248 else context_window.push_back(0);
252 std::vector<std::pair<int32, int32> > path;
253 int32 phone = phone_sequence[
i];
255 for (
size_t k = 0; k < path.size(); k++) {
258 int32 hmm_state = path[k].first,
259 transition_index = path[k].second,
260 forward_pdf_class = entry[hmm_state].forward_pdf_class,
261 self_loop_pdf_class = entry[hmm_state].self_loop_pdf_class,
262 forward_pdf_id, self_loop_pdf_id;
263 bool ans = ctx_dep.
Compute(context_window, forward_pdf_class, &forward_pdf_id);
264 KALDI_ASSERT(ans &&
"context-dependency computation failed.");
265 ans = ctx_dep.
Compute(context_window, self_loop_pdf_class, &self_loop_pdf_id);
266 KALDI_ASSERT(ans &&
"context-dependency computation failed.");
268 phone, hmm_state, forward_pdf_id, self_loop_pdf_id),
271 alignment->push_back(transition_id);
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
HmmTopology GetDefaultTopology(const std::vector< int32 > &phones_in)
This function returns a HmmTopology object giving a normal 3-state topology, covering all phones in t...
A structure defined inside HmmTopology to represent a HMM state.
int32 PairToTransitionId(int32 trans_state, int32 trans_index) const
ContextDependency * GenRandContextDependencyLarge(const std::vector< int32 > &phone_ids, int N, int P, bool ensure_all_covered, std::vector< int32 > *hmm_lengths)
GenRandContextDependencyLarge is like GenRandContextDependency but generates a larger tree with speci...
A class for storing topology information for phones.
virtual bool Compute(const std::vector< int32 > &phoneseq, int32 pdf_class, int32 *pdf_id) const =0
The "new" Compute interface.
int32 TupleToTransitionState(int32 phone, int32 hmm_state, int32 pdf, int32 self_loop_pdf) const
void Read(std::istream &is, bool binary)
std::vector< HmmState > TopologyEntry
TopologyEntry is a typedef that represents the topology of a single (prototype) state.
void GeneratePathThroughHmm(const HmmTopology &topology, bool reorder, int32 phone, std::vector< std::pair< int32, int32 > > *path)
This function generates a random path through the HMM for the given phone.
HmmTopology GenRandTopology(const std::vector< int32 > &phones_in, const std::vector< int32 > &num_pdf_classes)
This method of generating an arbitrary HmmTopology object allows you to specify the number of pdf-cla...
virtual int CentralPosition() const =0
Central position P of the phone context, in 0-based numbering, e.g.
const TopologyEntry & TopologyForPhone(int32 phone) const
Returns the topology entry (i.e.
const HmmTopology & GetTopo() const
return reference to HMM-topology object.
std::vector< std::pair< int32, BaseFloat > > transitions
A list of transitions, indexed by what we call a 'transition-index'.
void GenerateRandomAlignment(const ContextDependencyInterface &ctx_dep, const TransitionModel &trans_model, bool reorder, const std::vector< int32 > &phone_sequence, std::vector< int32 > *alignment)
For use in test code, this function generates an alignment (a sequence of transition-ids) correspondi...
context-dep-itf.h provides a link between the tree-building code in ../tree/, and the FST code in ...
virtual int ContextWidth() const =0
ContextWidth() returns the value N (e.g.
#define KALDI_ASSERT(cond)
TransitionModel * GenRandTransitionModel(ContextDependency **ctx_dep_out)
int32 forward_pdf_class
The Pdf-classes forward-pdf-class, typically 0, 1 or 2 (the same as the HMM-state index)...
bool IsSortedAndUniq(const std::vector< T > &vec)
Returns true if the vector is sorted and contains each element only once.
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)