00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <algorithm>
00023 #include "ChartCell.h"
00024 #include "ChartCellCollection.h"
00025 #include "HypergraphOutput.h"
00026 #include "RuleCubeQueue.h"
00027 #include "RuleCube.h"
00028 #include "Range.h"
00029 #include "Util.h"
00030 #include "ChartTranslationOptions.h"
00031 #include "ChartTranslationOptionList.h"
00032 #include "ChartManager.h"
00033 #include "util/exception.hh"
00034
00035 using namespace std;
00036
00037 namespace Moses
00038 {
00039
00040 ChartCellBase::ChartCellBase(size_t startPos, size_t endPos) :
00041 m_coverage(startPos, endPos),
00042 m_targetLabelSet(m_coverage) {}
00043
00044 ChartCellBase::~ChartCellBase() {}
00045
00050 ChartCell::ChartCell(size_t startPos, size_t endPos, ChartManager &manager) :
00051 ChartCellBase(startPos, endPos), m_manager(manager)
00052 {
00053 m_nBestIsEnabled = manager.options()->nbest.enabled;
00054 }
00055
00056 ChartCell::~ChartCell() {}
00057
00063 bool ChartCell::AddHypothesis(ChartHypothesis *hypo)
00064 {
00065 const Word &targetLHS = hypo->GetTargetLHS();
00066 MapType::iterator m = m_hypoColl.find(targetLHS);
00067 if (m == m_hypoColl.end()) {
00068 std::pair<Word, ChartHypothesisCollection>
00069 e(targetLHS, ChartHypothesisCollection(*m_manager.options()));
00070 m = m_hypoColl.insert(e).first;
00071 }
00072 return m->second.AddHypothesis(hypo, m_manager);
00073 }
00074
00076 void ChartCell::PruneToSize()
00077 {
00078 MapType::iterator iter;
00079 for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
00080 ChartHypothesisCollection &coll = iter->second;
00081 coll.PruneToSize(m_manager);
00082 }
00083 }
00084
00090 void ChartCell::Decode(const ChartTranslationOptionList &transOptList
00091 , const ChartCellCollection &allChartCells)
00092 {
00093
00094 RuleCubeQueue queue(m_manager);
00095
00096
00097 for (size_t i = 0; i < transOptList.GetSize(); ++i) {
00098 const ChartTranslationOptions &transOpt = transOptList.Get(i);
00099 RuleCube *ruleCube = new RuleCube(transOpt, allChartCells, m_manager);
00100 queue.Add(ruleCube);
00101 }
00102
00103
00104 const size_t popLimit = m_manager.options()->cube.pop_limit;
00105 for (size_t numPops = 0; numPops < popLimit && !queue.IsEmpty(); ++numPops) {
00106 ChartHypothesis *hypo = queue.Pop();
00107 AddHypothesis(hypo);
00108 }
00109 }
00110
00112 void ChartCell::SortHypotheses()
00113 {
00114 UTIL_THROW_IF2(!m_targetLabelSet.Empty(), "Already sorted");
00115
00116 MapType::iterator iter;
00117 for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
00118 ChartHypothesisCollection &coll = iter->second;
00119
00120 if (coll.GetSize()) {
00121 coll.SortHypotheses();
00122 m_targetLabelSet.AddConstituent(iter->first, &coll.GetSortedHypotheses());
00123 }
00124 }
00125 }
00126
00128 const ChartHypothesis *ChartCell::GetBestHypothesis() const
00129 {
00130 const ChartHypothesis *ret = NULL;
00131 float bestScore = -std::numeric_limits<float>::infinity();
00132
00133 MapType::const_iterator iter;
00134 for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
00135 const HypoList &sortedList = iter->second.GetSortedHypotheses();
00136 if (sortedList.size() > 0) {
00137 const ChartHypothesis *hypo = sortedList[0];
00138 if (hypo->GetFutureScore() > bestScore) {
00139 bestScore = hypo->GetFutureScore();
00140 ret = hypo;
00141 }
00142 }
00143 }
00144
00145 return ret;
00146 }
00147
00149 void ChartCell::CleanupArcList()
00150 {
00151
00152 if (!m_nBestIsEnabled) return;
00153
00154 MapType::iterator iter;
00155 for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
00156 ChartHypothesisCollection &coll = iter->second;
00157 coll.CleanupArcList();
00158 }
00159 }
00160
00162 void ChartCell::OutputSizes(std::ostream &out) const
00163 {
00164 MapType::const_iterator iter;
00165 for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
00166 const Word &targetLHS = iter->first;
00167 const ChartHypothesisCollection &coll = iter->second;
00168
00169 out << targetLHS << "=" << coll.GetSize() << " ";
00170 }
00171 }
00172
00174 size_t ChartCell::GetSize() const
00175 {
00176 size_t ret = 0;
00177 MapType::const_iterator iter;
00178 for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
00179 const ChartHypothesisCollection &coll = iter->second;
00180
00181 ret += coll.GetSize();
00182 }
00183
00184 return ret;
00185 }
00186
00187 const HypoList *ChartCell::GetAllSortedHypotheses() const
00188 {
00189 HypoList *ret = new HypoList();
00190
00191 MapType::const_iterator iter;
00192 for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
00193 const ChartHypothesisCollection &coll = iter->second;
00194 const HypoList &list = coll.GetSortedHypotheses();
00195 std::copy(list.begin(), list.end(), std::inserter(*ret, ret->end()));
00196 }
00197 return ret;
00198 }
00199
00201 void ChartCell::WriteSearchGraph(const ChartSearchGraphWriter& writer, const std::map<unsigned, bool> &reachable) const
00202 {
00203 MapType::const_iterator iterOutside;
00204 for (iterOutside = m_hypoColl.begin(); iterOutside != m_hypoColl.end(); ++iterOutside) {
00205 const ChartHypothesisCollection &coll = iterOutside->second;
00206 coll.WriteSearchGraph(writer, reachable);
00207 }
00208 }
00209
00210 std::ostream& operator<<(std::ostream &out, const ChartCell &cell)
00211 {
00212 ChartCell::MapType::const_iterator iterOutside;
00213 for (iterOutside = cell.m_hypoColl.begin(); iterOutside != cell.m_hypoColl.end(); ++iterOutside) {
00214 const Word &targetLHS = iterOutside->first;
00215 cerr << targetLHS << ":" << endl;
00216
00217 const ChartHypothesisCollection &coll = iterOutside->second;
00218 cerr << coll;
00219 }
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 return out;
00231 }
00232
00233 }