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

PhraseLengthFeatureTest.cpp « defer - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6fb15e71e83233b7612f2b5253eb586d87e960c3 (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
/***********************************************************************
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 <boost/test/unit_test.hpp>

#include "moses/FF/PhraseLengthFeature.h"
#include "moses/FactorCollection.h"
#include "moses/Sentence.h"
#include "moses/TargetPhrase.h"
#include "moses/TranslationOption.h"

using namespace Moses;
using namespace std;

BOOST_AUTO_TEST_SUITE(phrase_length_feature)

//TODO: Factor out setup code so that it can be reused

static Word MakeWord(string text)
{
  FactorCollection &factorCollection = FactorCollection::Instance();
  const Factor* f = factorCollection.AddFactor(Input,0,text);
  Word w;
  w.SetFactor(0,f);
  return w;
}


BOOST_AUTO_TEST_CASE(evaluate)
{
  Word w1 = MakeWord("w1");
  Word w2 = MakeWord("y2");
  Word w3 = MakeWord("x3");
  Word w4 = MakeWord("w4");

  Phrase p1;
  p1.AddWord(w1);
  p1.AddWord(w3);
  p1.AddWord(w4);

  Phrase p2;
  p2.AddWord(w1);
  p2.AddWord(w2);

  Phrase p3;
  p3.AddWord(w2);
  p3.AddWord(w1);
  p3.AddWord(w4);
  p3.AddWord(w4);

  TargetPhrase tp1(p1);
  TargetPhrase tp2(p2);
  TargetPhrase tp3(p3);

  Sentence sentence;
  vector<FactorType> order;
  order.push_back(0);
  stringstream in("the input sentence has 6 words");
  sentence.Read(in, order);

  TranslationOption topt1(WordsRange(0,0), tp1);
  TranslationOption topt2(WordsRange(1,3), tp2);
  TranslationOption topt3(WordsRange(2,3), tp3);

  PhraseBasedFeatureContext context1(topt1,sentence);
  PhraseBasedFeatureContext context2(topt2,sentence);
  PhraseBasedFeatureContext context3(topt3,sentence);

  PhraseLengthFeature plf;

  ScoreComponentCollection acc1,acc2,acc3;

  plf.Evaluate(context1, &acc1);
  BOOST_CHECK_EQUAL(acc1.GetScoreForProducer(&plf, "s1"),1);
  BOOST_CHECK_EQUAL(acc1.GetScoreForProducer(&plf, "t3"),1);
  BOOST_CHECK_EQUAL(acc1.GetScoreForProducer(&plf, "1,3"),1);

  plf.Evaluate(context2, &acc2);
  BOOST_CHECK_EQUAL(acc2.GetScoreForProducer(&plf, "s3"),1);
  BOOST_CHECK_EQUAL(acc2.GetScoreForProducer(&plf, "t2"),1);
  BOOST_CHECK_EQUAL(acc2.GetScoreForProducer(&plf, "3,2"),1);

  plf.Evaluate(context3, &acc3);
  BOOST_CHECK_EQUAL(acc3.GetScoreForProducer(&plf, "s2"),1);
  BOOST_CHECK_EQUAL(acc3.GetScoreForProducer(&plf, "t4"),1);
  BOOST_CHECK_EQUAL(acc3.GetScoreForProducer(&plf, "2,4"),1);
}

BOOST_AUTO_TEST_SUITE_END()