2010-10-12 17:00:36 +04:00
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2010 University of Edinburgh
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
***********************************************************************/
|
|
|
|
|
2010-10-15 01:52:35 +04:00
|
|
|
#include <stdexcept>
|
2010-10-12 17:00:36 +04:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2010-10-15 01:52:35 +04:00
|
|
|
#include "DummyScoreProducers.h"
|
|
|
|
#include "FeatureFunction.h"
|
|
|
|
#include "ScoreComponentCollection.h"
|
2010-10-12 17:00:36 +04:00
|
|
|
|
|
|
|
using namespace Moses;
|
2010-10-15 01:52:35 +04:00
|
|
|
using namespace std;
|
2010-10-12 17:00:36 +04:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(scc)
|
|
|
|
|
|
|
|
class MockSingleFeature : public StatelessFeatureFunction {
|
|
|
|
public:
|
2011-11-09 01:22:34 +04:00
|
|
|
MockSingleFeature(): StatelessFeatureFunction("MockSingle",1) {}
|
2011-09-20 14:23:38 +04:00
|
|
|
std::string GetScoreProducerWeightShortName(unsigned) const {return "sf";}
|
2010-10-12 17:00:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class MockMultiFeature : public StatelessFeatureFunction {
|
|
|
|
public:
|
2011-11-09 01:22:34 +04:00
|
|
|
MockMultiFeature(): StatelessFeatureFunction("MockMulti", 5) {}
|
2011-09-20 14:23:38 +04:00
|
|
|
std::string GetScoreProducerWeightShortName(unsigned) const {return "mf";}
|
2010-10-12 17:00:36 +04:00
|
|
|
};
|
|
|
|
|
2010-10-15 01:52:35 +04:00
|
|
|
class MockSparseFeature : public StatelessFeatureFunction {
|
|
|
|
public:
|
2011-11-09 01:22:34 +04:00
|
|
|
MockSparseFeature(): StatelessFeatureFunction("MockSparse", ScoreProducer::unlimited) {}
|
2011-09-20 14:23:38 +04:00
|
|
|
std::string GetScoreProducerWeightShortName(unsigned) const {return "sf";}
|
2010-10-15 01:52:35 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-10-12 17:00:36 +04:00
|
|
|
struct MockProducers {
|
2010-10-15 01:52:35 +04:00
|
|
|
MockProducers() {}
|
2010-10-12 17:00:36 +04:00
|
|
|
|
2010-10-15 01:52:35 +04:00
|
|
|
MockSingleFeature single;
|
|
|
|
MockMultiFeature multi;
|
|
|
|
MockSparseFeature sparse;
|
2010-10-12 17:00:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(ctor, MockProducers)
|
|
|
|
{
|
|
|
|
ScoreComponentCollection scc;
|
2010-10-15 01:52:35 +04:00
|
|
|
BOOST_CHECK_EQUAL(scc.GetScoreForProducer(&single),0);
|
2010-10-12 17:00:36 +04:00
|
|
|
float expected[] = {0,0,0,0,0};
|
2010-10-15 01:52:35 +04:00
|
|
|
std::vector<float> actual= scc.GetScoresForProducer(&multi);
|
2010-10-12 17:00:36 +04:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected+5, actual.begin(), actual.begin()+5);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(plusequals, MockProducers)
|
|
|
|
{
|
|
|
|
float arr1[] = {1,2,3,4,5};
|
|
|
|
float arr2[] = {2,4,6,8,10};
|
|
|
|
std::vector<float> vec1(arr1,arr1+5);
|
|
|
|
std::vector<float> vec2(arr2,arr2+5);
|
|
|
|
|
|
|
|
ScoreComponentCollection scc;
|
2010-10-15 01:52:35 +04:00
|
|
|
scc.PlusEquals(&single, 3.4f);
|
|
|
|
BOOST_CHECK_EQUAL(scc.GetScoreForProducer(&single), 3.4f);
|
|
|
|
scc.PlusEquals(&multi,vec1);
|
|
|
|
std::vector<float> actual = scc.GetScoresForProducer(&multi);
|
2010-10-12 17:00:36 +04:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(vec1.begin(),vec1.end()
|
|
|
|
,actual.begin(), actual.end());
|
2010-10-15 01:52:35 +04:00
|
|
|
scc.PlusEquals(&multi,vec1);
|
|
|
|
actual = scc.GetScoresForProducer(&multi);
|
2010-10-12 17:00:36 +04:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(vec2.begin(),vec2.end(),
|
|
|
|
actual.begin(), actual.end());
|
|
|
|
|
2010-10-15 01:52:35 +04:00
|
|
|
BOOST_CHECK_EQUAL(scc.GetScoreForProducer(&single), 3.4f);
|
2010-10-12 17:00:36 +04:00
|
|
|
}
|
|
|
|
|
2010-10-15 01:52:35 +04:00
|
|
|
BOOST_FIXTURE_TEST_CASE(sparse_feature, MockProducers)
|
2010-10-12 17:00:36 +04:00
|
|
|
{
|
2010-10-15 01:52:35 +04:00
|
|
|
ScoreComponentCollection scc;
|
|
|
|
scc.Assign(&sparse, "first", 1.3f);
|
|
|
|
scc.Assign(&sparse, "second", 2.1f);
|
|
|
|
BOOST_CHECK_EQUAL( scc.GetScoreForProducer(&sparse,"first"), 1.3f);
|
|
|
|
BOOST_CHECK_EQUAL( scc.GetScoreForProducer(&sparse,"second"), 2.1f);
|
|
|
|
BOOST_CHECK_EQUAL( scc.GetScoreForProducer(&sparse,"third"), 0.0f);
|
|
|
|
scc.Assign(&sparse, "first", -1.9f);
|
|
|
|
BOOST_CHECK_EQUAL( scc.GetScoreForProducer(&sparse,"first"), -1.9f);
|
|
|
|
scc.PlusEquals(&sparse, "first", -1.9f);
|
|
|
|
BOOST_CHECK_EQUAL( scc.GetScoreForProducer(&sparse,"first"), -3.8f);
|
2010-10-12 17:00:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|