/* * PhraseImpl.cpp * * Created on: 23 Oct 2015 * Author: hieu */ #include #include #include "Phrase.h" #include "Word.h" #include "legacy/Util2.h" #include "MemPool.h" using namespace std; 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; } //////////////////////////////////////////////////////////////////////////////////////// PhraseImpl *PhraseImpl::CreateFromString(MemPool &pool, FactorCollection &vocab, const System &system, const std::string &str) { vector toks = Tokenize(str); size_t size = toks.size(); PhraseImpl *ret; ret = new (pool.Allocate()) PhraseImpl(pool, size); ret->CreateFromString(vocab, system, toks); return ret; } void PhraseImpl::CreateFromString(FactorCollection &vocab, const System &system, const std::vector &toks) { for (size_t i = 0; i < m_size; ++i) { Word &word = (*this)[i]; word.CreateFromString(vocab, system, toks[i]); } } PhraseImpl::PhraseImpl(MemPool &pool, size_t size) :m_size(size) { m_words = new (pool.Allocate(size)) Word[size]; } PhraseImpl::PhraseImpl(MemPool &pool, const PhraseImpl ©) :m_size(copy.GetSize()) { m_words = new (pool.Allocate(m_size)) Word[m_size]; for (size_t i = 0; i < m_size; ++i) { const Word &word = copy[i]; (*this)[i] = word; } } PhraseImpl::~PhraseImpl() { } SubPhrase PhraseImpl::GetSubPhrase(size_t start, size_t end) const { SubPhrase ret(*this, start, end); return ret; } 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; } //////////////////////////////////////////////////////////////////////////////////////// SubPhrase::SubPhrase(const PhraseImpl &origPhrase, size_t start, size_t end) :m_origPhrase(&origPhrase) ,m_start(start) ,m_end(end) { } std::ostream& operator<<(std::ostream &out, const SubPhrase &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; }