2011-05-11 02:02:25 +04:00
|
|
|
#include "PhraseBoundaryFeature.h"
|
|
|
|
|
2013-05-24 21:02:49 +04:00
|
|
|
#include "moses/Hypothesis.h"
|
2013-08-05 16:46:54 +04:00
|
|
|
#include "moses/TranslationOption.h"
|
2013-10-03 14:05:53 +04:00
|
|
|
#include "moses/InputPath.h"
|
2015-10-03 00:15:03 +03:00
|
|
|
#include "util/string_stream.hh"
|
2011-05-11 02:02:25 +04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
2011-05-11 02:02:25 +04:00
|
|
|
|
2015-10-16 15:53:33 +03:00
|
|
|
size_t PhraseBoundaryState::hash() const
|
|
|
|
{
|
2015-10-14 17:21:37 +03:00
|
|
|
size_t ret = hash_value(*m_targetWord);
|
|
|
|
boost::hash_combine(ret, hash_value(*m_sourceWord));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
bool PhraseBoundaryState::operator==(const FFState& other) const
|
|
|
|
{
|
2015-10-18 14:41:36 +03:00
|
|
|
const PhraseBoundaryState& rhs = static_cast<const PhraseBoundaryState&>(other);
|
2015-10-14 17:21:37 +03:00
|
|
|
bool ret = *m_targetWord == *rhs.m_targetWord && *m_sourceWord == *rhs.m_sourceWord;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
2013-01-02 20:06:22 +04:00
|
|
|
PhraseBoundaryFeature::PhraseBoundaryFeature(const std::string &line)
|
2013-10-29 22:59:53 +04:00
|
|
|
: StatefulFeatureFunction(0, line)
|
2011-05-11 02:02:25 +04:00
|
|
|
{
|
2013-01-02 20:06:22 +04:00
|
|
|
std::cerr << "Initializing source word deletion feature.." << std::endl;
|
2013-06-20 16:06:03 +04:00
|
|
|
ReadParameters();
|
2011-05-11 02:02:25 +04:00
|
|
|
}
|
|
|
|
|
2013-06-20 16:25:02 +04:00
|
|
|
void PhraseBoundaryFeature::SetParameter(const std::string& key, const std::string& value)
|
2013-06-11 02:16:28 +04:00
|
|
|
{
|
2013-06-11 04:46:04 +04:00
|
|
|
if (key == "source") {
|
|
|
|
m_sourceFactors = Tokenize<FactorType>(value, ",");
|
|
|
|
} else if (key == "target") {
|
|
|
|
m_targetFactors = Tokenize<FactorType>(value, ",");
|
|
|
|
} else {
|
2013-06-20 16:25:02 +04:00
|
|
|
StatefulFeatureFunction::SetParameter(key, value);
|
2013-06-11 04:46:04 +04:00
|
|
|
}
|
2013-06-11 02:16:28 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
const FFState* PhraseBoundaryFeature::EmptyHypothesisState(const InputType &) const
|
2011-05-11 02:02:25 +04:00
|
|
|
{
|
2011-06-24 17:06:58 +04:00
|
|
|
return new PhraseBoundaryState(NULL,NULL);
|
2011-05-11 02:02:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PhraseBoundaryFeature::AddFeatures(
|
|
|
|
const Word* leftWord, const Word* rightWord, const FactorList& factors, const string& side,
|
2013-05-29 21:16:15 +04:00
|
|
|
ScoreComponentCollection* scores) const
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < factors.size(); ++i) {
|
2015-10-03 02:33:38 +03:00
|
|
|
util::StringStream name;
|
2013-05-29 21:16:15 +04:00
|
|
|
name << side << ":";
|
|
|
|
name << factors[i];
|
|
|
|
name << ":";
|
|
|
|
if (leftWord) {
|
|
|
|
name << leftWord->GetFactor(factors[i])->GetString();
|
|
|
|
} else {
|
|
|
|
name << BOS_;
|
|
|
|
}
|
|
|
|
name << ":";
|
|
|
|
if (rightWord) {
|
|
|
|
name << rightWord->GetFactor(factors[i])->GetString();
|
|
|
|
} else {
|
|
|
|
name << EOS_;
|
2011-05-11 02:02:25 +04:00
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
scores->PlusEquals(this,name.str(),1);
|
|
|
|
}
|
2011-05-11 02:02:25 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-10 02:41:08 +04:00
|
|
|
FFState* PhraseBoundaryFeature::EvaluateWhenApplied
|
2013-05-29 21:16:15 +04:00
|
|
|
(const Hypothesis& cur_hypo, const FFState* prev_state,
|
|
|
|
ScoreComponentCollection* scores) const
|
2011-05-11 02:02:25 +04:00
|
|
|
{
|
2015-10-18 14:41:36 +03:00
|
|
|
const PhraseBoundaryState* pbState = static_cast<const PhraseBoundaryState*>(prev_state);
|
2011-05-11 02:02:25 +04:00
|
|
|
const Phrase& targetPhrase = cur_hypo.GetCurrTargetPhrase();
|
|
|
|
if (targetPhrase.GetSize() == 0) {
|
|
|
|
return new PhraseBoundaryState(*pbState);
|
|
|
|
}
|
2011-06-24 17:06:58 +04:00
|
|
|
const Word* leftTargetWord = pbState->GetTargetWord();
|
|
|
|
const Word* rightTargetWord = &(targetPhrase.GetWord(0));
|
|
|
|
AddFeatures(leftTargetWord,rightTargetWord,m_targetFactors,"tgt",scores);
|
2011-05-11 02:02:25 +04:00
|
|
|
|
2013-08-13 23:36:32 +04:00
|
|
|
const Phrase& sourcePhrase = cur_hypo.GetTranslationOption().GetInputPath().GetPhrase();
|
2011-06-24 17:06:58 +04:00
|
|
|
const Word* leftSourceWord = pbState->GetSourceWord();
|
2013-08-05 16:46:54 +04:00
|
|
|
const Word* rightSourceWord = &(sourcePhrase.GetWord(0));
|
2011-06-24 17:06:58 +04:00
|
|
|
AddFeatures(leftSourceWord,rightSourceWord,m_sourceFactors,"src",scores);
|
|
|
|
|
2013-08-05 16:46:54 +04:00
|
|
|
const Word* endSourceWord = &(sourcePhrase.GetWord(sourcePhrase.GetSize()-1));
|
2011-06-24 17:06:58 +04:00
|
|
|
const Word* endTargetWord = &(targetPhrase.GetWord(targetPhrase.GetSize()-1));
|
2011-05-11 02:02:25 +04:00
|
|
|
|
|
|
|
//if end of sentence add EOS
|
|
|
|
if (cur_hypo.IsSourceCompleted()) {
|
2011-06-24 17:06:58 +04:00
|
|
|
AddFeatures(endSourceWord,NULL,m_sourceFactors,"src",scores);
|
|
|
|
AddFeatures(endTargetWord,NULL,m_targetFactors,"tgt",scores);
|
2011-05-11 02:02:25 +04:00
|
|
|
}
|
|
|
|
|
2011-06-24 17:06:58 +04:00
|
|
|
return new PhraseBoundaryState(endSourceWord,endTargetWord);
|
2011-05-11 02:02:25 +04:00
|
|
|
}
|
|
|
|
|
2013-05-30 15:41:08 +04:00
|
|
|
bool PhraseBoundaryFeature::IsUseable(const FactorMask &mask) const
|
|
|
|
{
|
2013-05-30 15:51:40 +04:00
|
|
|
for (size_t i = 0; i < m_targetFactors.size(); ++i) {
|
|
|
|
const FactorType &factor = m_targetFactors[i];
|
|
|
|
if (!mask[factor]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2013-05-30 15:41:08 +04:00
|
|
|
}
|
2011-05-11 02:02:25 +04:00
|
|
|
|
|
|
|
}
|