// weight_test.h // 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 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // Copyright 2005-2010 Google, Inc. // Author: riley@google.com (Michael Riley) // // \file // Regression test for Fst weights. #include #include #include #include #include #include "./weight-tester.h" DEFINE_int32(seed, -1, "random seed"); DEFINE_int32(repeat, 100000, "number of test repetitions"); using fst::TropicalWeight; using fst::TropicalWeightGenerator; using fst::TropicalWeightTpl; using fst::TropicalWeightGenerator_; using fst::LogWeight; using fst::LogWeightGenerator; using fst::LogWeightTpl; using fst::LogWeightGenerator_; using fst::MinMaxWeight; using fst::MinMaxWeightGenerator; using fst::MinMaxWeightTpl; using fst::MinMaxWeightGenerator_; using fst::StringWeight; using fst::StringWeightGenerator; using fst::GallicWeight; using fst::GallicWeightGenerator; using fst::LexicographicWeight; using fst::LexicographicWeightGenerator; using fst::ProductWeight; using fst::ProductWeightGenerator; using fst::PowerWeight; using fst::PowerWeightGenerator; using fst::SignedLogWeightTpl; using fst::SignedLogWeightGenerator_; using fst::ExpectationWeight; using fst::SparsePowerWeight; using fst::SparsePowerWeightGenerator; using fst::STRING_LEFT; using fst::STRING_RIGHT; using fst::WeightTester; template void TestTemplatedWeights(int repeat, int seed) { TropicalWeightGenerator_ tropical_generator(seed); WeightTester, TropicalWeightGenerator_ > tropical_tester(tropical_generator); tropical_tester.Test(repeat); LogWeightGenerator_ log_generator(seed); WeightTester, LogWeightGenerator_ > log_tester(log_generator); log_tester.Test(repeat); MinMaxWeightGenerator_ minmax_generator(seed); WeightTester, MinMaxWeightGenerator_ > minmax_tester(minmax_generator); minmax_tester.Test(repeat); SignedLogWeightGenerator_ signedlog_generator(seed); WeightTester, SignedLogWeightGenerator_ > signedlog_tester(signedlog_generator); signedlog_tester.Test(repeat); } int main(int argc, char **argv) { std::set_new_handler(FailedNewHandler); SET_FLAGS(argv[0], &argc, &argv, true); int seed = FLAGS_seed >= 0 ? FLAGS_seed : time(0); LOG(INFO) << "Seed = " << seed; TestTemplatedWeights(FLAGS_repeat, seed); TestTemplatedWeights(FLAGS_repeat, seed); FLAGS_fst_weight_parentheses = "()"; TestTemplatedWeights(FLAGS_repeat, seed); TestTemplatedWeights(FLAGS_repeat, seed); FLAGS_fst_weight_parentheses = ""; // Make sure type names for templated weights are consistent CHECK(TropicalWeight::Type() == "tropical"); CHECK(TropicalWeightTpl::Type() != TropicalWeightTpl::Type()); CHECK(LogWeight::Type() == "log"); CHECK(LogWeightTpl::Type() != LogWeightTpl::Type()); TropicalWeightTpl w(15.0); TropicalWeight tw(15.0); StringWeightGenerator left_string_generator(seed); WeightTester, StringWeightGenerator > left_string_tester(left_string_generator); left_string_tester.Test(FLAGS_repeat); StringWeightGenerator right_string_generator(seed); WeightTester, StringWeightGenerator > right_string_tester(right_string_generator); right_string_tester.Test(FLAGS_repeat); typedef GallicWeight TropicalGallicWeight; typedef GallicWeightGenerator TropicalGallicWeightGenerator; TropicalGallicWeightGenerator tropical_gallic_generator(seed); WeightTester tropical_gallic_tester(tropical_gallic_generator); tropical_gallic_tester.Test(FLAGS_repeat); typedef ProductWeight TropicalProductWeight; typedef ProductWeightGenerator TropicalProductWeightGenerator; TropicalProductWeightGenerator tropical_product_generator(seed); WeightTester tropical_product_weight_tester(tropical_product_generator); tropical_product_weight_tester.Test(FLAGS_repeat); typedef PowerWeight TropicalCubeWeight; typedef PowerWeightGenerator TropicalCubeWeightGenerator; TropicalCubeWeightGenerator tropical_cube_generator(seed); WeightTester tropical_cube_weight_tester(tropical_cube_generator); tropical_cube_weight_tester.Test(FLAGS_repeat); typedef ProductWeight SecondNestedProductWeight; typedef ProductWeightGenerator SecondNestedProductWeightGenerator; SecondNestedProductWeightGenerator second_nested_product_generator(seed); WeightTester second_nested_product_weight_tester(second_nested_product_generator); second_nested_product_weight_tester.Test(FLAGS_repeat); // This only works with fst_weight_parentheses = "()" typedef ProductWeight FirstNestedProductWeight; typedef ProductWeightGenerator FirstNestedProductWeightGenerator; FirstNestedProductWeightGenerator first_nested_product_generator(seed); WeightTester first_nested_product_weight_tester(first_nested_product_generator); typedef PowerWeight NestedProductCubeWeight; typedef PowerWeightGenerator NestedProductCubeWeightGenerator; NestedProductCubeWeightGenerator nested_product_cube_generator(seed); WeightTester nested_product_cube_weight_tester(nested_product_cube_generator); typedef SparsePowerWeight SparseNestedProductCubeWeight; typedef SparsePowerWeightGenerator SparseNestedProductCubeWeightGenerator; SparseNestedProductCubeWeightGenerator sparse_nested_product_cube_generator(seed); WeightTester sparse_nested_product_cube_weight_tester( sparse_nested_product_cube_generator); typedef SparsePowerWeight LogSparsePowerWeight; typedef SparsePowerWeightGenerator LogSparsePowerWeightGenerator; LogSparsePowerWeightGenerator log_sparse_power_weight_generator(seed); WeightTester log_sparse_power_weight_tester( log_sparse_power_weight_generator); typedef ExpectationWeight LogLogExpectWeight; typedef ProductWeightGenerator LogLogExpectWeightGenerator; LogLogExpectWeightGenerator log_log_expect_weight_generator(seed); WeightTester log_log_expect_weight_tester(log_log_expect_weight_generator); typedef ExpectationWeight LogLogSparseExpectWeight; typedef ProductWeightGenerator< LogWeightGenerator, LogSparsePowerWeightGenerator, LogLogSparseExpectWeight> LogLogSparseExpectWeightGenerator; LogLogSparseExpectWeightGenerator log_logsparse_expect_weight_generator(seed); WeightTester log_logsparse_expect_weight_tester(log_logsparse_expect_weight_generator); // Test all product weight I/O with parentheses FLAGS_fst_weight_parentheses = "()"; first_nested_product_weight_tester.Test(FLAGS_repeat); nested_product_cube_weight_tester.Test(FLAGS_repeat); log_sparse_power_weight_tester.Test(1); sparse_nested_product_cube_weight_tester.Test(1); tropical_product_weight_tester.Test(5); second_nested_product_weight_tester.Test(5); tropical_gallic_tester.Test(5); tropical_cube_weight_tester.Test(5); FLAGS_fst_weight_parentheses = ""; log_sparse_power_weight_tester.Test(1); log_log_expect_weight_tester.Test(1, false); // disables division log_logsparse_expect_weight_tester.Test(1, false); typedef LexicographicWeight TropicalLexicographicWeight; typedef LexicographicWeightGenerator TropicalLexicographicWeightGenerator; TropicalLexicographicWeightGenerator tropical_lexicographic_generator(seed); WeightTester tropical_lexicographic_tester(tropical_lexicographic_generator); tropical_lexicographic_tester.Test(FLAGS_repeat); cout << "PASS" << endl; return 0; }