mosesdecoder/contrib/other-builds/moses2/Phrase.cpp
2016-02-25 17:50:44 +00:00

97 lines
1.9 KiB
C++

/*
* PhraseImpl.cpp
*
* Created on: 23 Oct 2015
* Author: hieu
*/
#include <boost/functional/hash.hpp>
#include "Phrase.h"
#include "Word.h"
#include "MemPool.h"
#include "Scores.h"
#include "System.h"
using namespace std;
namespace Moses2
{
size_t Phrase::hash() const
{
size_t seed = 0;
for (size_t i = 0; i < GetSize(); ++i) {
const Word &word = (*this)[i];
size_t wordHash = word.hash();
boost::hash_combine(seed, wordHash);
}
return seed;
}
bool Phrase::operator==(const Phrase &compare) const
{
if (GetSize() != compare.GetSize()) {
return false;
}
for (size_t i = 0; i < GetSize(); ++i) {
const Word &word = (*this)[i];
const Word &otherWord = compare[i];
if (word != otherWord) {
return false;
}
}
return true;
}
std::string Phrase::GetString(const FactorList &factorTypes) const
{
if (GetSize() == 0) {
return "";
}
std::stringstream ret;
const Word &word = (*this)[0];
ret << word.GetString(factorTypes);
for (size_t i = 1; i < GetSize(); ++i) {
const Word &word = (*this)[i];
ret << " " << word.GetString(factorTypes);
}
return ret.str();
}
std::ostream& operator<<(std::ostream &out, const Phrase &obj)
{
if (obj.GetSize()) {
out << obj[0];
for (size_t i = 1; i < obj.GetSize(); ++i) {
const Word &word = obj[i];
out << " " << word;
}
}
return out;
}
////////////////////////////////////////////////////////////////////////
TargetPhrase::TargetPhrase(MemPool &pool, const PhraseTable &pt, const System &system)
:pt(pt)
,scoreProperties(NULL)
{
m_scores = new (pool.Allocate<Scores>()) Scores(system, pool, system.featureFunctions.GetNumScores());
}
SCORE TargetPhrase::GetFutureScore() const
{ return m_scores->GetTotalScore() + m_estimatedScore; }
SCORE *TargetPhrase::GetScoresProperty(int propertyInd) const
{
return scoreProperties ? scoreProperties + propertyInd : NULL;
}
} // namespace