2011-05-11 02:02:25 +04:00
|
|
|
#include "PhraseBoundaryFeature.h"
|
|
|
|
|
|
|
|
#include "Hypothesis.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace Moses {
|
|
|
|
|
|
|
|
int PhraseBoundaryState::Compare(const FFState& other) const
|
|
|
|
{
|
|
|
|
const PhraseBoundaryState& rhs = dynamic_cast<const PhraseBoundaryState&>(other);
|
2011-06-24 17:06:58 +04:00
|
|
|
int tgt = Word::Compare(*m_targetWord,*(rhs.m_targetWord));
|
|
|
|
if (tgt) return tgt;
|
|
|
|
return Word::Compare(*m_sourceWord,*(rhs.m_sourceWord));
|
2011-05-11 02:02:25 +04:00
|
|
|
}
|
|
|
|
|
2013-01-02 20:06:22 +04:00
|
|
|
PhraseBoundaryFeature::PhraseBoundaryFeature(const std::string &line)
|
2013-02-02 00:23:36 +04:00
|
|
|
: StatefulFeatureFunction("PhraseBoundaryFeature", ScoreProducer::unlimited, 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-02-05 03:48:26 +04:00
|
|
|
for (size_t i = 0; i < m_args.size(); ++i) {
|
|
|
|
const vector<string> &args = m_args[i];
|
2013-01-02 20:06:22 +04:00
|
|
|
|
2013-02-05 03:48:26 +04:00
|
|
|
if (args[0] == "source") {
|
|
|
|
m_sourceFactors = Tokenize<FactorType>(args[1], ",");
|
2013-01-02 20:06:22 +04:00
|
|
|
}
|
2013-02-05 03:48:26 +04:00
|
|
|
else if (args[0] == "target") {
|
|
|
|
m_targetFactors = Tokenize<FactorType>(args[1], ",");
|
2013-01-02 20:06:22 +04:00
|
|
|
}
|
2013-02-19 21:31:11 +04:00
|
|
|
else {
|
|
|
|
throw "Unknown argument " + args[0];
|
|
|
|
}
|
2013-01-02 20:06:22 +04:00
|
|
|
}
|
2011-05-11 02:02:25 +04:00
|
|
|
}
|
|
|
|
|
2011-08-22 17:52:02 +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,
|
|
|
|
ScoreComponentCollection* scores) const {
|
|
|
|
for (size_t i = 0; i < factors.size(); ++i) {
|
|
|
|
ostringstream name;
|
|
|
|
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_;
|
|
|
|
}
|
|
|
|
scores->PlusEquals(this,name.str(),1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
FFState* PhraseBoundaryFeature::Evaluate
|
|
|
|
(const Hypothesis& cur_hypo, const FFState* prev_state,
|
|
|
|
ScoreComponentCollection* scores) const
|
|
|
|
{
|
|
|
|
const PhraseBoundaryState* pbState = dynamic_cast<const PhraseBoundaryState*>(prev_state);
|
|
|
|
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
|
|
|
|
2011-06-24 17:06:58 +04:00
|
|
|
const Phrase* sourcePhrase = cur_hypo.GetSourcePhrase();
|
|
|
|
const Word* leftSourceWord = pbState->GetSourceWord();
|
|
|
|
const Word* rightSourceWord = &(sourcePhrase->GetWord(0));
|
|
|
|
AddFeatures(leftSourceWord,rightSourceWord,m_sourceFactors,"src",scores);
|
|
|
|
|
|
|
|
const Word* endSourceWord = &(sourcePhrase->GetWord(sourcePhrase->GetSize()-1));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|