mosesdecoder/contrib/other-builds/moses2/PhraseImpl.h

90 lines
1.7 KiB
C
Raw Normal View History

#pragma once
2016-02-19 23:58:31 +03:00
#include <vector>
#include <string>
#include "Phrase.h"
2016-02-19 23:58:31 +03:00
#include "SubPhrase.h"
#include "legacy/Util2.h"
namespace Moses2
{
class SubPhrase;
template<typename WORD>
class PhraseImplTemplate : public Phrase
{
public:
PhraseImplTemplate(MemPool &pool, size_t size)
2016-02-19 23:58:31 +03:00
:m_size(size)
{
m_words = new (pool.Allocate<WORD>(size)) WORD[size];
2016-02-19 23:58:31 +03:00
}
PhraseImplTemplate(MemPool &pool, const PhraseImplTemplate &copy)
2016-02-19 23:58:31 +03:00
:m_size(copy.GetSize())
{
m_words = new (pool.Allocate<WORD>(m_size)) WORD[m_size];
2016-02-19 23:58:31 +03:00
for (size_t i = 0; i < m_size; ++i) {
const WORD &word = copy[i];
2016-02-19 23:58:31 +03:00
(*this)[i] = word;
}
}
virtual ~PhraseImplTemplate()
2016-02-19 23:58:31 +03:00
{}
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; }
2016-02-19 23:58:31 +03:00
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<std::string> &toks)
2016-02-19 23:58:31 +03:00
{
for (size_t i = 0; i < m_size; ++i) {
WORD &word = (*this)[i];
2016-02-19 23:58:31 +03:00
word.CreateFromString(vocab, system, toks[i]);
}
}
};
class PhraseImpl : public PhraseImplTemplate<Word>
{
public:
static PhraseImpl *CreateFromString(MemPool &pool, FactorCollection &vocab, const System &system, const std::string &str)
{
std::vector<std::string> toks = Moses2::Tokenize(str);
size_t size = toks.size();
PhraseImpl *ret;
ret = new (pool.Allocate<PhraseImpl>()) PhraseImpl(pool, size);
ret->PhraseImplTemplate<Word>::CreateFromString(vocab, system, toks);
return ret;
}
PhraseImpl(MemPool &pool, size_t size)
:PhraseImplTemplate(pool, size)
{}
};
2016-02-19 23:58:31 +03:00
}