File size: 3,643 Bytes
158b61b |
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 136 137 138 139 140 141 142 143 144 145 146 147 148 |
/***********************************************************************
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
***********************************************************************/
#pragma once
#include "ChartCellLabel.h"
#include "NonTerminal.h"
#include "moses/FactorCollection.h"
#include <boost/functional/hash.hpp>
#include <boost/unordered_map.hpp>
#include <boost/version.hpp>
namespace Moses
{
class ChartHypothesisCollection;
/** @todo I have no idea what's in here
*/
class ChartCellLabelSet
{
private:
typedef std::vector<ChartCellLabel*> MapType;
public:
typedef MapType::const_iterator const_iterator;
typedef MapType::iterator iterator;
ChartCellLabelSet(const Range &coverage)
: m_coverage(coverage)
, m_map(FactorCollection::Instance().GetNumNonTerminals(), NULL)
, m_size(0) { }
~ChartCellLabelSet() {
RemoveAllInColl(m_map);
}
// TODO: skip empty elements when iterating, or deprecate this
const_iterator begin() const {
return m_map.begin();
}
const_iterator end() const {
return m_map.end();
}
iterator mutable_begin() {
return m_map.begin();
}
iterator mutable_end() {
return m_map.end();
}
void AddWord(const Word &w) {
size_t idx = w[0]->GetId();
if (! ChartCellExists(idx)) {
m_size++;
m_map[idx] = new ChartCellLabel(m_coverage, w);
}
}
// Stack is a HypoList or whatever the search algorithm uses.
void AddConstituent(const Word &w, const HypoList *stack) {
size_t idx = w[0]->GetId();
if (ChartCellExists(idx)) {
ChartCellLabel::Stack & s = m_map[idx]->MutableStack();
s.cube = stack;
} else {
ChartCellLabel::Stack s;
s.cube = stack;
m_size++;
m_map[idx] = new ChartCellLabel(m_coverage, w, s);
}
}
// grow vector if necessary
bool ChartCellExists(size_t idx) {
try {
if (m_map.at(idx) != NULL) {
return true;
}
} catch (const std::out_of_range& oor) {
m_map.resize(FactorCollection::Instance().GetNumNonTerminals(), NULL);
}
return false;
}
bool Empty() const {
return m_size == 0;
}
size_t GetSize() const {
return m_size;
}
const ChartCellLabel *Find(const Word &w) const {
size_t idx = w[0]->GetId();
try {
return m_map.at(idx);
} catch (const std::out_of_range& oor) {
return NULL;
}
}
const ChartCellLabel *Find(size_t idx) const {
try {
return m_map.at(idx);
} catch (const std::out_of_range& oor) {
return NULL;
}
}
ChartCellLabel::Stack &FindOrInsert(const Word &w) {
size_t idx = w[0]->GetId();
if (! ChartCellExists(idx)) {
m_size++;
m_map[idx] = new ChartCellLabel(m_coverage, w);
}
return m_map[idx]->MutableStack();
}
private:
const Range &m_coverage;
MapType m_map;
size_t m_size;
};
}
|