2016-02-19 22:32:32 +03:00
|
|
|
#pragma once
|
2016-02-19 23:58:31 +03:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2016-02-19 22:32:32 +03:00
|
|
|
#include "Phrase.h"
|
2016-02-19 23:58:31 +03:00
|
|
|
#include "SubPhrase.h"
|
|
|
|
#include "legacy/Util2.h"
|
2016-02-19 22:32:32 +03:00
|
|
|
|
|
|
|
namespace Moses2
|
|
|
|
{
|
|
|
|
class SubPhrase;
|
|
|
|
|
2016-02-22 21:32:59 +03:00
|
|
|
template<typename WORD>
|
|
|
|
class PhraseImplTemplate : public Phrase
|
2016-02-19 22:32:32 +03:00
|
|
|
{
|
|
|
|
public:
|
2016-02-22 21:32:59 +03:00
|
|
|
PhraseImplTemplate(MemPool &pool, size_t size)
|
2016-02-19 23:58:31 +03:00
|
|
|
:m_size(size)
|
|
|
|
{
|
2016-02-22 21:32:59 +03:00
|
|
|
m_words = new (pool.Allocate<WORD>(size)) WORD[size];
|
2016-02-19 23:58:31 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-22 21:32:59 +03:00
|
|
|
PhraseImplTemplate(MemPool &pool, const PhraseImplTemplate ©)
|
2016-02-19 23:58:31 +03:00
|
|
|
:m_size(copy.GetSize())
|
|
|
|
{
|
2016-02-22 21:32:59 +03:00
|
|
|
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) {
|
2016-02-22 21:32:59 +03:00
|
|
|
const WORD &word = copy[i];
|
2016-02-19 23:58:31 +03:00
|
|
|
(*this)[i] = word;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-22 21:32:59 +03:00
|
|
|
virtual ~PhraseImplTemplate()
|
2016-02-19 23:58:31 +03:00
|
|
|
{}
|
2016-02-19 22:32:32 +03:00
|
|
|
|
2016-02-22 21:32:59 +03:00
|
|
|
const WORD& operator[](size_t pos) const {
|
2016-02-19 22:32:32 +03:00
|
|
|
return m_words[pos];
|
|
|
|
}
|
|
|
|
|
2016-02-22 21:32:59 +03:00
|
|
|
WORD& operator[](size_t pos) {
|
2016-02-19 22:32:32 +03:00
|
|
|
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;
|
|
|
|
}
|
2016-02-19 22:32:32 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
size_t m_size;
|
2016-02-22 21:32:59 +03:00
|
|
|
WORD *m_words;
|
2016-02-19 22:32:32 +03:00
|
|
|
|
2016-02-22 21:32:59 +03:00
|
|
|
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) {
|
2016-02-22 21:32:59 +03:00
|
|
|
WORD &word = (*this)[i];
|
2016-02-19 23:58:31 +03:00
|
|
|
word.CreateFromString(vocab, system, toks[i]);
|
|
|
|
}
|
|
|
|
}
|
2016-02-22 21:32:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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 22:32:32 +03:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-02-19 23:58:31 +03:00
|
|
|
|
2016-02-19 22:32:32 +03:00
|
|
|
}
|