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

RuleCube.cpp « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 874a1f57d0026711a35eabef969a52f1f60c30e4 (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
// $Id$
// vim:tabstop=2
/***********************************************************************
 Moses - factored phrase-based language decoder
 Copyright (C) 2010 Hieu Hoang

 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 "ChartTranslationOptions.h"
#include "RuleCube.h"
#include "RuleCubeQueue.h"
#include "StaticData.h"
#include "Util.h"
#include "Range.h"
#include <boost/functional/hash.hpp>

using namespace std;

namespace Moses
{

// initialise the RuleCube by creating the top-left corner item
RuleCube::RuleCube(const ChartTranslationOptions &transOpt,
                   const ChartCellCollection &allChartCells,
                   ChartManager &manager)
  : m_transOpt(transOpt)
{
  RuleCubeItem *item = new RuleCubeItem(transOpt, allChartCells);
  m_covered.insert(item);
  if (StaticData::Instance().options()->cube.lazy_scoring) {
    item->EstimateScore();
  } else {
    item->CreateHypothesis(transOpt, manager);
  }
  m_queue.push(item);
}

RuleCube::~RuleCube()
{
  RemoveAllInColl(m_covered);
}

RuleCubeItem *RuleCube::Pop(ChartManager &manager)
{
  RuleCubeItem *item = m_queue.top();
  m_queue.pop();
  CreateNeighbors(*item, manager);
  return item;
}

// create new RuleCube for neighboring principle rules
void RuleCube::CreateNeighbors(const RuleCubeItem &item, ChartManager &manager)
{
  // create neighbor along translation dimension
  const TranslationDimension &translationDimension =
    item.GetTranslationDimension();
  if (translationDimension.HasMoreTranslations()) {
    CreateNeighbor(item, -1, manager);
  }

  // create neighbors along all hypothesis dimensions
  for (size_t i = 0; i < item.GetHypothesisDimensions().size(); ++i) {
    const HypothesisDimension &dimension = item.GetHypothesisDimensions()[i];
    if (dimension.HasMoreHypo()) {
      CreateNeighbor(item, i, manager);
    }
  }
}

void RuleCube::CreateNeighbor(const RuleCubeItem &item, int dimensionIndex,
                              ChartManager &manager)
{
  RuleCubeItem *newItem = new RuleCubeItem(item, dimensionIndex);
  std::pair<ItemSet::iterator, bool> result = m_covered.insert(newItem);
  if (!result.second) {
    delete newItem;  // already seen it
  } else {
    if (StaticData::Instance().options()->cube.lazy_scoring) {
      newItem->EstimateScore();
    } else {
      newItem->CreateHypothesis(m_transOpt, manager);
    }
    m_queue.push(newItem);
  }
}

std::ostream& operator<<(std::ostream &out, const RuleCube &obj)
{
  out << obj.GetItemSetSize();
  return out;
}
}