Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ScoreComponentCollectionTest.cpp « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: da7e99ea10324ddc46c50341e68759a43f3098c0 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
/***********************************************************************
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
***********************************************************************/

#include <stdexcept>

#include <boost/test/unit_test.hpp>

#include "moses/FF/StatelessFeatureFunction.h"
#include "ScoreComponentCollection.h"

using namespace Moses;
using namespace std;

BOOST_AUTO_TEST_SUITE(scc)

class MockStatelessFeatureFunction : public StatelessFeatureFunction
{
public:
  MockStatelessFeatureFunction(size_t n, const string &line) :
    StatelessFeatureFunction(n, line) {}
  void EvaluateWhenApplied(const Hypothesis&, ScoreComponentCollection*) const {}
  void EvaluateWhenApplied(const ChartHypothesis&, ScoreComponentCollection*) const {}
  void EvaluateWithSourceContext(const InputType &input
                                 , const InputPath &inputPath
                                 , const TargetPhrase &targetPhrase
                                 , const StackVec *stackVec
                                 , ScoreComponentCollection &scoreBreakdown
                                 , ScoreComponentCollection *estimatedScores) const {
  }

  void EvaluateTranslationOptionListWithSourceContext(const InputType &input
      , const TranslationOptionList &translationOptionList) const {
  }
  void EvaluateInIsolation(const Phrase &source
                           , const TargetPhrase &targetPhrase
                           , ScoreComponentCollection &scoreBreakdown
                           , ScoreComponentCollection &estimatedScores) const {
  }

};

class MockSingleFeature : public MockStatelessFeatureFunction
{
public:
  MockSingleFeature(): MockStatelessFeatureFunction(1, "MockSingle") {}

  bool IsUseable(const FactorMask &mask) const {
    return true;
  }
};

class MockMultiFeature : public MockStatelessFeatureFunction
{
public:
  MockMultiFeature(): MockStatelessFeatureFunction(5, "MockMulti") {}

  bool IsUseable(const FactorMask &mask) const {
    return true;
  }

};

class MockSparseFeature : public MockStatelessFeatureFunction
{
public:
  MockSparseFeature(): MockStatelessFeatureFunction(0, "MockSparse") {}

  bool IsUseable(const FactorMask &mask) const {
    return true;
  }
};



struct MockProducers {
  MockProducers() {
    FeatureFunction::Register(&single);
    FeatureFunction::Register(&multi);
    FeatureFunction::Register(&sparse);
  }

  MockSingleFeature single;
  MockMultiFeature multi;
  MockSparseFeature sparse;
};

BOOST_FIXTURE_TEST_CASE(ctor, MockProducers)
{
  ScoreComponentCollection scc;
  BOOST_CHECK_EQUAL(scc.GetScoreForProducer(&single),0);
  float expected[] =  {0,0,0,0,0};
  std::vector<float> actual= scc.GetScoresForProducer(&multi);
  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;
  scc.PlusEquals(&single, 3.4f);
  BOOST_CHECK_EQUAL(scc.GetScoreForProducer(&single), 3.4f);
  scc.PlusEquals(&multi,vec1);
  std::vector<float> actual = scc.GetScoresForProducer(&multi);
  BOOST_CHECK_EQUAL_COLLECTIONS(vec1.begin(),vec1.end()
                                ,actual.begin(), actual.end());
  scc.PlusEquals(&multi,vec1);
  actual = scc.GetScoresForProducer(&multi);
  BOOST_CHECK_EQUAL_COLLECTIONS(vec2.begin(),vec2.end(),
                                actual.begin(), actual.end());

  BOOST_CHECK_EQUAL(scc.GetScoreForProducer(&single), 3.4f);
}

BOOST_FIXTURE_TEST_CASE(sparse_feature, MockProducers)
{
  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, StringPiece("first"), -1.9f);
  BOOST_CHECK_EQUAL( scc.GetScoreForProducer(&sparse,"first"), -3.8f);
}

/*
 Doesn't work because of the static registration of ScoreProducers
 in ScoreComponentCollection.
BOOST_FIXTURE_TEST_CASE(save, MockProducers)
{
  ScoreComponentCollection scc;
  scc.Assign(&sparse, "first", 1.1f);
  scc.Assign(&single, 0.25f);
  float arr[] = {1,2.1,3,4,5};
  std::vector<float> vec1(arr,arr+5);
  scc.Assign(&multi,vec1);
  ostringstream out;
  scc.Save(out);
  cerr << out.str() << endl;
  istringstream in (out.str());
  string line;
  getline(in,line);
  BOOST_CHECK_EQUAL(line, "MockSingle:4_1 0.25");
  getline(in,line);
  BOOST_CHECK_EQUAL(line, "MockMulti:4_1 1");
  getline(in,line);
  BOOST_CHECK_EQUAL(line, "MockMulti:4_2 2.1");
  getline(in,line);
  BOOST_CHECK_EQUAL(line, "MockMulti:4_3 3");
  getline(in,line);
  BOOST_CHECK_EQUAL(line, "MockMulti:4_4 4");
  getline(in,line);
  BOOST_CHECK_EQUAL(line, "MockMulti:4_5 5");
  getline(in,line);
  BOOST_CHECK_EQUAL(line,"MockSparse:4_first 1.1");
  BOOST_CHECK(!getline(in,line));
}
*/


BOOST_AUTO_TEST_SUITE_END()