30 #include "tropical-sparse-tuple-weight.h" 31 #include "tropical-sparse-tuple-weight-decls.h" 42 namespace bfs = boost::filesystem;
45 TEST ( tropicalsparseweight, makesparseweight ) {
47 fst::StdArc::Weight stdweight ( 3.0f );
50 EXPECT_EQ ( mw2 ( w ).Value(), 3.0f );
54 TEST ( tropicalsparseweight, makeweight2 ) {
56 TupleArc32::Weight w = mw ( 3.0f );
58 EXPECT_EQ ( mw2 ( w ).Value(), 3.0f );
62 TEST ( lexicographic, makeweight_stdarc ) {
64 fst::StdArc::Weight w = mw ( 3.0f );
65 EXPECT_EQ ( 3.0f, w.Value() );
66 fst::StdArc::Weight w2 = 4.0f;
67 EXPECT_EQ ( 4.0f, mw ( w2 ) );
71 TEST ( lexicographic, makeweight_lexstdarc ) {
73 fst::LexStdArc::Weight w = mw ( 3.0f );
74 EXPECT_EQ ( 3.0f, w.Value1() );
75 EXPECT_EQ ( fst::StdArc::Weight::One(), w.Value2() );
76 fst::LexStdArc::Weight w2 ( 6.5f, 7.0f );
77 fst::LexStdArc::Weight w2check ( 6.5f, fst::StdArc::Weight::One() );
78 EXPECT_EQ ( w2check, mw ( w2 ) );
82 TEST ( lexicographic, makeweight2_stdarc ) {
84 fst::StdArc::Weight w = mw ( 2.0f );
85 EXPECT_EQ ( 2.0f, w.Value() );
86 fst::StdArc::Weight w2 = 4.5f;
87 EXPECT_EQ ( 4.5f, mw ( w2 ) );
91 TEST ( lexicographic, makeweight2_lexstdarc ) {
93 fst::LexStdArc::Weight w = mw ( 2.0f );
94 EXPECT_EQ ( 2.0f, w.Value1() );
95 EXPECT_EQ ( 2.0f, w.Value2() );
96 fst::LexStdArc::Weight w2 ( 7.5f, 8.5f );
97 fst::LexStdArc::Weight w2check ( 8.5f, 8.5f );
98 EXPECT_EQ ( w2check, mw ( w2 ) );
102 TEST ( lexicographic, copyw2tow1_stdarc ) {
103 fst::VectorFst<fst::StdArc> a;
107 a.SetFinal ( 1, fst::StdArc::Weight::One() );
108 fst::StdArc::Weight wa ( 3.0f );
109 a.AddArc ( 0, fst::StdArc ( 1, 1, wa, 1 ) );
112 fst::Map<fst::StdArc> ( &a,
115 fst::VectorFst<fst::StdArc> b;
119 b.SetFinal ( 1, fst::StdArc::Weight::One() );
120 fst::StdArc::Weight wb ( 3.0f );
121 b.AddArc ( 0, fst::StdArc ( 1, 1, wb, 1 ) );
122 EXPECT_TRUE ( Equivalent ( a, b ) );
126 TEST ( lexicographic, copyw2tow1_lexstdarc ) {
127 fst::VectorFst<fst::LexStdArc> a;
131 a.SetFinal ( 1, fst::LexStdWeight::One() );
135 fst::Map<fst::LexStdArc> ( &a,
138 fst::VectorFst<fst::LexStdArc> b;
142 b.SetFinal ( 1, fst::LexStdWeight::One() );
145 EXPECT_TRUE ( Equivalent ( a, b ) );
150 TEST(tropicalsparseweight, makeweighthifst) {
154 unordered_map<std::string, boost::any> v;
160 MakeWeightHifst<TupleArc32> mwh(rg);
161 TupleArc32::Weight w = mwh ( 3.0f );
162 EXPECT_TRUE(ucam::util::toString<TupleArc32::Weight>(w, 0) ==
"0,1,2,3," 163 || ucam::util::toString<TupleArc32::Weight>(w, 0) ==
"0,2,3" 165 w = mwh ( 6.0f , 8 );
166 EXPECT_TRUE(ucam::util::toString<TupleArc32::Weight>(w, 0) ==
"0,2,2,6,-10,1," 167 || ucam::util::toString<TupleArc32::Weight>(w, 0) ==
"0,2,6,-10,1" 171 unordered_map<std::string, boost::any> v;
176 MakeWeightHifst<TupleArc32> mwh(rg);
177 TupleArc32::Weight w = mwh ( 3.0f );
178 EXPECT_TRUE(ucam::util::toString<TupleArc32::Weight>(w, 0) ==
"0,1,3,3," 179 || ucam::util::toString<TupleArc32::Weight>(w, 0) ==
"0,3,3" 181 w = mwh ( 6.0f , 8 );
182 EXPECT_TRUE(ucam::util::toString<TupleArc32::Weight>(w, 0) ==
"0,1,3,6," 183 || ucam::util::toString<TupleArc32::Weight>(w, 0) ==
"0,3,6" 192 TEST(tropicalsparseweight, makeweighthifstMapper) {
196 typedef Arc::Weight Weight;
197 fst::VectorFst<Arc> a;
201 a.SetFinal ( 1, Weight::One() );
206 a.AddArc ( 0, Arc ( 1, 1, wa, 1 ) );
207 unordered_map<std::string, boost::any> v;
217 fst::VectorFst<Arc> b;
221 b.SetFinal ( 1, Weight::One() );
226 b.AddArc ( 0, Arc ( 1, 1, wb, 1 ) );
227 EXPECT_TRUE ( Equivalent ( a, b ) );
230 TEST(lexicographic, makeweighthifst) {
232 unordered_map<std::string, boost::any> v;
234 MakeWeightHifst<fst::LexStdArc> mw(rg);
235 fst::LexStdArc::Weight w = mw ( 2.0f );
236 EXPECT_EQ ( 2.0f, w.Value1() );
237 EXPECT_EQ ( 2.0f, w.Value2() );
238 fst::LexStdArc::Weight w2 ( 7.5f, 8.5f );
239 fst::LexStdArc::Weight w2check ( 8.5f, 8.5f );
240 EXPECT_EQ ( w2check, mw ( w2 ) );
255 TEST(tropicalsparseweight, removeKthWeight) {
257 TupleArc32::Weight w,w2;
262 w.SetDefaultValue(300);
263 std::stringstream ss; ss << w;
264 EXPECT_TRUE ( ss.str() ==
"300,3,6,6,5,5,5,-2," 265 || ss.str() ==
"300,6,6,5,5,5,-2");
269 for (SparseTupleWeightIterator<StdArc::Weight, int> it(w); !it.Done(); it.Next()) {
270 if (it.Value().first != 5)
273 std::stringstream ss2; ss2 << w2;
274 EXPECT_TRUE ( ss2.str() ==
"0,1,6,6," 275 || ss2.str() ==
"0,6,6" 277 w2.SetDefaultValue(w.DefaultValue());
278 std::stringstream ss3; ss3 << w2;
279 EXPECT_TRUE ( ss3.str() ==
"300,1,6,6," 280 || ss3.str() ==
"300,6,6" 284 TupleArc32::Weight w3=
Times(w,w2);
295 int main (
int argc,
char **argv ) {
296 ::testing::InitGoogleTest ( &argc, argv );
297 return RUN_ALL_TESTS();
Unit testing: google testing common header.
Convenience functors/functions for lexicographic<tropical,tropical> semiring.
LexStdArc::Weight LexStdWeight
Convenience functions for tropical sparse vector weight.
const std::string kHifstDisableRuleFeatures
templated Mapper that modifies weights over an FST, passing through the other values of the arc...
Implements Tropical Sparse tuple weight semiring, extending from openfst SparsePowerWeight class...
std::string const kLmFeatureweights
Static variables for logger. Include only once from main file.
Templated functor that creates a weight given a float.
Functor to convert sparse tuple weight to tropical (single weight)
Headers for standalone shared library.
Convenience functions to parse parameters from a string.
Templated functor that creates a weight given a float.
LexicographicArc< StdArc::Weight, StdArc::Weight > LexStdArc
Generalized weight mapper functor.
Functor that converts tropical to sparse tuple weight.
int main(int argc, char **argv)
TropicalSparseTupleWeight< T > Times(const TropicalSparseTupleWeight< T > &w1, const TropicalSparseTupleWeight< T > &w2)
Convenience functors that allow transparent handling for weights within hifst.
TEST(tropicalsparseweight, makesparseweight)
Unit testing: google testing common header.
Static variable for custom_assert. Include only once from main file.