diff options
author | Tetsuo Kiso <tetsuo-s@is.naist.jp> | 2012-04-06 20:02:32 +0400 |
---|---|---|
committer | Tetsuo Kiso <tetsuo-s@is.naist.jp> | 2012-04-06 20:02:32 +0400 |
commit | d034eeb703bd56776d0e83675ab4d105cda0e340 (patch) | |
tree | 902424a5e89a1d7efc405d80e1c101c11ecb22c6 /mert/BleuScorerTest.cpp | |
parent | bcc1958d9454085d558966b4be2aa2f6345b33e9 (diff) |
Add test cases for BLEU and sentence-level BLEU+1.
- Move a definition of sentenceLevelBleuPlusOne() from pro.cpp
to BleuScorer.cpp.
- Add check for the length of an input vector.
Diffstat (limited to 'mert/BleuScorerTest.cpp')
-rw-r--r-- | mert/BleuScorerTest.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/mert/BleuScorerTest.cpp b/mert/BleuScorerTest.cpp index 30fac9357..a40949d78 100644 --- a/mert/BleuScorerTest.cpp +++ b/mert/BleuScorerTest.cpp @@ -3,6 +3,7 @@ #define BOOST_TEST_MODULE MertBleuScorer #include <boost/test/unit_test.hpp> +#include <cmath> #include "Ngram.h" #include "Vocabulary.h" #include "Util.h" @@ -110,6 +111,19 @@ void SetUpReferences(BleuScorer& scorer) { } } +const float kEPS = 0.0001f; + +template <typename T> +bool IsAlmostEqual(T expected, T actual) { + if (abs(expected - actual) < kEPS) { + return true; + } else { + cerr << "Fail: expected = " << expected + << " (actual = " << actual << ")" << endl; + return false; + } +} + } // namespace BOOST_AUTO_TEST_CASE(bleu_reference_type) { @@ -204,3 +218,56 @@ BOOST_AUTO_TEST_CASE(bleu_clipped_counts) { BOOST_CHECK_EQUAL(entry.get(5), 4); // trigram BOOST_CHECK_EQUAL(entry.get(7), 3); // fourgram } + +BOOST_AUTO_TEST_CASE(calculate_actual_score) { + BOOST_REQUIRE(4 == kBleuNgramOrder); + vector<int> stats(2 * kBleuNgramOrder + 1); + BleuScorer scorer; + + // unigram + stats[0] = 6; + stats[1] = 6; + + // bigram + stats[2] = 4; + stats[3] = 5; + + // trigram + stats[4] = 2; + stats[5] = 4; + + // fourgram + stats[6] = 1; + stats[7] = 3; + + // reference-length + stats[8] = 7; + + BOOST_CHECK(IsAlmostEqual(0.5115f, scorer.calculateScore(stats))); +} + +BOOST_AUTO_TEST_CASE(sentence_level_bleu) { + BOOST_REQUIRE(4 == kBleuNgramOrder); + vector<float> stats(2 * kBleuNgramOrder + 1); + + // unigram + stats[0] = 6.0; + stats[1] = 6.0; + + // bigram + stats[2] = 4.0; + stats[3] = 5.0; + + // trigram + stats[4] = 2.0; + stats[5] = 4.0; + + // fourgram + stats[6] = 1.0; + stats[7] = 3.0; + + // reference-length + stats[8] = 7.0; + + BOOST_CHECK(IsAlmostEqual(0.5985f, sentenceLevelBleuPlusOne(stats))); +} |