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

RuleScope.cpp « FF « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8a18e0a282431411a439ebd80849087e2ad6b499 (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
#include "RuleScope.h"
#include "moses/StaticData.h"
#include "moses/Word.h"

using namespace std;

namespace Moses
{
RuleScope::RuleScope(const std::string &line)
  :StatelessFeatureFunction(1, line)
  ,m_sourceSyntax(true)
  ,m_perScope(false)
  ,m_futureCostOnly(false)
{
}

bool IsAmbiguous(const Word &word, bool sourceSyntax)
{
  const Word &inputDefaultNonTerminal = StaticData::Instance().GetInputDefaultNonTerminal();
  return word.IsNonTerminal() && (!sourceSyntax || word == inputDefaultNonTerminal);
}

void RuleScope::EvaluateInIsolation(const Phrase &source
                                    , const TargetPhrase &targetPhrase
                                    , ScoreComponentCollection &scoreBreakdown
                                    , ScoreComponentCollection &estimatedScores) const
{
  if (IsGlueRule(source)) {
    return;
  }

  float score = 0;

  if (source.GetSize() > 0 && source.Front().IsNonTerminal()) {
    ++score;
  }
  if (source.GetSize() > 1 && source.Back().IsNonTerminal()) {
    ++score;
  }

  /*
  int count = 0;
  for (size_t i = 0; i < source.GetSize(); ++i) {
    const Word &word = source.GetWord(i);
    bool ambiguous = IsAmbiguous(word, m_sourceSyntax);
    if (ambiguous) {
      ++count;
    } else {
      if (count > 0) {
        score += count;
      }
      count = -1;
    }
  }

  // 1st & last always adjacent to ambiguity
  ++count;
  if (count > 0) {
    score += count;
  }
  */

  if (m_perScope) {
    UTIL_THROW_IF2(m_numScoreComponents <= score,
                   "Insufficient number of score components. Scope=" << score << ". NUmber of score components=" << score);
    vector<float> scores(m_numScoreComponents, 0);
    scores[score] = 1;

    if (m_futureCostOnly) {
      estimatedScores.PlusEquals(this, scores);
    } else {
      scoreBreakdown.PlusEquals(this, scores);
    }
  } else if (m_futureCostOnly) {
    estimatedScores.PlusEquals(this, score);
  } else {
    scoreBreakdown.PlusEquals(this, score);
  }
}

void RuleScope::SetParameter(const std::string& key, const std::string& value)
{
  if (key == "source-syntax") {
    m_sourceSyntax = Scan<bool>(value);
  } else if (key == "per-scope") {
    m_perScope = Scan<bool>(value);
  } else if ("future-cost-only") {
    m_futureCostOnly = Scan<bool>(value);
  } else {
    StatelessFeatureFunction::SetParameter(key, value);
  }
}

bool RuleScope::IsGlueRule(const Phrase &source) const
{
  string sourceStr = source.ToString();
  if (sourceStr == "<s> " || sourceStr == "X </s> " || sourceStr == "X X ") {
    // don't score glue rule
    //cerr << "sourceStr=" << sourceStr << endl;
    return true;
  } else {
    return false;
  }

}

}