mosesdecoder/contrib/other-builds/moses2/Phrase.cpp

165 lines
3.3 KiB
C++
Raw Normal View History

2015-10-23 22:53:36 +03:00
/*
2015-11-03 17:20:10 +03:00
* PhraseImpl.cpp
2015-10-23 22:53:36 +03:00
*
* Created on: 23 Oct 2015
* Author: hieu
*/
2015-12-07 17:43:10 +03:00
#include <boost/functional/hash.hpp>
2015-10-24 01:19:31 +03:00
#include <vector>
2015-10-23 22:53:36 +03:00
#include "Phrase.h"
#include "Word.h"
2015-11-11 19:23:49 +03:00
#include "legacy/Util2.h"
2015-10-28 19:11:12 +03:00
#include "MemPool.h"
2015-10-24 01:19:31 +03:00
using namespace std;
2015-12-10 23:49:30 +03:00
namespace Moses2
{
2015-12-07 17:43:10 +03:00
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;
}
2015-12-15 16:36:44 +03:00
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;
}
2015-12-18 21:38:24 +03:00
std::string Phrase::GetString(const FactorList &factorTypes) const
{
if (GetSize() == 0) {
return "";
}
std::stringstream ret;
const Word &word = (*this)[0];
ret << word.GetString(factorTypes);
2015-12-20 03:10:28 +03:00
for (size_t i = 1; i < GetSize(); ++i) {
const Word &word = (*this)[i];
2015-12-18 21:38:24 +03:00
ret << " " << word.GetString(factorTypes);
}
return ret.str();
}
2015-12-07 17:43:10 +03:00
////////////////////////////////////////////////////////////////////////////////////////
2015-11-18 14:08:32 +03:00
PhraseImpl *PhraseImpl::CreateFromString(MemPool &pool, FactorCollection &vocab, const System &system, const std::string &str)
2015-10-24 01:19:31 +03:00
{
2015-11-12 02:28:18 +03:00
vector<string> toks = Tokenize(str);
2015-10-24 01:19:31 +03:00
size_t size = toks.size();
2015-11-03 17:20:10 +03:00
PhraseImpl *ret;
2015-10-24 01:19:31 +03:00
2015-11-03 17:20:10 +03:00
ret = new (pool.Allocate<PhraseImpl>()) PhraseImpl(pool, size);
2015-10-24 04:32:58 +03:00
2015-11-18 14:08:32 +03:00
ret->CreateFromString(vocab, system, toks);
2015-10-24 01:19:31 +03:00
return ret;
}
2015-10-23 22:53:36 +03:00
2015-11-18 14:08:32 +03:00
void PhraseImpl::CreateFromString(FactorCollection &vocab, const System &system, const std::vector<std::string> &toks)
2015-10-24 04:02:50 +03:00
{
for (size_t i = 0; i < m_size; ++i) {
Word &word = (*this)[i];
2015-11-18 14:08:32 +03:00
word.CreateFromString(vocab, system, toks[i]);
2015-10-24 04:02:50 +03:00
}
}
2015-11-03 17:20:10 +03:00
PhraseImpl::PhraseImpl(MemPool &pool, size_t size)
2015-10-24 04:02:50 +03:00
:m_size(size)
2015-10-23 22:53:36 +03:00
{
2015-10-24 05:32:30 +03:00
m_words = new (pool.Allocate<Word>(size)) Word[size];
2015-10-23 22:53:36 +03:00
}
2015-12-07 19:49:02 +03:00
PhraseImpl::PhraseImpl(MemPool &pool, const PhraseImpl &copy)
:m_size(copy.GetSize())
{
m_words = new (pool.Allocate<Word>(m_size)) Word[m_size];
for (size_t i = 0; i < m_size; ++i) {
const Word &word = copy[i];
(*this)[i] = word;
}
}
2015-11-03 17:20:10 +03:00
PhraseImpl::~PhraseImpl() {
2015-10-26 19:02:07 +03:00
2015-10-23 22:53:36 +03:00
}
2015-11-03 17:20:10 +03:00
SubPhrase PhraseImpl::GetSubPhrase(size_t start, size_t end) const
2015-10-24 01:19:31 +03:00
{
SubPhrase ret(*this, start, end);
return ret;
}
2015-11-04 19:46:04 +03:00
std::ostream& operator<<(std::ostream &out, const Phrase &obj)
2015-10-26 19:32:47 +03:00
{
2015-10-26 21:42:42 +03:00
if (obj.GetSize()) {
out << obj[0];
for (size_t i = 1; i < obj.GetSize(); ++i) {
const Word &word = obj[i];
out << " " << word;
}
2015-10-26 19:32:47 +03:00
}
return out;
}
2015-12-09 15:41:59 +03:00
void PhraseImpl::Prefetch() const
{
2015-12-09 15:56:03 +03:00
for (size_t i = 0; i < m_size; ++i) {
const Word &word = m_words[i];
const Factor *factor = word[0];
__builtin_prefetch(factor);
}
2015-12-09 15:41:59 +03:00
}
2015-10-26 19:32:47 +03:00
2015-12-07 17:43:10 +03:00
////////////////////////////////////////////////////////////////////////////////////////
2015-11-03 17:20:10 +03:00
SubPhrase::SubPhrase(const PhraseImpl &origPhrase, size_t start, size_t end)
2015-10-29 22:11:11 +03:00
:m_origPhrase(&origPhrase)
2015-10-24 01:19:31 +03:00
,m_start(start)
,m_end(end)
{
}
2015-10-26 19:32:47 +03:00
std::ostream& operator<<(std::ostream &out, const SubPhrase &obj)
{
2015-10-26 21:42:42 +03:00
if (obj.GetSize()) {
out << obj[0];
for (size_t i = 1; i < obj.GetSize(); ++i) {
const Word &word = obj[i];
out << " " << word;
}
2015-10-26 19:32:47 +03:00
}
return out;
}
2015-12-10 23:49:30 +03:00
SubPhrase SubPhrase::GetSubPhrase(size_t start, size_t end) const
{
SubPhrase ret(*m_origPhrase, m_start + start, m_start + end);
return ret;
}
2015-12-10 23:49:30 +03:00
}