mosesdecoder/moses/InputPath.cpp

119 lines
3.8 KiB
C++
Raw Normal View History

#include "InputPath.h"
2013-07-09 01:47:02 +04:00
#include "ScoreComponentCollection.h"
2013-07-18 23:39:15 +04:00
#include "TargetPhraseCollection.h"
2013-07-19 02:09:33 +04:00
#include "StaticData.h"
#include "TypeDef.h"
2013-07-19 18:38:13 +04:00
#include "AlignmentInfo.h"
using namespace std;
namespace Moses
{
2013-07-09 01:47:02 +04:00
InputPath::InputPath(const Phrase &phrase, const WordsRange &range, const InputPath *prevNode
,const ScoreComponentCollection *inputScore)
2013-07-09 01:47:02 +04:00
:m_prevNode(prevNode)
,m_phrase(phrase)
,m_range(range)
,m_inputScore(inputScore)
2013-07-09 01:47:02 +04:00
{
2013-07-19 16:58:39 +04:00
FactorType factorType = StaticData::Instance().GetPlaceholderFactor();
if (factorType != NOT_FOUND) {
for (size_t pos = 0; pos < m_phrase.GetSize(); ++pos) {
if (m_phrase.GetFactor(pos, factorType)) {
m_placeholders.push_back(pos);
}
}
}
2013-07-09 01:47:02 +04:00
}
InputPath::~InputPath()
{
delete m_inputScore;
2013-07-18 23:39:15 +04:00
// detach target phrase before delete objects from m_copiedSet
// the phrase dictionary owns the target phrases so they should be doing the deleting
std::vector<TargetPhraseCollection>::iterator iter;
for (iter = m_copiedSet.begin(); iter != m_copiedSet.end(); ++iter) {
2013-07-19 16:58:39 +04:00
TargetPhraseCollection &coll = *iter;
coll.Detach();
2013-07-18 23:39:15 +04:00
}
2013-07-09 01:47:02 +04:00
}
2013-07-07 05:14:51 +04:00
const TargetPhraseCollection *InputPath::GetTargetPhrases(const PhraseDictionary &phraseDictionary) const
{
std::map<const PhraseDictionary*, std::pair<const TargetPhraseCollection*, const void*> >::const_iterator iter;
iter = m_targetPhrases.find(&phraseDictionary);
if (iter == m_targetPhrases.end()) {
2013-07-05 02:38:18 +04:00
return NULL;
}
return iter->second.first;
}
2013-07-07 05:14:51 +04:00
const void *InputPath::GetPtNode(const PhraseDictionary &phraseDictionary) const
{
std::map<const PhraseDictionary*, std::pair<const TargetPhraseCollection*, const void*> >::const_iterator iter;
iter = m_targetPhrases.find(&phraseDictionary);
if (iter == m_targetPhrases.end()) {
2013-07-05 02:38:18 +04:00
return NULL;
}
return iter->second.second;
}
2013-07-18 23:23:44 +04:00
void InputPath::SetTargetPhrases(const PhraseDictionary &phraseDictionary
2013-07-19 16:58:39 +04:00
, const TargetPhraseCollection *targetPhrases
, const void *ptNode)
{
2013-07-19 18:38:13 +04:00
FactorType placeholderFactor = StaticData::Instance().GetPlaceholderFactor();
if (targetPhrases == NULL || placeholderFactor == NOT_FOUND || m_placeholders.size() == 0) {
// use all of the target phrase given
std::pair<const TargetPhraseCollection*, const void*> value(targetPhrases, ptNode);
m_targetPhrases[&phraseDictionary] = value;
}
else {
// filter out target phrases with alignments that are not 1-to-1 with placeholder
m_copiedSet.push_back(TargetPhraseCollection());
TargetPhraseCollection &newTargetPhrases = m_copiedSet.back();
std::pair<const TargetPhraseCollection*, const void*> value(&newTargetPhrases, ptNode);
m_targetPhrases[&phraseDictionary] = value;
TargetPhraseCollection::const_iterator iter;
for (iter = targetPhrases->begin(); iter != targetPhrases->end(); ++iter) {
TargetPhrase *targetPhrase = *iter;
const AlignmentInfo &alignments = targetPhrase->GetAlignTerm();
bool ok = IsCompatibleWithPlaceholders(alignments);
if (ok) {
newTargetPhrases.Add(targetPhrase);
}
}
}
}
bool InputPath::IsCompatibleWithPlaceholders(const AlignmentInfo &alignments) const
{
for (size_t i = 0; i < m_placeholders.size(); ++i) {
size_t sourcePos = m_placeholders[i];
set<size_t> targetPos = alignments.GetAlignmentsForSource(sourcePos);
if (targetPos.size() != 1) {
return false;
}
}
return true;
2013-07-18 23:23:44 +04:00
}
2013-07-07 05:14:51 +04:00
std::ostream& operator<<(std::ostream& out, const InputPath& obj)
{
2013-07-05 02:38:18 +04:00
out << &obj << " " << obj.GetWordsRange() << " " << obj.GetPrevNode() << " " << obj.GetPhrase();
2013-07-05 02:38:18 +04:00
out << "pt: ";
std::map<const PhraseDictionary*, std::pair<const TargetPhraseCollection*, const void*> >::const_iterator iter;
for (iter = obj.m_targetPhrases.begin(); iter != obj.m_targetPhrases.end(); ++iter) {
const PhraseDictionary *pt = iter->first;
out << pt << " ";
}
2013-07-05 02:38:18 +04:00
return out;
}
}