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

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

#include <sstream>

#include "moses/FF/UnknownWordPenaltyProducer.h"
#include "moses/StaticData.h"
#include "util/string_stream.hh"

namespace Moses
{
namespace Syntax
{
namespace F2S
{

GlueRuleSynthesizer::GlueRuleSynthesizer(HyperTree &trie)
  : m_hyperTree(trie)
{
  const std::vector<FactorType> &inputFactorOrder =
    StaticData::Instance().GetInputFactorOrder();
  Word *lhs = NULL;
  m_dummySourcePhrase.CreateFromString(Input, inputFactorOrder, "hello", &lhs);
  delete lhs;
}

void GlueRuleSynthesizer::SynthesizeRule(const Forest::Hyperedge &e)
{
  HyperPath source;
  SynthesizeHyperPath(e, source);
  TargetPhrase *tp = SynthesizeTargetPhrase(e);
  TargetPhraseCollection::shared_ptr tpc
  = GetOrCreateTargetPhraseCollection(m_hyperTree, source);
  tpc->Add(tp);
}

void GlueRuleSynthesizer::SynthesizeHyperPath(const Forest::Hyperedge &e,
    HyperPath &path)
{
  path.nodeSeqs.clear();
  path.nodeSeqs.resize(2);
  path.nodeSeqs[0].push_back(e.head->pvertex.symbol[0]->GetId());
  for (std::vector<Forest::Vertex*>::const_iterator p = e.tail.begin();
       p != e.tail.end(); ++p) {
    const Forest::Vertex &child = **p;
    path.nodeSeqs[1].push_back(child.pvertex.symbol[0]->GetId());
  }
}

TargetPhrase *GlueRuleSynthesizer::SynthesizeTargetPhrase(
  const Forest::Hyperedge &e)
{
  const StaticData &staticData = StaticData::Instance();

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

  TargetPhrase *targetPhrase = new TargetPhrase();

  util::StringStream alignmentSS;
  for (std::size_t i = 0; i < e.tail.size(); ++i) {
    const Word &symbol = e.tail[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(m_dummySourcePhrase);
  Word *targetLhs = new Word(staticData.GetOutputDefaultNonTerminal());
  targetPhrase->SetTargetLHS(targetLhs);
  targetPhrase->SetAlignmentInfo(alignmentSS.str());

  return targetPhrase;
}

}  // F2S
}  // Syntax
}  // Moses