This commit is contained in:
Hieu Hoang 2016-08-23 15:36:28 +01:00
parent e1fd84b864
commit 8c99db123f
2 changed files with 33 additions and 17 deletions

View File

@ -20,7 +20,7 @@ namespace Moses2
{
namespace SCFG
{
NBestCandidate::NBestCandidate(const SCFG::Manager &mgr, const ArcList &varcList, size_t vind)
NBest::NBest(const SCFG::Manager &mgr, const ArcList &varcList, size_t vind)
:arcList(&varcList)
,ind(vind)
{
@ -44,14 +44,14 @@ NBestCandidate::NBestCandidate(const SCFG::Manager &mgr, const ArcList &varcList
}
}
const SCFG::Hypothesis &NBestCandidate::GetHypo() const
const SCFG::Hypothesis &NBest::GetHypo() const
{
const HypothesisBase *hypoBase = (*arcList)[ind];
const SCFG::Hypothesis &hypo = *static_cast<const SCFG::Hypothesis*>(hypoBase);
return hypo;
}
void NBestCandidate::OutputToStream(
void NBest::OutputToStream(
const SCFG::Manager &mgr,
std::stringstream &strm,
const NBestColl &nbestColl) const
@ -71,8 +71,8 @@ void NBestCandidate::OutputToStream(
const Child &child = children[nonTermInd];
UTIL_THROW_IF2(child.first == NULL, "ArcList == NULL");
const NBestCandidates &nbests = nbestColl.GetNBestCandidates(*child.first);
const NBestCandidate &nbest = nbests[child.second];
const NBests &nbests = nbestColl.GetNBests(*child.first);
const NBest &nbest = nbests[child.second];
nbest.OutputToStream(mgr, strm, nbestColl);
}
else {
@ -87,18 +87,32 @@ void NBestCandidate::OutputToStream(
/////////////////////////////////////////////////////////////
void NBestColl::Add(const SCFG::Manager &mgr, const ArcList &arcList)
{
NBestCandidate candidate(mgr, arcList, 0);
m_candidates[&arcList].push_back(candidate);
NBest best(mgr, arcList, 0);
GetOrCreateNBests(arcList).push_back(best);
}
const NBestCandidates &NBestColl::GetNBestCandidates(const ArcList &arcList) const
const NBests &NBestColl::GetNBests(const ArcList &arcList) const
{
Coll::const_iterator iter = m_candidates.find(&arcList);
UTIL_THROW_IF2(iter == m_candidates.end(), "Can't find arclist");
const NBestCandidates &ret = iter->second;
const NBests &ret = *iter->second;
return ret;
}
NBests &NBestColl::GetOrCreateNBests(const ArcList &arcList)
{
NBests *ret;
Coll::const_iterator iter = m_candidates.find(&arcList);
if(iter == m_candidates.end()) {
ret = new NBests();
m_candidates[&arcList] = ret;
}
else {
ret = iter->second;
}
return *ret;
}
/////////////////////////////////////////////////////////////
KBestExtractor::KBestExtractor(const SCFG::Manager &mgr)
:m_mgr(mgr)
@ -146,9 +160,9 @@ void KBestExtractor::OutputToStream(std::stringstream &strm)
const ArcLists &arcLists = m_mgr.arcLists;
const ArcList &arcList = arcLists.GetArcList(hypo);
const NBestCandidates &nbestVec = m_nbestColl.GetNBestCandidates(arcList);
const NBests &nbestVec = m_nbestColl.GetNBests(arcList);
BOOST_FOREACH(const NBestCandidate &deriv, nbestVec) {
BOOST_FOREACH(const NBest &deriv, nbestVec) {
strm << m_mgr.GetTranslationId() << " ||| ";
//cerr << "1" << flush;
deriv.OutputToStream(m_mgr, strm, m_nbestColl);

View File

@ -21,17 +21,17 @@ class Manager;
class Hypothesis;
class NBestColl;
class NBestCandidate
class NBest
{
public:
const ArcList *arcList;
size_t ind;
typedef std::pair<const ArcList *, size_t> Child; // key to another NBestCandidate
typedef std::pair<const ArcList *, size_t> Child; // key to another NBest
typedef std::vector<Child> Children;
Children children;
NBestCandidate(const SCFG::Manager &mgr, const ArcList &varcList, size_t vind);
NBest(const SCFG::Manager &mgr, const ArcList &varcList, size_t vind);
const Scores &GetScores() const
{ return *m_scores; }
@ -48,18 +48,20 @@ protected:
};
/////////////////////////////////////////////////////////////
typedef std::vector<NBestCandidate> NBestCandidates;
typedef std::vector<NBest> NBests;
/////////////////////////////////////////////////////////////
class NBestColl
{
public:
void Add(const SCFG::Manager &mgr, const ArcList &arcList);
const NBestCandidates &GetNBestCandidates(const ArcList &arcList) const;
const NBests &GetNBests(const ArcList &arcList) const;
protected:
typedef boost::unordered_map<const ArcList*, NBestCandidates> Coll;
typedef boost::unordered_map<const ArcList*, NBests*> Coll;
Coll m_candidates;
NBests &GetOrCreateNBests(const ArcList &arcList);
};
/////////////////////////////////////////////////////////////