2010-10-12 20:17:41 +04:00
|
|
|
// $Id$
|
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based, hierarchical and syntactic language decoder
|
|
|
|
Copyright (C) 2009 Hieu Hoang
|
2011-02-24 16:57:11 +03:00
|
|
|
|
2010-10-12 20:17:41 +04:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2011-02-24 16:57:11 +03:00
|
|
|
|
2010-10-12 20:17:41 +04:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
2011-02-24 16:57:11 +03:00
|
|
|
|
2010-10-12 20:17:41 +04:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
***********************************************************************/
|
2010-04-08 21:16:10 +04:00
|
|
|
#include <iostream>
|
2011-11-18 16:07:41 +04:00
|
|
|
#include "util/check.hh"
|
2012-11-12 23:56:18 +04:00
|
|
|
#include "moses/Util.h"
|
2010-04-08 21:16:10 +04:00
|
|
|
#include "Phrase.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace OnDiskPt
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2012-09-20 00:48:10 +04:00
|
|
|
void Phrase::AddWord(WordPtr word)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
m_words.push_back(word);
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2012-09-20 00:48:10 +04:00
|
|
|
void Phrase::AddWord(WordPtr word, size_t pos)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-11-18 16:07:41 +04:00
|
|
|
CHECK(pos < m_words.size());
|
2011-02-24 16:57:11 +03:00
|
|
|
m_words.insert(m_words.begin() + pos + 1, word);
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int Phrase::Compare(const Phrase &compare) const
|
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
int ret = 0;
|
|
|
|
for (size_t pos = 0; pos < GetSize(); ++pos) {
|
|
|
|
if (pos >= compare.GetSize()) {
|
|
|
|
// we're bigger than the other. Put 1st
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Word &thisWord = GetWord(pos)
|
|
|
|
,&compareWord = compare.GetWord(pos);
|
|
|
|
int wordRet = thisWord.Compare(compareWord);
|
|
|
|
if (wordRet != 0) {
|
|
|
|
ret = wordRet;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == 0) {
|
2011-11-18 16:07:41 +04:00
|
|
|
CHECK(compare.GetSize() >= GetSize());
|
2011-02-24 16:57:11 +03:00
|
|
|
ret = (compare.GetSize() > GetSize()) ? 1 : 0;
|
|
|
|
}
|
|
|
|
return ret;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//! transitive comparison
|
|
|
|
bool Phrase::operator<(const Phrase &compare) const
|
2011-02-24 16:57:11 +03:00
|
|
|
{
|
|
|
|
int ret = Compare(compare);
|
|
|
|
return ret < 0;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Phrase::operator>(const Phrase &compare) const
|
2011-02-24 16:57:11 +03:00
|
|
|
{
|
|
|
|
int ret = Compare(compare);
|
|
|
|
return ret > 0;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Phrase::operator==(const Phrase &compare) const
|
2011-02-24 16:57:11 +03:00
|
|
|
{
|
|
|
|
int ret = Compare(compare);
|
|
|
|
return ret == 0;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2012-05-15 19:16:16 +04:00
|
|
|
void Phrase::DebugPrint(ostream &out, const Vocab &vocab) const
|
|
|
|
{
|
|
|
|
for (size_t pos = 0; pos < GetSize(); ++pos) {
|
|
|
|
const Word &word = GetWord(pos);
|
2012-05-15 19:46:16 +04:00
|
|
|
word.DebugPrint(out, vocab);
|
|
|
|
out << " ";
|
2012-05-15 19:16:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
std::ostream& operator<<(std::ostream &out, const Phrase &phrase)
|
|
|
|
{
|
2011-02-24 16:57:11 +03:00
|
|
|
for (size_t pos = 0; pos < phrase.GetSize(); ++pos) {
|
|
|
|
const Word &word = phrase.GetWord(pos);
|
|
|
|
out << word << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2011-02-24 16:57:11 +03:00
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|