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

RulePairUnlexicalizedSource.cpp « FF « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 236bf76afb92051c4f096a94727d95830cc523f2 (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
#include "RulePairUnlexicalizedSource.h"
#include "moses/StaticData.h"
#include "moses/InputFileStream.h"
#include "moses/ScoreComponentCollection.h"
#include "moses/FactorCollection.h"
#include <sstream>
#include "util/string_stream.hh"

using namespace std;

namespace Moses
{

RulePairUnlexicalizedSource::RulePairUnlexicalizedSource(const std::string &line)
  : StatelessFeatureFunction(1, line)
  , m_glueRules(false)
  , m_nonGlueRules(true)
  , m_glueTargetLHSStr("Q")
{
  VERBOSE(1, "Initializing feature " << GetScoreProducerDescription() << " ...");
  ReadParameters();
  FactorCollection &factorCollection = FactorCollection::Instance();
  m_glueTargetLHS = factorCollection.AddFactor(m_glueTargetLHSStr, true);
  VERBOSE(1, " Done.");
}

void RulePairUnlexicalizedSource::SetParameter(const std::string& key, const std::string& value)
{
  if (key == "glueRules") {
    m_glueRules = Scan<bool>(value);
  } else if (key == "nonGlueRules") {
    m_nonGlueRules = Scan<bool>(value);
  } else if (key == "glueTargetLHS") {
    m_glueTargetLHSStr = value;
  } else {
    StatelessFeatureFunction::SetParameter(key, value);
  }
}


void RulePairUnlexicalizedSource::EvaluateInIsolation(const Phrase &source
    , const TargetPhrase &targetPhrase
    , ScoreComponentCollection &scoreBreakdown
    , ScoreComponentCollection &estimatedScores) const
{
  const Factor* targetPhraseLHS = targetPhrase.GetTargetLHS()[0];
  if ( !m_glueRules && (targetPhraseLHS == m_glueTargetLHS) ) {
    return;
  }
  if ( !m_nonGlueRules && (targetPhraseLHS != m_glueTargetLHS) ) {
    return;
  }

  for (size_t posS=0; posS<source.GetSize(); ++posS) {
    const Word &wordS = source.GetWord(posS);
    if ( !wordS.IsNonTerminal() ) {
      return;
    }
  }

  util::StringStream namestr;

  for (size_t posT=0; posT<targetPhrase.GetSize(); ++posT) {
    const Word &wordT = targetPhrase.GetWord(posT);
    const Factor* factorT = wordT[0];
    if ( wordT.IsNonTerminal() ) {
      namestr << "[";
    }
    namestr << factorT->GetString();
    if ( wordT.IsNonTerminal() ) {
      namestr << "]";
    }
    namestr << "|";
  }

  namestr << targetPhraseLHS->GetString() << "|";

  for (AlignmentInfo::const_iterator it=targetPhrase.GetAlignNonTerm().begin();
       it!=targetPhrase.GetAlignNonTerm().end(); ++it) {
    namestr << "|" << it->first << "-" << it->second;
  }

  scoreBreakdown.PlusEquals(this, namestr.str(), 1);
  if ( targetPhraseLHS != m_glueTargetLHS ) {
    scoreBreakdown.PlusEquals(this, 1);
  }
}

}