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

86 lines
1.6 KiB
C++
Raw Normal View History

2015-10-23 22:53:36 +03:00
/*
* Phrase.cpp
*
* Created on: 23 Oct 2015
* Author: hieu
*/
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-10-24 01:19:31 +03:00
#include "moses/Util.h"
2015-10-28 19:11:12 +03:00
#include "MemPool.h"
2015-10-24 01:19:31 +03:00
using namespace std;
2015-10-28 19:11:12 +03:00
Phrase *Phrase::CreateFromString(MemPool &pool, Moses::FactorCollection &vocab, const std::string &str)
2015-10-24 01:19:31 +03:00
{
vector<string> toks = Moses::Tokenize(str);
size_t size = toks.size();
2015-10-24 04:32:58 +03:00
Phrase *ret;
2015-10-24 01:19:31 +03:00
2015-10-24 05:32:30 +03:00
ret = new (pool.Allocate<Phrase>()) Phrase(pool, size);
2015-10-24 04:32:58 +03:00
2015-10-26 17:58:59 +03:00
ret->CreateFromString(vocab, toks);
2015-10-24 01:19:31 +03:00
return ret;
}
2015-10-23 22:53:36 +03:00
2015-10-26 17:58:59 +03:00
void Phrase::CreateFromString(Moses::FactorCollection &vocab, 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-10-26 17:58:59 +03:00
word.CreateFromString(vocab, toks[i]);
2015-10-24 04:02:50 +03:00
}
}
2015-10-28 19:11:12 +03:00
Phrase::Phrase(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
}
Phrase::~Phrase() {
2015-10-26 19:02:07 +03:00
2015-10-23 22:53:36 +03:00
}
2015-10-24 01:19:31 +03:00
SubPhrase Phrase::GetSubPhrase(size_t start, size_t end) const
{
SubPhrase ret(*this, start, end);
return ret;
}
2015-10-26 19:32:47 +03:00
std::ostream& operator<<(std::ostream &out, const Phrase &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-10-24 01:19:31 +03:00
////////////////////////////////////////////////////////////
SubPhrase::SubPhrase(const Phrase &origPhrase, size_t start, size_t end)
:m_origPhrase(origPhrase)
,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;
}