2013-07-07 01:42:52 +04:00
|
|
|
#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"
|
2013-07-30 18:28:20 +04:00
|
|
|
#include "util/check.hh"
|
2013-07-19 18:38:13 +04:00
|
|
|
|
|
|
|
using namespace std;
|
2013-07-09 18:48:36 +04:00
|
|
|
|
2013-07-02 02:27:13 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
2013-08-02 18:54:49 +04:00
|
|
|
InputPath::InputPath(const Phrase &phrase, const NonTerminalSet &sourceNonTerms, const WordsRange &range, const InputPath *prevNode
|
2013-09-08 17:57:31 +04:00
|
|
|
,const ScorePair *inputScore)
|
2013-07-09 01:47:02 +04:00
|
|
|
:m_prevNode(prevNode)
|
|
|
|
,m_phrase(phrase)
|
2013-08-02 18:54:49 +04:00
|
|
|
,m_sourceNonTerms(sourceNonTerms)
|
2013-07-09 01:47:02 +04:00
|
|
|
,m_range(range)
|
2013-07-09 18:48:36 +04:00
|
|
|
,m_inputScore(inputScore)
|
2013-07-09 01:47:02 +04:00
|
|
|
{
|
2013-09-10 17:36:21 +04:00
|
|
|
//cerr << "phrase=" << phrase << " m_inputScore=" << *m_inputScore << endl;
|
|
|
|
|
2013-07-19 21:23:38 +04:00
|
|
|
FactorType placeholderFactor = StaticData::Instance().GetPlaceholderFactor().first;
|
|
|
|
if (placeholderFactor != NOT_FOUND) {
|
2013-07-19 16:58:39 +04:00
|
|
|
for (size_t pos = 0; pos < m_phrase.GetSize(); ++pos) {
|
2013-07-19 21:23:38 +04:00
|
|
|
if (m_phrase.GetFactor(pos, placeholderFactor)) {
|
2013-07-19 16:58:39 +04:00
|
|
|
m_placeholders.push_back(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 01:47:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
InputPath::~InputPath()
|
|
|
|
{
|
|
|
|
delete m_inputScore;
|
|
|
|
}
|
|
|
|
|
2013-07-07 05:14:51 +04:00
|
|
|
const TargetPhraseCollection *InputPath::GetTargetPhrases(const PhraseDictionary &phraseDictionary) const
|
2013-07-02 02:27:13 +04:00
|
|
|
{
|
2013-07-03 22:07:36 +04:00
|
|
|
std::map<const PhraseDictionary*, std::pair<const TargetPhraseCollection*, const void*> >::const_iterator iter;
|
2013-07-02 02:27:13 +04:00
|
|
|
iter = m_targetPhrases.find(&phraseDictionary);
|
2013-07-04 12:24:13 +04:00
|
|
|
if (iter == m_targetPhrases.end()) {
|
2013-07-05 02:38:18 +04:00
|
|
|
return NULL;
|
2013-07-04 12:24:13 +04:00
|
|
|
}
|
2013-07-02 02:27:13 +04:00
|
|
|
return iter->second.first;
|
|
|
|
}
|
|
|
|
|
2013-07-07 05:14:51 +04:00
|
|
|
const void *InputPath::GetPtNode(const PhraseDictionary &phraseDictionary) const
|
2013-07-03 22:07:36 +04:00
|
|
|
{
|
|
|
|
std::map<const PhraseDictionary*, std::pair<const TargetPhraseCollection*, const void*> >::const_iterator iter;
|
|
|
|
iter = m_targetPhrases.find(&phraseDictionary);
|
2013-07-04 12:24:13 +04:00
|
|
|
if (iter == m_targetPhrases.end()) {
|
2013-07-05 02:38:18 +04:00
|
|
|
return NULL;
|
2013-07-04 12:24:13 +04:00
|
|
|
}
|
2013-07-03 22:07:36 +04:00
|
|
|
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-26 22:39:10 +04:00
|
|
|
std::pair<const TargetPhraseCollection*, const void*> value(targetPhrases, ptNode);
|
|
|
|
m_targetPhrases[&phraseDictionary] = value;
|
2013-07-19 18:38:13 +04:00
|
|
|
}
|
|
|
|
|
2013-07-19 20:41:52 +04:00
|
|
|
bool InputPath::SetPlaceholders(TargetPhrase *targetPhrase) const
|
2013-07-19 18:38:13 +04:00
|
|
|
{
|
2013-07-19 21:23:38 +04:00
|
|
|
FactorType sourcePlaceholderFactor = StaticData::Instance().GetPlaceholderFactor().first;
|
|
|
|
FactorType targetPlaceholderFactor = StaticData::Instance().GetPlaceholderFactor().second;
|
2013-07-19 20:41:52 +04:00
|
|
|
|
|
|
|
const AlignmentInfo &alignments = targetPhrase->GetAlignTerm();
|
2013-07-19 18:38:13 +04:00
|
|
|
for (size_t i = 0; i < m_placeholders.size(); ++i) {
|
2013-07-19 18:38:39 +04:00
|
|
|
size_t sourcePos = m_placeholders[i];
|
|
|
|
set<size_t> targetPos = alignments.GetAlignmentsForSource(sourcePos);
|
2013-07-19 20:41:52 +04:00
|
|
|
if (targetPos.size() == 1) {
|
|
|
|
const Word &sourceWord = m_phrase.GetWord(sourcePos);
|
|
|
|
Word &targetWord = targetPhrase->GetWord(*targetPos.begin());
|
2013-07-19 21:23:38 +04:00
|
|
|
targetWord[targetPlaceholderFactor] = sourceWord[sourcePlaceholderFactor];
|
2013-07-19 20:41:52 +04:00
|
|
|
} else {
|
2013-07-19 18:38:39 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-19 18:38:13 +04:00
|
|
|
}
|
|
|
|
return true;
|
2013-07-18 23:23:44 +04:00
|
|
|
}
|
|
|
|
|
2013-07-30 18:28:20 +04:00
|
|
|
const Word &InputPath::GetLastWord() const
|
|
|
|
{
|
|
|
|
size_t len = m_phrase.GetSize();
|
|
|
|
CHECK(len);
|
|
|
|
const Word &ret = m_phrase.GetWord(len - 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-07 05:14:51 +04:00
|
|
|
std::ostream& operator<<(std::ostream& out, const InputPath& obj)
|
2013-07-03 22:07:36 +04:00
|
|
|
{
|
2013-07-05 02:38:18 +04:00
|
|
|
out << &obj << " " << obj.GetWordsRange() << " " << obj.GetPrevNode() << " " << obj.GetPhrase();
|
2013-07-04 12:24:13 +04:00
|
|
|
|
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-04 12:24:13 +04:00
|
|
|
|
2013-07-05 02:38:18 +04:00
|
|
|
return out;
|
2013-07-03 22:07:36 +04:00
|
|
|
}
|
|
|
|
|
2013-07-02 02:27:13 +04:00
|
|
|
}
|