2010-07-18 03:23:09 +04:00
|
|
|
// $Id$
|
2010-04-12 14:15:49 +04:00
|
|
|
// vim:tabstop=2
|
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2010 Hieu Hoang
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2010-04-12 14:15:49 +04:00
|
|
|
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.
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2010-04-12 14:15:49 +04:00
|
|
|
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.
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2010-04-12 14:15:49 +04:00
|
|
|
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
|
|
|
|
***********************************************************************/
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include "ChartCell.h"
|
|
|
|
#include "ChartCellCollection.h"
|
2011-03-11 19:28:36 +03:00
|
|
|
#include "RuleCubeQueue.h"
|
|
|
|
#include "RuleCube.h"
|
2011-03-11 16:08:43 +03:00
|
|
|
#include "WordsRange.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "StaticData.h"
|
2012-09-25 20:34:43 +04:00
|
|
|
#include "ChartTranslationOptions.h"
|
2011-03-11 16:08:43 +03:00
|
|
|
#include "ChartTranslationOptionList.h"
|
2011-06-29 17:38:11 +04:00
|
|
|
#include "ChartManager.h"
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace Moses
|
|
|
|
{
|
2011-02-24 15:36:50 +03:00
|
|
|
extern bool g_debug;
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2012-10-02 17:30:55 +04:00
|
|
|
ChartCellBase::ChartCellBase(size_t startPos, size_t endPos) :
|
|
|
|
m_coverage(startPos, endPos),
|
|
|
|
m_targetLabelSet(m_coverage) {}
|
|
|
|
|
2012-10-02 19:34:25 +04:00
|
|
|
ChartCellBase::~ChartCellBase() {}
|
|
|
|
|
2012-07-02 23:01:07 +04:00
|
|
|
/** Constructor
|
|
|
|
* \param startPos endPos range of this cell
|
|
|
|
* \param manager pointer back to the manager
|
|
|
|
*/
|
2012-10-02 17:30:55 +04:00
|
|
|
ChartCell::ChartCell(size_t startPos, size_t endPos, ChartManager &manager) :
|
|
|
|
ChartCellBase(startPos, endPos), m_manager(manager) {
|
2011-02-24 15:36:50 +03:00
|
|
|
const StaticData &staticData = StaticData::Instance();
|
|
|
|
m_nBestIsEnabled = staticData.IsNBestEnabled();
|
2011-06-29 17:38:11 +04:00
|
|
|
}
|
|
|
|
|
2012-10-02 19:34:25 +04:00
|
|
|
ChartCell::~ChartCell() {}
|
|
|
|
|
2012-07-03 21:11:53 +04:00
|
|
|
/** Add the given hypothesis to the cell.
|
|
|
|
* Returns true if added, false if not. Maybe it already exists in the collection or score falls below threshold etc.
|
|
|
|
* This function just calls the correspondind AddHypothesis() in ChartHypothesisCollection
|
|
|
|
* \param hypo Hypothesis to be added
|
|
|
|
*/
|
2011-03-11 16:08:43 +03:00
|
|
|
bool ChartCell::AddHypothesis(ChartHypothesis *hypo)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 15:36:50 +03:00
|
|
|
const Word &targetLHS = hypo->GetTargetLHS();
|
|
|
|
return m_hypoColl[targetLHS].AddHypothesis(hypo, m_manager);
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 23:01:07 +04:00
|
|
|
/** Prune each collection in this cell to a particular size */
|
2010-04-08 21:16:10 +04:00
|
|
|
void ChartCell::PruneToSize()
|
|
|
|
{
|
2012-01-23 18:19:19 +04:00
|
|
|
MapType::iterator iter;
|
2011-02-24 15:36:50 +03:00
|
|
|
for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
|
2011-03-11 16:08:43 +03:00
|
|
|
ChartHypothesisCollection &coll = iter->second;
|
2011-02-24 15:36:50 +03:00
|
|
|
coll.PruneToSize(m_manager);
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
/** Decoding at span level: fill chart cell with hypotheses
|
|
|
|
* (implementation of cube pruning)
|
|
|
|
* \param transOptList list of applicable rules to create hypotheses for the cell
|
|
|
|
* \param allChartCells entire chart - needed to look up underlying hypotheses
|
|
|
|
*/
|
2010-09-23 21:39:32 +04:00
|
|
|
void ChartCell::ProcessSentence(const ChartTranslationOptionList &transOptList
|
2011-02-24 15:36:50 +03:00
|
|
|
, const ChartCellCollection &allChartCells)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 15:36:50 +03:00
|
|
|
const StaticData &staticData = StaticData::Instance();
|
|
|
|
|
2011-03-09 20:55:27 +03:00
|
|
|
// priority queue for applicable rules with selected hypotheses
|
2011-06-27 19:13:15 +04:00
|
|
|
RuleCubeQueue queue(m_manager);
|
2011-02-24 15:36:50 +03:00
|
|
|
|
|
|
|
// add all trans opt into queue. using only 1st child node.
|
2012-02-07 03:54:01 +04:00
|
|
|
for (size_t i = 0; i < transOptList.GetSize(); ++i) {
|
2012-09-25 20:34:43 +04:00
|
|
|
const ChartTranslationOptions &transOpt = transOptList.Get(i);
|
2011-06-27 19:13:15 +04:00
|
|
|
RuleCube *ruleCube = new RuleCube(transOpt, allChartCells, m_manager);
|
2011-03-11 19:28:36 +03:00
|
|
|
queue.Add(ruleCube);
|
2011-02-24 15:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// pluck things out of queue and add to hypo collection
|
|
|
|
const size_t popLimit = staticData.GetCubePruningPopLimit();
|
2011-03-11 19:28:36 +03:00
|
|
|
for (size_t numPops = 0; numPops < popLimit && !queue.IsEmpty(); ++numPops)
|
2011-03-09 20:55:27 +03:00
|
|
|
{
|
2011-06-27 19:13:15 +04:00
|
|
|
ChartHypothesis *hypo = queue.Pop();
|
2011-02-24 15:36:50 +03:00
|
|
|
AddHypothesis(hypo);
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 23:01:07 +04:00
|
|
|
//! call SortHypotheses() in each hypo collection in this cell
|
2010-04-08 21:16:10 +04:00
|
|
|
void ChartCell::SortHypotheses()
|
|
|
|
{
|
2011-11-18 16:07:41 +04:00
|
|
|
CHECK(m_targetLabelSet.Empty());
|
2012-01-23 18:19:19 +04:00
|
|
|
MapType::iterator iter;
|
2011-02-24 15:36:50 +03:00
|
|
|
for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
|
2011-03-11 16:08:43 +03:00
|
|
|
ChartHypothesisCollection &coll = iter->second;
|
2011-02-24 15:36:50 +03:00
|
|
|
coll.SortHypotheses();
|
2012-09-27 20:33:31 +04:00
|
|
|
m_targetLabelSet.AddConstituent(iter->first, &coll.GetSortedHypotheses());
|
2011-02-24 15:36:50 +03:00
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2012-07-03 21:11:53 +04:00
|
|
|
/** Return the highest scoring hypothesis out of all the hypo collection in this cell */
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesis *ChartCell::GetBestHypothesis() const
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesis *ret = NULL;
|
2011-02-24 15:36:50 +03:00
|
|
|
float bestScore = -std::numeric_limits<float>::infinity();
|
|
|
|
|
2012-01-23 18:19:19 +04:00
|
|
|
MapType::const_iterator iter;
|
2011-02-24 15:36:50 +03:00
|
|
|
for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
|
|
|
|
const HypoList &sortedList = iter->second.GetSortedHypotheses();
|
2011-11-18 16:07:41 +04:00
|
|
|
CHECK(sortedList.size() > 0);
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesis *hypo = sortedList[0];
|
2011-02-24 15:36:50 +03:00
|
|
|
if (hypo->GetTotalScore() > bestScore) {
|
|
|
|
bestScore = hypo->GetTotalScore();
|
|
|
|
ret = hypo;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 23:01:07 +04:00
|
|
|
//! call CleanupArcList() in each hypo collection in this cell
|
2010-04-08 21:16:10 +04:00
|
|
|
void ChartCell::CleanupArcList()
|
|
|
|
{
|
2011-02-24 15:36:50 +03:00
|
|
|
// only necessary if n-best calculations are enabled
|
|
|
|
if (!m_nBestIsEnabled) return;
|
|
|
|
|
2012-01-23 18:19:19 +04:00
|
|
|
MapType::iterator iter;
|
2011-02-24 15:36:50 +03:00
|
|
|
for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
|
2011-03-11 16:08:43 +03:00
|
|
|
ChartHypothesisCollection &coll = iter->second;
|
2011-02-24 15:36:50 +03:00
|
|
|
coll.CleanupArcList();
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 23:01:07 +04:00
|
|
|
//! debug info - size of each hypo collection in this cell
|
2010-04-08 21:16:10 +04:00
|
|
|
void ChartCell::OutputSizes(std::ostream &out) const
|
|
|
|
{
|
2012-01-23 18:19:19 +04:00
|
|
|
MapType::const_iterator iter;
|
2011-02-24 15:36:50 +03:00
|
|
|
for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
|
2011-03-11 19:28:36 +03:00
|
|
|
const Word &targetLHS = iter->first;
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesisCollection &coll = iter->second;
|
2011-02-24 15:36:50 +03:00
|
|
|
|
|
|
|
out << targetLHS << "=" << coll.GetSize() << " ";
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 23:01:07 +04:00
|
|
|
//! debug info - total number of hypos in all hypo collection in this cell
|
2010-04-08 21:16:10 +04:00
|
|
|
size_t ChartCell::GetSize() const
|
|
|
|
{
|
2011-02-24 15:36:50 +03:00
|
|
|
size_t ret = 0;
|
2012-01-23 18:19:19 +04:00
|
|
|
MapType::const_iterator iter;
|
2011-02-24 15:36:50 +03:00
|
|
|
for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesisCollection &coll = iter->second;
|
2011-02-24 15:36:50 +03:00
|
|
|
|
|
|
|
ret += coll.GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2012-11-13 21:43:52 +04:00
|
|
|
const HypoList *ChartCell::GetAllSortedHypotheses() const
|
|
|
|
{
|
|
|
|
HypoList *ret = new HypoList();
|
|
|
|
|
|
|
|
MapType::const_iterator iter;
|
|
|
|
for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
|
|
|
|
const ChartHypothesisCollection &coll = iter->second;
|
|
|
|
const HypoList &list = coll.GetSortedHypotheses();
|
|
|
|
std::copy(list.begin(), list.end(), std::inserter(*ret, ret->end()));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-07-02 23:01:07 +04:00
|
|
|
//! call GetSearchGraph() for each hypo collection
|
2011-10-06 14:31:09 +04:00
|
|
|
void ChartCell::GetSearchGraph(long translationId, std::ostream &outputSearchGraphStream, const std::map<unsigned, bool> &reachable) const
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2012-01-23 18:19:19 +04:00
|
|
|
MapType::const_iterator iterOutside;
|
2011-02-24 15:36:50 +03:00
|
|
|
for (iterOutside = m_hypoColl.begin(); iterOutside != m_hypoColl.end(); ++iterOutside) {
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesisCollection &coll = iterOutside->second;
|
2011-06-27 04:38:43 +04:00
|
|
|
coll.GetSearchGraph(translationId, outputSearchGraphStream, reachable);
|
2011-02-24 15:36:50 +03:00
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream &out, const ChartCell &cell)
|
|
|
|
{
|
2012-01-23 18:19:19 +04:00
|
|
|
ChartCell::MapType::const_iterator iterOutside;
|
2011-02-24 15:36:50 +03:00
|
|
|
for (iterOutside = cell.m_hypoColl.begin(); iterOutside != cell.m_hypoColl.end(); ++iterOutside) {
|
2011-03-11 19:28:36 +03:00
|
|
|
const Word &targetLHS = iterOutside->first;
|
2011-02-24 15:36:50 +03:00
|
|
|
cerr << targetLHS << ":" << endl;
|
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesisCollection &coll = iterOutside->second;
|
2011-02-24 15:36:50 +03:00
|
|
|
cerr << coll;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
ChartCell::HCType::const_iterator iter;
|
|
|
|
for (iter = cell.m_hypos.begin(); iter != cell.m_hypos.end(); ++iter)
|
|
|
|
{
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesis &hypo = **iter;
|
2011-02-24 15:36:50 +03:00
|
|
|
out << hypo << endl;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
return out;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|