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
|
|
|
#pragma once
|
2010-04-12 14:15:49 +04:00
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
#include <set>
|
|
|
|
#include "ChartHypothesis.h"
|
2011-03-11 19:28:36 +03:00
|
|
|
#include "RuleCube.h"
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
namespace Moses
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2012-06-27 03:45:02 +04:00
|
|
|
//! functor to compare (chart) hypotheses by (descending) score
|
2010-04-08 21:16:10 +04:00
|
|
|
class ChartHypothesisScoreOrderer
|
2011-02-24 15:36:50 +03:00
|
|
|
{
|
|
|
|
public:
|
2011-03-11 16:08:43 +03:00
|
|
|
bool operator()(const ChartHypothesis* hypoA, const ChartHypothesis* hypoB) const {
|
2011-02-24 15:36:50 +03:00
|
|
|
return hypoA->GetTotalScore() > hypoB->GetTotalScore();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-27 03:45:02 +04:00
|
|
|
/** functor to compare (chart) hypotheses by feature function states.
|
|
|
|
* If 2 hypos are equal, according to this functor, then they can be recombined.
|
|
|
|
*/
|
2011-03-11 16:08:43 +03:00
|
|
|
class ChartHypothesisRecombinationOrderer
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
|
|
|
public:
|
2011-03-11 16:08:43 +03:00
|
|
|
bool operator()(const ChartHypothesis* hypoA, const ChartHypothesis* hypoB) const {
|
2011-02-24 15:36:50 +03:00
|
|
|
// assert in same cell
|
2014-03-17 18:18:11 +04:00
|
|
|
assert(hypoA->GetCurrSourceRange() == hypoB->GetCurrSourceRange());
|
2011-02-24 15:36:50 +03:00
|
|
|
|
|
|
|
// shouldn't be mixing hypos with different lhs
|
2013-11-19 22:52:15 +04:00
|
|
|
assert(hypoA->GetTargetLHS() == hypoB->GetTargetLHS());
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2011-06-16 01:31:27 +04:00
|
|
|
int ret = hypoA->RecombineCompare(*hypoB);
|
2011-02-24 15:36:50 +03:00
|
|
|
if (ret != 0)
|
|
|
|
return (ret < 0);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
};
|
|
|
|
|
2012-06-27 03:45:02 +04:00
|
|
|
/** Contains a set of unique hypos that have the same HS non-term.
|
|
|
|
* ie. 1 of these for each target LHS in each cell
|
|
|
|
*/
|
2011-03-11 16:08:43 +03:00
|
|
|
class ChartHypothesisCollection
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-03-11 16:08:43 +03:00
|
|
|
friend std::ostream& operator<<(std::ostream&, const ChartHypothesisCollection&);
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
protected:
|
2011-03-11 16:08:43 +03:00
|
|
|
typedef std::set<ChartHypothesis*, ChartHypothesisRecombinationOrderer> HCType;
|
2011-02-24 15:36:50 +03:00
|
|
|
HCType m_hypos;
|
|
|
|
HypoList m_hyposOrdered;
|
|
|
|
|
|
|
|
float m_bestScore; /**< score of the best hypothesis in collection */
|
|
|
|
float m_beamWidth; /**< minimum score due to threashold pruning */
|
|
|
|
size_t m_maxHypoStackSize; /**< maximum number of hypothesis allowed in this stack */
|
|
|
|
bool m_nBestIsEnabled; /**< flag to determine whether to keep track of old arcs */
|
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
std::pair<HCType::iterator, bool> Add(ChartHypothesis *hypo, ChartManager &manager);
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
public:
|
2011-02-24 15:36:50 +03:00
|
|
|
typedef HCType::iterator iterator;
|
|
|
|
typedef HCType::const_iterator const_iterator;
|
|
|
|
//! iterators
|
|
|
|
const_iterator begin() const {
|
|
|
|
return m_hypos.begin();
|
|
|
|
}
|
|
|
|
const_iterator end() const {
|
|
|
|
return m_hypos.end();
|
|
|
|
}
|
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
ChartHypothesisCollection();
|
|
|
|
~ChartHypothesisCollection();
|
|
|
|
bool AddHypothesis(ChartHypothesis *hypo, ChartManager &manager);
|
2011-02-24 15:36:50 +03:00
|
|
|
|
|
|
|
void Detach(const HCType::iterator &iter);
|
|
|
|
void Remove(const HCType::iterator &iter);
|
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
void PruneToSize(ChartManager &manager);
|
2011-02-24 15:36:50 +03:00
|
|
|
|
|
|
|
size_t GetSize() const {
|
|
|
|
return m_hypos.size();
|
|
|
|
}
|
|
|
|
size_t GetHypo() const {
|
|
|
|
return m_hypos.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SortHypotheses();
|
|
|
|
void CleanupArcList();
|
|
|
|
|
2012-07-02 23:01:07 +04:00
|
|
|
//! return vector of hypothesis that has been sorted by score
|
2011-02-24 15:36:50 +03:00
|
|
|
const HypoList &GetSortedHypotheses() const {
|
|
|
|
return m_hyposOrdered;
|
|
|
|
}
|
|
|
|
|
2012-07-02 23:01:07 +04:00
|
|
|
//! return the best total score of all hypos in this collection
|
2013-05-29 21:16:15 +04:00
|
|
|
float GetBestScore() const {
|
|
|
|
return m_bestScore;
|
|
|
|
}
|
2011-07-19 16:57:02 +04:00
|
|
|
|
2011-10-06 14:31:09 +04:00
|
|
|
void GetSearchGraph(long translationId, std::ostream &outputSearchGraphStream, const std::map<unsigned,bool> &reachable) const;
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|