2015-10-23 22:53:36 +03:00
|
|
|
/*
|
2015-11-03 17:20:10 +03:00
|
|
|
* PhraseImpl.cpp
|
2015-10-23 22:53:36 +03:00
|
|
|
*
|
|
|
|
* 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-11-11 19:23:49 +03:00
|
|
|
#include "legacy/Util2.h"
|
2015-10-28 19:11:12 +03:00
|
|
|
#include "MemPool.h"
|
2015-10-24 01:19:31 +03:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2015-11-13 01:51:13 +03:00
|
|
|
PhraseImpl *PhraseImpl::CreateFromString(MemPool &pool, FactorCollection &vocab, const std::string &str)
|
2015-10-24 01:19:31 +03:00
|
|
|
{
|
2015-11-12 02:28:18 +03:00
|
|
|
vector<string> toks = Tokenize(str);
|
2015-10-24 01:19:31 +03:00
|
|
|
size_t size = toks.size();
|
2015-11-03 17:20:10 +03:00
|
|
|
PhraseImpl *ret;
|
2015-10-24 01:19:31 +03:00
|
|
|
|
2015-11-03 17:20:10 +03:00
|
|
|
ret = new (pool.Allocate<PhraseImpl>()) PhraseImpl(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-11-13 01:51:13 +03:00
|
|
|
void PhraseImpl::CreateFromString(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-11-03 17:20:10 +03:00
|
|
|
PhraseImpl::PhraseImpl(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
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:20:10 +03:00
|
|
|
PhraseImpl::~PhraseImpl() {
|
2015-10-26 19:02:07 +03:00
|
|
|
|
2015-10-23 22:53:36 +03:00
|
|
|
}
|
|
|
|
|
2015-11-03 17:20:10 +03:00
|
|
|
SubPhrase PhraseImpl::GetSubPhrase(size_t start, size_t end) const
|
2015-10-24 01:19:31 +03:00
|
|
|
{
|
|
|
|
SubPhrase ret(*this, start, end);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-11-04 19:46:04 +03:00
|
|
|
std::ostream& operator<<(std::ostream &out, const Phrase &obj)
|
2015-10-26 19:32:47 +03:00
|
|
|
{
|
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
|
|
|
////////////////////////////////////////////////////////////
|
2015-11-03 17:20:10 +03:00
|
|
|
SubPhrase::SubPhrase(const PhraseImpl &origPhrase, size_t start, size_t end)
|
2015-10-29 22:11:11 +03:00
|
|
|
:m_origPhrase(&origPhrase)
|
2015-10-24 01:19:31 +03:00
|
|
|
,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;
|
|
|
|
}
|