#pragma once #include #include #include "Phrase.h" #include "SubPhrase.h" #include "legacy/Util2.h" namespace Moses2 { class SubPhrase; template class PhraseImplTemplate : public Phrase { public: PhraseImplTemplate(MemPool &pool, size_t size) :m_size(size) { m_words = new (pool.Allocate(size)) WORD[size]; } PhraseImplTemplate(MemPool &pool, const PhraseImplTemplate ©) :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; } } virtual ~PhraseImplTemplate() {} const WORD& operator[](size_t pos) const { return m_words[pos]; } WORD& operator[](size_t pos) { return m_words[pos]; } size_t GetSize() const { return m_size; } SubPhrase GetSubPhrase(size_t start, size_t end) const { SubPhrase ret(*this, start, end); return ret; } protected: size_t m_size; WORD *m_words; virtual void 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]); } } }; class PhraseImpl : public PhraseImplTemplate { public: static PhraseImpl *CreateFromString(MemPool &pool, FactorCollection &vocab, const System &system, const std::string &str) { std::vector toks = Moses2::Tokenize(str); size_t size = toks.size(); PhraseImpl *ret; ret = new (pool.Allocate()) PhraseImpl(pool, size); ret->PhraseImplTemplate::CreateFromString(vocab, system, toks); return ret; } PhraseImpl(MemPool &pool, size_t size) :PhraseImplTemplate(pool, size) {} }; }