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

GlueRuleSynthesizer.cpp « T2S « Syntax « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f50f84629c879977d7a8060ea3762d8f448c1b8a (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
#include "GlueRuleSynthesizer.h"

#include <sstream>

#include "moses/FF/UnknownWordPenaltyProducer.h"
#include "moses/StaticData.h"

namespace Moses
{
namespace Syntax
{
namespace T2S
{

void GlueRuleSynthesizer::SynthesizeRule(const InputTree::Node &node)
{
  const Word &sourceLhs = node.pvertex.symbol;
  boost::scoped_ptr<Phrase> sourceRhs(SynthesizeSourcePhrase(node));
  TargetPhrase *tp = SynthesizeTargetPhrase(node, *sourceRhs);
  TargetPhraseCollection::shared_ptr tpc
  = GetOrCreateTargetPhraseCollection(m_ruleTrie, sourceLhs, *sourceRhs);
  tpc->Add(tp);
}

Phrase *GlueRuleSynthesizer::SynthesizeSourcePhrase(const InputTree::Node &node)
{
  Phrase *phrase = new Phrase(node.children.size());
  for (std::vector<InputTree::Node*>::const_iterator p = node.children.begin();
       p != node.children.end(); ++p) {
    phrase->AddWord((*p)->pvertex.symbol);
  }
  /*
  TODO What counts as an OOV?
    phrase->AddWord() = sourceWord;
    phrase->GetWord(0).SetIsOOV(true);
  */
  return phrase;
}

TargetPhrase *GlueRuleSynthesizer::SynthesizeTargetPhrase(
  const InputTree::Node &node, const Phrase &sourceRhs)
{
  const StaticData &staticData = StaticData::Instance();

  const UnknownWordPenaltyProducer &unknownWordPenaltyProducer =
    UnknownWordPenaltyProducer::Instance();

  TargetPhrase *targetPhrase = new TargetPhrase();

  util::StringStream alignmentSS;
  for (std::size_t i = 0; i < node.children.size(); ++i) {
    const Word &symbol = node.children[i]->pvertex.symbol;
    if (symbol.IsNonTerminal()) {
      targetPhrase->AddWord(staticData.GetOutputDefaultNonTerminal());
    } else {
      // TODO Check this
      Word &targetWord = targetPhrase->AddWord();
      targetWord.CreateUnknownWord(symbol);
    }
    alignmentSS << i << "-" << i << " ";
  }

  // Assign the lowest possible score so that glue rules are only used when
  // absolutely required.
  float score = LOWEST_SCORE;
  targetPhrase->GetScoreBreakdown().Assign(&unknownWordPenaltyProducer, score);
  targetPhrase->EvaluateInIsolation(sourceRhs);
  Word *targetLhs = new Word(staticData.GetOutputDefaultNonTerminal());
  targetPhrase->SetTargetLHS(targetLhs);
  targetPhrase->SetAlignmentInfo(alignmentSS.str());

  return targetPhrase;
}

}  // T2S
}  // Syntax
}  // Moses