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

UnalignedWordCountFeature.cpp « FF « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f1e2a8d9bf62becb32cfb2ee76158c11b3e09df3 (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
#include "UnalignedWordCountFeature.h"
#include "moses/Phrase.h"
#include "moses/TargetPhrase.h"
#include "moses/ScoreComponentCollection.h"
#include "moses/StaticData.h"
#include "moses/Util.h"

namespace Moses
{

using namespace std;

UnalignedWordCountFeature::UnalignedWordCountFeature(const std::string &line)
  : StatelessFeatureFunction(2, line)
{
  VERBOSE(1, "Initializing feature " << GetScoreProducerDescription() << " ...");
  ReadParameters();
  VERBOSE(1, " Done." << std::endl);
}

void UnalignedWordCountFeature::EvaluateInIsolation(const Phrase &source
    , const TargetPhrase &targetPhrase
    , ScoreComponentCollection &scoreBreakdown
    , ScoreComponentCollection &estimatedScores) const
{
  const AlignmentInfo &alignmentInfo = targetPhrase.GetAlignTerm();
  const size_t sourceLength = source.GetSize();
  const size_t targetLength = targetPhrase.GetSize();

  std::vector<bool> alignedSource(sourceLength, false);
  std::vector<bool> alignedTarget(targetLength, false);

  for (AlignmentInfo::const_iterator alignmentPoint = alignmentInfo.begin(); alignmentPoint != alignmentInfo.end(); ++alignmentPoint) {
    alignedSource[ alignmentPoint->first ] = true;
    alignedTarget[ alignmentPoint->second ] = true;
  }

  size_t sourceUnalignedCount = 0;

  for (size_t j=0; j<sourceLength; ++j) {
    if (!alignedSource[j]) {
      if (!source.GetWord(j).IsNonTerminal()) {
        ++sourceUnalignedCount;
      }
    }
  }

  size_t targetUnalignedCount = 0;

  for (size_t i=0; i<targetLength; i++) {
    if (!alignedTarget[i]) {
      if (!targetPhrase.GetWord(i).IsNonTerminal()) {
        ++targetUnalignedCount;
      }
    }
  }

  scoreBreakdown.PlusEquals(m_index, sourceUnalignedCount);
  scoreBreakdown.PlusEquals(m_index+1, targetUnalignedCount);

  IFFEATUREVERBOSE(2) {
    FEATUREVERBOSE(2, source << std::endl);
    FEATUREVERBOSE(2, targetPhrase << std::endl);

    for (AlignmentInfo::const_iterator it=targetPhrase.GetAlignTerm().begin();
         it!=targetPhrase.GetAlignTerm().end(); ++it) {
      FEATUREVERBOSE(2, "alignTerm " << it->first << " " << it->second << std::endl);
    }

    for (AlignmentInfo::const_iterator it=targetPhrase.GetAlignNonTerm().begin();
         it!=targetPhrase.GetAlignNonTerm().end(); ++it) {
      FEATUREVERBOSE(2, "alignNonTerm " << it->first << " " << it->second << std::endl);
    }

    FEATUREVERBOSE(2, "sourceLength= " << sourceLength << std::endl);
    FEATUREVERBOSE(2, "targetLength= " << targetLength << std::endl);
    FEATUREVERBOSE(2, "sourceUnalignedCount= " << sourceUnalignedCount << std::endl);
    FEATUREVERBOSE(2, "targetUnalignedCount= " << targetUnalignedCount << std::endl);
  }
}

}