hmm-topology-test.cc
Go to the documentation of this file.
1 // hmm/hmm-topology-test.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2015 Johns Hopkins University (author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "hmm/hmm-topology.h"
22 #include "hmm/hmm-test-utils.h"
23 
24 namespace kaldi {
25 
26 
28  bool binary = (Rand()%2 == 0);
29 
30  std::string input_str = "<Topology>\n"
31  "<TopologyEntry>\n"
32  "<ForPhones> 1 2 3 4 5 6 7 8 9 </ForPhones>\n"
33  "<State> 0 <PdfClass> 0\n"
34  "<Transition> 0 0.5\n"
35  "<Transition> 1 0.5\n"
36  "</State> \n"
37  "<State> 1 <PdfClass> 1 \n"
38  "<Transition> 1 0.5\n"
39  "<Transition> 2 0.5\n"
40  "</State> \n"
41  " <State> 2 <PdfClass> 2\n"
42  " <Transition> 2 0.5\n"
43  " <Transition> 3 0.5\n"
44  " </State> \n"
45  " <State> 3 </State>\n"
46  " </TopologyEntry>\n"
47  " <TopologyEntry>\n"
48  " <ForPhones> 10 11 13 </ForPhones>\n"
49  " <State> 0 <PdfClass> 0\n"
50  " <Transition> 0 0.5\n"
51  " <Transition> 1 0.5\n"
52  " </State> \n"
53  " <State> 1 <PdfClass> 1 \n"
54  " <Transition> 1 0.5\n"
55  " <Transition> 2 0.5\n"
56  " </State> \n"
57  " <State> 2 </State>"
58  " </TopologyEntry>\n"
59  " </Topology>\n";
60 
61  std::string chain_input_str = "<Topology>\n"
62  "<TopologyEntry>\n"
63  "<ForPhones> 1 2 3 4 5 6 7 8 9 </ForPhones>\n"
64  " <State> 0 <ForwardPdfClass> 0 <SelfLoopPdfClass> 1\n"
65  " <Transition> 0 0.5\n"
66  " <Transition> 1 0.5\n"
67  " </State> \n"
68  " <State> 1 </State>\n"
69  "</TopologyEntry>\n"
70  "</Topology>\n";
71 
72  HmmTopology topo;
73 
74  if (RandInt(0, 1) == 0) {
75  topo = GenRandTopology();
76  } else {
77  std::istringstream iss(input_str);
78  topo.Read(iss, false);
79  KALDI_ASSERT(topo.MinLength(3) == 3);
80  KALDI_ASSERT(topo.MinLength(11) == 2);
81  }
82 
83  std::ostringstream oss;
84  topo.Write(oss, binary);
85 
86  HmmTopology topo2;
87  // std::cout << oss.str() << '\n' << std::flush;
88  std::istringstream iss2(oss.str());
89  topo2.Read(iss2, binary);
90 
91  { // test equality.
92  std::ostringstream oss1, oss2;
93  topo.Write(oss1, false);
94  topo2.Write(oss2, false);
95  KALDI_ASSERT(oss1.str() == oss2.str());
96  }
97 
98  { // test chain topology
99  HmmTopology chain_topo;
100  std::istringstream chain_iss(chain_input_str);
101  chain_topo.Read(chain_iss, false);
102  KALDI_ASSERT(chain_topo.MinLength(3) == 1);
103  }
104 
105  { // make sure GetDefaultTopology does not crash.
106  std::vector<int32> phones;
107  phones.push_back(1);
108  phones.push_back(2);
109  GetDefaultTopology(phones);
110  }
111 }
112 
113 
114 }
115 
116 int main() {
117  // repeat the test ten times
118  for (int i = 0; i < 10; i++) {
120  }
121  std::cout << "Test OK.\n";
122 }
123 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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 class for storing topology information for phones.
Definition: hmm-topology.h:93
void Read(std::istream &is, bool binary)
Definition: hmm-topology.cc:39
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...
void Write(std::ostream &os, bool binary) const
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
int main()
void TestHmmTopology()
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 MinLength(int32 phone) const
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95