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

RuleCubeItem.cpp « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fa4b4c51e5f599b9ba84ff43cfb74d5e2cfdcd7b (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/***********************************************************************
 Moses - statistical machine translation system
 Copyright (C) 2006-2011 University of Edinburgh

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ***********************************************************************/

#include "ChartCell.h"
#include "ChartCellCollection.h"
#include "ChartTranslationOption.h"
#include "ChartTranslationOptionCollection.h"
#include "DotChart.h"
#include "RuleCubeItem.h"
#include "RuleCubeQueue.h"
#include "WordsRange.h"
#include "Util.h"

#include <boost/functional/hash.hpp>

namespace Moses
{

std::size_t hash_value(const HypothesisDimension &dimension)
{
  boost::hash<const ChartHypothesis*> hasher;
  return hasher(dimension.GetHypothesis());
}

RuleCubeItem::RuleCubeItem(const ChartTranslationOption &transOpt,
                           const ChartCellCollection &allChartCells)
  : m_translationDimension(0,
                           transOpt.GetTargetPhraseCollection().GetCollection())
  , m_hypothesis(0)
{
  CreateHypothesisDimensions(transOpt.GetDottedRule(), allChartCells);
}

// create the RuleCube from an existing one, differing only in one dimension
RuleCubeItem::RuleCubeItem(const RuleCubeItem &copy, int hypoDimensionIncr)
  : m_translationDimension(copy.m_translationDimension)
  , m_hypothesisDimensions(copy.m_hypothesisDimensions)
  , m_hypothesis(0)
{
  if (hypoDimensionIncr == -1) {
    m_translationDimension.IncrementPos();
  } else {
    HypothesisDimension &dimension = m_hypothesisDimensions[hypoDimensionIncr];
    dimension.IncrementPos();
  }
}

RuleCubeItem::~RuleCubeItem()
{
  delete m_hypothesis;
}

void RuleCubeItem::EstimateScore()
{
  m_score = m_translationDimension.GetTargetPhrase()->GetFutureScore();
  std::vector<HypothesisDimension>::const_iterator p;
  for (p = m_hypothesisDimensions.begin();
       p != m_hypothesisDimensions.end(); ++p) {
    m_score += p->GetHypothesis()->GetTotalScore();
  }
}

void RuleCubeItem::CreateHypothesis(const ChartTranslationOption &transOpt,
                                    ChartManager &manager)
{
  m_hypothesis = new ChartHypothesis(transOpt, *this, manager);
  m_hypothesis->CalcScore();
  m_score = m_hypothesis->GetTotalScore();
}

ChartHypothesis *RuleCubeItem::ReleaseHypothesis()
{
  CHECK(m_hypothesis);
  ChartHypothesis *hypo = m_hypothesis;
  m_hypothesis = 0;
  return hypo;
}

// for each non-terminal, create a ordered list of matching hypothesis from the
// chart
void RuleCubeItem::CreateHypothesisDimensions(
  const DottedRule &dottedRule,
  const ChartCellCollection &allChartCells)
{
  CHECK(!dottedRule.IsRoot());

  const DottedRule *prev = dottedRule.GetPrev();
  if (!prev->IsRoot()) {
    CreateHypothesisDimensions(*prev, allChartCells);
  }

  // only deal with non-terminals
  if (dottedRule.IsNonTerminal()) {
    // get a sorted list of the underlying hypotheses
    const ChartCellLabel &cellLabel = dottedRule.GetChartCellLabel();
    const ChartHypothesisCollection *hypoColl = cellLabel.GetStack();
    CHECK(hypoColl);
    const HypoList &hypoList = hypoColl->GetSortedHypotheses();

    // there have to be hypothesis with the desired non-terminal
    // (otherwise the rule would not be considered)
    CHECK(!hypoList.empty());

    // create a list of hypotheses that match the non-terminal
    HypothesisDimension dimension(0, hypoList);
    // add them to the vector for such lists
    m_hypothesisDimensions.push_back(dimension);
  }
}

bool RuleCubeItem::operator<(const RuleCubeItem &compare) const
{
  if (m_translationDimension == compare.m_translationDimension) {
    return m_hypothesisDimensions < compare.m_hypothesisDimensions;
  }
  return m_translationDimension < compare.m_translationDimension;
}

}