kaldi-lattice-test.cc
4.53 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
163
164
165
166
167
168
169
170
171
172
173
// lat/kaldi-lattice-test.cc
// 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.
#include "lat/kaldi-lattice.h"
#include "fstext/rand-fst.h"
namespace kaldi {
CompactLattice *RandCompactLattice() {
Lattice *fst = fst::RandPairFst<LatticeArc>();
CompactLattice *cfst = new CompactLattice;
ConvertLattice(*fst, cfst);
delete fst;
return cfst;
}
Lattice *RandLattice() {
Lattice *fst = fst::RandPairFst<LatticeArc>();
return fst;
}
void TestCompactLatticeTable(bool binary) {
CompactLatticeWriter writer(binary ? "ark:tmpf" : "ark,t:tmpf");
int N = 10;
std::vector<CompactLattice*> lat_vec(N);
for (int i = 0; i < N; i++) {
char buf[2];
buf[0] = '0' + i;
buf[1] = '\0';
std::string key = "key" + std::string(buf);
CompactLattice *fst = RandCompactLattice();
lat_vec[i] = fst;
writer.Write(key, *fst);
}
writer.Close();
RandomAccessCompactLatticeReader reader("ark:tmpf");
for (int i = 0; i < N; i++) {
char buf[2];
buf[0] = '0' + i;
buf[1] = '\0';
std::string key = "key" + std::string(buf);
const CompactLattice &fst = reader.Value(key);
KALDI_ASSERT(fst::Equal(fst, *(lat_vec[i])));
delete lat_vec[i];
}
}
// Write as CompactLattice, read as Lattice.
void TestCompactLatticeTableCross(bool binary) {
CompactLatticeWriter writer(binary ? "ark:tmpf" : "ark,t:tmpf");
int N = 10;
std::vector<CompactLattice*> lat_vec(N);
for (int i = 0; i < N; i++) {
char buf[2];
buf[0] = '0' + i;
buf[1] = '\0';
std::string key = "key" + std::string(buf);
CompactLattice *fst = RandCompactLattice();
lat_vec[i] = fst;
writer.Write(key, *fst);
}
writer.Close();
RandomAccessLatticeReader reader("ark:tmpf");
for (int i = 0; i < N; i++) {
char buf[2];
buf[0] = '0' + i;
buf[1] = '\0';
std::string key = "key" + std::string(buf);
const Lattice &fst = reader.Value(key);
CompactLattice fst2;
ConvertLattice(fst, &fst2);
KALDI_ASSERT(fst::Equal(fst2, *(lat_vec[i])));
delete lat_vec[i];
}
}
// Lattice, binary.
void TestLatticeTable(bool binary) {
LatticeWriter writer(binary ? "ark:tmpf" : "ark,t:tmpf");
int N = 10;
std::vector<Lattice*> lat_vec(N);
for (int i = 0; i < N; i++) {
char buf[2];
buf[0] = '0' + i;
buf[1] = '\0';
std::string key = "key" + std::string(buf);
Lattice *fst = RandLattice();
lat_vec[i] = fst;
writer.Write(key, *fst);
}
writer.Close();
RandomAccessLatticeReader reader("ark:tmpf");
for (int i = 0; i < N; i++) {
char buf[2];
buf[0] = '0' + i;
buf[1] = '\0';
std::string key = "key" + std::string(buf);
const Lattice &fst = reader.Value(key);
KALDI_ASSERT(fst::Equal(fst, *(lat_vec[i])));
delete lat_vec[i];
}
}
// Write as Lattice, read as CompactLattice.
void TestLatticeTableCross(bool binary) {
LatticeWriter writer(binary ? "ark:tmpf" : "ark,t:tmpf");
int N = 10;
std::vector<Lattice*> lat_vec(N);
for (int i = 0; i < N; i++) {
char buf[2];
buf[0] = '0' + i;
buf[1] = '\0';
std::string key = "key" + std::string(buf);
Lattice *fst = RandLattice();
lat_vec[i] = fst;
writer.Write(key, *fst);
}
writer.Close();
RandomAccessCompactLatticeReader reader("ark:tmpf");
for (int i = 0; i < N; i++) {
char buf[2];
buf[0] = '0' + i;
buf[1] = '\0';
std::string key = "key" + std::string(buf);
const CompactLattice &fst = reader.Value(key);
Lattice fst2;
ConvertLattice(fst, &fst2);
KALDI_ASSERT(fst::RandEquivalent(fst2, *(lat_vec[i]), 5, 0.01, Rand(), 10));
delete lat_vec[i];
}
}
} // end namespace kaldi
int main() {
using namespace kaldi;
for (int i = 0; i < 2; i++) {
bool binary = (i%2 == 0);
TestCompactLatticeTable(binary);
TestCompactLatticeTableCross(binary);
TestLatticeTable(binary);
TestLatticeTableCross(binary);
}
std::cout << "Test OK\n";
unlink("tmpf");
}