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 "RuleCubeQueue.h"
00026 #include "RuleCube.h"
00027 #include "WordsRange.h"
00028 #include "Util.h"
00029 #include "StaticData.h"
00030 #include "ChartTranslationOptions.h"
00031 #include "ChartTranslationOptionList.h"
00032 #include "ChartManager.h"
00033
00034 using namespace std;
00035
00036 namespace Moses
00037 {
00038 extern bool g_debug;
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 const StaticData &staticData = StaticData::Instance();
00053 m_nBestIsEnabled = staticData.IsNBestEnabled();
00054 }
00055
00056 ChartCell::~ChartCell() {}
00057
00063 bool ChartCell::AddHypothesis(ChartHypothesis *hypo)
00064 {
00065 const Word &targetLHS = hypo->GetTargetLHS();
00066 return m_hypoColl[targetLHS].AddHypothesis(hypo, m_manager);
00067 }
00068
00070 void ChartCell::PruneToSize()
00071 {
00072 MapType::iterator iter;
00073 for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
00074 ChartHypothesisCollection &coll = iter->second;
00075 coll.PruneToSize(m_manager);
00076 }
00077 }
00078
00084 void ChartCell::ProcessSentence(const ChartTranslationOptionList &transOptList
00085 , const ChartCellCollection &allChartCells)
00086 {
00087 const StaticData &staticData = StaticData::Instance();
00088
00089
00090 RuleCubeQueue queue(m_manager);
00091
00092
00093 for (size_t i = 0; i < transOptList.GetSize(); ++i) {
00094 const ChartTranslationOptions &transOpt = transOptList.Get(i);
00095 RuleCube *ruleCube = new RuleCube(transOpt, allChartCells, m_manager);
00096 queue.Add(ruleCube);
00097 }
00098
00099
00100 const size_t popLimit = staticData.GetCubePruningPopLimit();
00101 for (size_t numPops = 0; numPops < popLimit && !queue.IsEmpty(); ++numPops)
00102 {
00103 ChartHypothesis *hypo = queue.Pop();
00104 AddHypothesis(hypo);
00105 }
00106 }
00107
00109 void ChartCell::SortHypotheses()
00110 {
00111 CHECK(m_targetLabelSet.Empty());
00112 MapType::iterator iter;
00113 for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
00114 ChartHypothesisCollection &coll = iter->second;
00115 coll.SortHypotheses();
00116 m_targetLabelSet.AddConstituent(iter->first, &coll.GetSortedHypotheses());
00117 }
00118 }
00119
00121 const ChartHypothesis *ChartCell::GetBestHypothesis() const
00122 {
00123 const ChartHypothesis *ret = NULL;
00124 float bestScore = -std::numeric_limits<float>::infinity();
00125
00126 MapType::const_iterator iter;
00127 for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
00128 const HypoList &sortedList = iter->second.GetSortedHypotheses();
00129 CHECK(sortedList.size() > 0);
00130
00131 const ChartHypothesis *hypo = sortedList[0];
00132 if (hypo->GetTotalScore() > bestScore) {
00133 bestScore = hypo->GetTotalScore();
00134 ret = hypo;
00135 };
00136 }
00137
00138 return ret;
00139 }
00140
00142 void ChartCell::CleanupArcList()
00143 {
00144
00145 if (!m_nBestIsEnabled) return;
00146
00147 MapType::iterator iter;
00148 for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
00149 ChartHypothesisCollection &coll = iter->second;
00150 coll.CleanupArcList();
00151 }
00152 }
00153
00155 void ChartCell::OutputSizes(std::ostream &out) const
00156 {
00157 MapType::const_iterator iter;
00158 for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
00159 const Word &targetLHS = iter->first;
00160 const ChartHypothesisCollection &coll = iter->second;
00161
00162 out << targetLHS << "=" << coll.GetSize() << " ";
00163 }
00164 }
00165
00167 size_t ChartCell::GetSize() const
00168 {
00169 size_t ret = 0;
00170 MapType::const_iterator iter;
00171 for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
00172 const ChartHypothesisCollection &coll = iter->second;
00173
00174 ret += coll.GetSize();
00175 }
00176
00177 return ret;
00178 }
00179
00180 const HypoList *ChartCell::GetAllSortedHypotheses() const
00181 {
00182 HypoList *ret = new HypoList();
00183
00184 MapType::const_iterator iter;
00185 for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
00186 const ChartHypothesisCollection &coll = iter->second;
00187 const HypoList &list = coll.GetSortedHypotheses();
00188 std::copy(list.begin(), list.end(), std::inserter(*ret, ret->end()));
00189 }
00190 return ret;
00191 }
00192
00194 void ChartCell::GetSearchGraph(long translationId, std::ostream &outputSearchGraphStream, const std::map<unsigned, bool> &reachable) const
00195 {
00196 MapType::const_iterator iterOutside;
00197 for (iterOutside = m_hypoColl.begin(); iterOutside != m_hypoColl.end(); ++iterOutside) {
00198 const ChartHypothesisCollection &coll = iterOutside->second;
00199 coll.GetSearchGraph(translationId, outputSearchGraphStream, reachable);
00200 }
00201 }
00202
00203 std::ostream& operator<<(std::ostream &out, const ChartCell &cell)
00204 {
00205 ChartCell::MapType::const_iterator iterOutside;
00206 for (iterOutside = cell.m_hypoColl.begin(); iterOutside != cell.m_hypoColl.end(); ++iterOutside) {
00207 const Word &targetLHS = iterOutside->first;
00208 cerr << targetLHS << ":" << endl;
00209
00210 const ChartHypothesisCollection &coll = iterOutside->second;
00211 cerr << coll;
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 return out;
00224 }
00225
00226 }