mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2024-09-11 11:25:40 +03:00
move lookup of all subphrases into PhraseDictionary class
This commit is contained in:
parent
ac8d10af38
commit
5e355c1595
@ -4,10 +4,24 @@ namespace Moses
|
||||
{
|
||||
const TargetPhraseCollection *InputLatticeNode::GetTargetPhrases(const PhraseDictionary &phraseDictionary) const
|
||||
{
|
||||
std::map<const PhraseDictionary*, std::pair<const TargetPhraseCollection*, void*> >::const_iterator iter;
|
||||
std::map<const PhraseDictionary*, std::pair<const TargetPhraseCollection*, const void*> >::const_iterator iter;
|
||||
iter = m_targetPhrases.find(&phraseDictionary);
|
||||
CHECK(iter != m_targetPhrases.end());
|
||||
return iter->second.first;
|
||||
}
|
||||
|
||||
const void *InputLatticeNode::GetPtNode(const PhraseDictionary &phraseDictionary) const
|
||||
{
|
||||
std::map<const PhraseDictionary*, std::pair<const TargetPhraseCollection*, const void*> >::const_iterator iter;
|
||||
iter = m_targetPhrases.find(&phraseDictionary);
|
||||
CHECK(iter != m_targetPhrases.end());
|
||||
return iter->second.second;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const InputLatticeNode& obj)
|
||||
{
|
||||
out << &obj << " " << obj.GetWordsRange() << " " << obj.GetPrevNode() << " " << obj.GetPhrase();
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
#include "Phrase.h"
|
||||
#include "WordsRange.h"
|
||||
|
||||
@ -18,14 +19,16 @@ This is for both sentence input, and confusion network/lattices
|
||||
*/
|
||||
class InputLatticeNode
|
||||
{
|
||||
friend std::ostream& operator<<(std::ostream& out, const InputLatticeNode &pbj);
|
||||
|
||||
protected:
|
||||
const InputLatticeNode *m_prevNode;
|
||||
Phrase m_phrase;
|
||||
WordsRange m_range;
|
||||
std::map<const PhraseDictionary*, std::pair<const TargetPhraseCollection*, void*> > m_targetPhrases;
|
||||
std::map<const PhraseDictionary*, std::pair<const TargetPhraseCollection*, const void*> > m_targetPhrases;
|
||||
|
||||
public:
|
||||
InputLatticeNode()
|
||||
explicit InputLatticeNode()
|
||||
: m_prevNode(NULL)
|
||||
, m_range(NOT_FOUND, NOT_FOUND)
|
||||
{}
|
||||
@ -41,14 +44,18 @@ public:
|
||||
const WordsRange &GetWordsRange() const {
|
||||
return m_range;
|
||||
}
|
||||
const InputLatticeNode *GetPrevNode() const {
|
||||
return m_prevNode;
|
||||
}
|
||||
|
||||
void SetTargetPhrases(const PhraseDictionary &phraseDictionary
|
||||
, const TargetPhraseCollection *targetPhrases
|
||||
, void *ptNode) {
|
||||
std::pair<const TargetPhraseCollection*, void*> value(targetPhrases, ptNode);
|
||||
, const void *ptNode) {
|
||||
std::pair<const TargetPhraseCollection*, const void*> value(targetPhrases, ptNode);
|
||||
m_targetPhrases[&phraseDictionary] = value;
|
||||
}
|
||||
const TargetPhraseCollection *GetTargetPhrases(const PhraseDictionary &phraseDictionary) const;
|
||||
const void *GetPtNode(const PhraseDictionary &phraseDictionary) const;
|
||||
|
||||
};
|
||||
|
||||
|
@ -71,18 +71,13 @@ void PhraseDictionary::SetFeaturesToApply()
|
||||
|
||||
void PhraseDictionary::SetTargetPhraseFromPtMatrix(const std::vector<InputLatticeNode*> &phraseDictionaryQueue) const
|
||||
{
|
||||
// UTIL_THROW(util::Exception, "SetTargetPhraseFromPtMatrix() not implemented");
|
||||
// UTIL_THROW(util::Exception, "SetTargetPhraseFromPtMatrix() not implemented");
|
||||
for (size_t i = 0; i < phraseDictionaryQueue.size(); ++i) {
|
||||
InputLatticeNode &node = *phraseDictionaryQueue[i];
|
||||
|
||||
bool doIt = true;
|
||||
|
||||
doIt = true; // TODO HACK
|
||||
if (doIt) {
|
||||
const Phrase &phrase = node.GetPhrase();
|
||||
const TargetPhraseCollection *targetPhrases = this->GetTargetPhraseCollection(phrase);
|
||||
node.SetTargetPhrases(*this, targetPhrases, NULL);
|
||||
}
|
||||
const Phrase &phrase = node.GetPhrase();
|
||||
const TargetPhraseCollection *targetPhrases = this->GetTargetPhraseCollection(phrase);
|
||||
node.SetTargetPhrases(*this, targetPhrases, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,6 +34,7 @@
|
||||
#include "moses/TranslationModel/RuleTable/LoaderFactory.h"
|
||||
#include "moses/TranslationModel/RuleTable/Loader.h"
|
||||
#include "moses/TranslationModel/CYKPlusParser/ChartRuleLookupManagerMemory.h"
|
||||
#include "moses/InputLatticeNode.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -132,7 +133,46 @@ void PhraseDictionaryMemory::SortAndPrune()
|
||||
m_collection.Sort(GetTableLimit());
|
||||
}
|
||||
}
|
||||
/*
|
||||
void PhraseDictionaryMemory::SetTargetPhraseFromPtMatrix(const std::vector<InputLatticeNode*> &phraseDictionaryQueue) const
|
||||
{
|
||||
// UTIL_THROW(util::Exception, "SetTargetPhraseFromPtMatrix() not implemented");
|
||||
for (size_t i = 0; i < phraseDictionaryQueue.size(); ++i) {
|
||||
bool doIt = true;
|
||||
InputLatticeNode &node = *phraseDictionaryQueue[i];
|
||||
const Phrase &phrase = node.GetPhrase();
|
||||
const InputLatticeNode *prevNode = node.GetPrevNode();
|
||||
cerr << node << endl;
|
||||
|
||||
const PhraseDictionaryNodeMemory *prevPtNode;
|
||||
|
||||
if (prevNode) {
|
||||
prevPtNode = static_cast<const PhraseDictionaryNodeMemory*>(prevNode->GetPtNode(*this));
|
||||
|
||||
if (prevPtNode == NULL) {
|
||||
doIt = false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Starting subphrase.
|
||||
assert(phrase.GetSize() == 1);
|
||||
prevPtNode = &GetRootNode();
|
||||
}
|
||||
|
||||
if (doIt) {
|
||||
const Word &lastWord = phrase.GetWord(phrase.GetSize() - 1);
|
||||
const PhraseDictionaryNodeMemory *ptNode = prevPtNode->GetChild(lastWord);
|
||||
if (ptNode) {
|
||||
const TargetPhraseCollection *targetPhrases = ptNode->GetTargetPhraseCollection();
|
||||
node.SetTargetPhrases(*this, targetPhrases, ptNode);
|
||||
}
|
||||
else {
|
||||
node.SetTargetPhrases(*this, NULL, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
TO_STRING_BODY(PhraseDictionaryMemory);
|
||||
|
||||
// friend
|
||||
|
@ -53,6 +53,8 @@ public:
|
||||
const InputType &,
|
||||
const ChartCellCollectionBase &);
|
||||
|
||||
//void SetTargetPhraseFromPtMatrix(const std::vector<InputLatticeNode*> &phraseDictionaryQueue) const;
|
||||
|
||||
TO_STRING();
|
||||
|
||||
protected:
|
||||
|
@ -598,22 +598,5 @@ void TranslationOptionCollection::SetTargetPhraseFromPtMatrix()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
void TranslationOptionCollection::SetTargetPhraseFromPtMatrix(const PhraseDictionary &phraseDictionary)
|
||||
{
|
||||
for (size_t i = 0; i < m_phraseDictionaryQueue.size(); ++i) {
|
||||
InputLatticeNode &node = *m_phraseDictionaryQueue[i];
|
||||
|
||||
bool doIt = true;
|
||||
|
||||
doIt = true; // TODO HACK
|
||||
if (doIt) {
|
||||
const Phrase &phrase = node.GetPhrase();
|
||||
const TargetPhraseCollection *targetPhrases = phraseDictionary.GetTargetPhraseCollection(phrase);
|
||||
node.SetTargetPhrases(phraseDictionary, targetPhrases, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
@ -37,9 +37,11 @@ TranslationOptionCollectionText::TranslationOptionCollectionText(Sentence const
|
||||
{
|
||||
size_t size = input.GetSize();
|
||||
m_targetPhrasesfromPt.resize(size);
|
||||
for (size_t startPos = 0; startPos < size; ++startPos) {
|
||||
std::vector<InputLatticeNode> &vec = m_targetPhrasesfromPt[startPos];
|
||||
for (size_t endPos = startPos; endPos < size; ++endPos) {
|
||||
for (size_t phaseSize = 1; phaseSize <= size; ++phaseSize) {
|
||||
for (size_t startPos = 0; startPos < size - phaseSize + 1; ++startPos) {
|
||||
size_t endPos = startPos + phaseSize -1;
|
||||
std::vector<InputLatticeNode> &vec = m_targetPhrasesfromPt[startPos];
|
||||
|
||||
Phrase subphrase(input.GetSubString(WordsRange(startPos, endPos)));
|
||||
WordsRange range(startPos, endPos);
|
||||
|
||||
@ -48,10 +50,11 @@ TranslationOptionCollectionText::TranslationOptionCollectionText(Sentence const
|
||||
vec.push_back(node);
|
||||
}
|
||||
else {
|
||||
const InputLatticeNode prevNode = GetInputLatticeNode(startPos, endPos - 1);
|
||||
const InputLatticeNode &prevNode = GetInputLatticeNode(startPos, endPos - 1);
|
||||
InputLatticeNode node(subphrase, range, &prevNode);
|
||||
vec.push_back(node);
|
||||
}
|
||||
cerr << vec.back() << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user