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

SpanLength.cpp « FF « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a413013c0237a3813c6a59fb6af1eb77f335a29 (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
#include <boost/shared_ptr.hpp>
#include "SpanLength.h"
#include "moses/StaticData.h"
#include "moses/Word.h"
#include "moses/ChartCellLabel.h"
#include "moses/Range.h"
#include "moses/StackVec.h"
#include "moses/TargetPhrase.h"
#include "moses/PP/PhraseProperty.h"
#include "moses/PP/SpanLengthPhraseProperty.h"

using namespace std;

namespace Moses
{
SpanLength::SpanLength(const std::string &line)
  :StatelessFeatureFunction(1, line)
  ,m_smoothingMethod(None)
  ,m_const(0)
{
  ReadParameters();
}

void SpanLength::EvaluateInIsolation(const Phrase &source
                                     , const TargetPhrase &targetPhrase
                                     , ScoreComponentCollection &scoreBreakdown
                                     , ScoreComponentCollection &estimatedScores) const
{
  targetPhrase.SetRuleSource(source);
}

void SpanLength::EvaluateWithSourceContext(const InputType &input
    , const InputPath &inputPath
    , const TargetPhrase &targetPhrase
    , const StackVec *stackVec
    , ScoreComponentCollection &scoreBreakdown
    , ScoreComponentCollection *estimatedScores) const
{
  assert(stackVec);

  const PhraseProperty *property = targetPhrase.GetProperty("SpanLength");
  if (property == NULL) {
    return;
  }

  const SpanLengthPhraseProperty *slProp = static_cast<const SpanLengthPhraseProperty*>(property);

  assert(targetPhrase.GetRuleSource());

  float score = 0;
  for (size_t i = 0; i < stackVec->size(); ++i) {
    const ChartCellLabel &cell = *stackVec->at(i);
    const Range &ntRange = cell.GetCoverage();
    size_t sourceWidth = ntRange.GetNumWordsCovered();
    float prob = slProp->GetProb(i, sourceWidth, m_const);
    score += TransformScore(prob);
  }

  if (score < -100.0f) {
    float weight = StaticData::Instance().GetWeight(this);
    if (weight < 0) {
      score = -100;
    }
  }

  scoreBreakdown.PlusEquals(this, score);

}

void SpanLength::SetParameter(const std::string& key, const std::string& value)
{
  if (key == "smoothing") {
    if (value == "plus-constant") {
      m_smoothingMethod = PlusConst;
    } else if (value == "none") {
      m_smoothingMethod = None;
    } else {
      UTIL_THROW(util::Exception, "Unknown smoothing type " << value);
    }
  } else if (key == "constant") {
    m_const = Scan<float>(value);
  } else {
    StatelessFeatureFunction::SetParameter(key, value);
  }
}

}