mosesdecoder/moses/FF/SourceWordDeletionFeature.cpp

108 lines
3.0 KiB
C++
Raw Normal View History

#include <sstream>
#include "SourceWordDeletionFeature.h"
2013-05-24 21:02:49 +04:00
#include "moses/Phrase.h"
#include "moses/TargetPhrase.h"
#include "moses/Hypothesis.h"
#include "moses/ChartHypothesis.h"
#include "moses/ScoreComponentCollection.h"
#include "moses/TranslationOption.h"
#include "moses/Util.h"
#include "util/string_piece_hash.hh"
#include "util/exception.hh"
2013-05-29 21:16:15 +04:00
namespace Moses
{
using namespace std;
SourceWordDeletionFeature::SourceWordDeletionFeature(const std::string &line)
:StatelessFeatureFunction(0, line),
2013-05-29 21:16:15 +04:00
m_unrestricted(true)
{
std::cerr << "Initializing source word deletion feature.." << std::endl;
ReadParameters();
}
void SourceWordDeletionFeature::SetParameter(const std::string& key, const std::string& value)
{
if (key == "factor") {
m_factorType = Scan<FactorType>(value);
} else if (key == "path") {
m_filename = value;
} else {
StatelessFeatureFunction::SetParameter(key, value);
}
}
void SourceWordDeletionFeature::Load()
{
if (m_filename == "") {
return;
}
cerr << "loading source word deletion word list from " << m_filename << endl;
ifstream inFile(m_filename.c_str());
2013-11-23 00:27:46 +04:00
UTIL_THROW_IF2(!inFile, "Can't open file " << m_filename);
std::string line;
while (getline(inFile, line)) {
m_vocab.insert(line);
}
inFile.close();
m_unrestricted = false;
}
bool SourceWordDeletionFeature::IsUseable(const FactorMask &mask) const
{
bool ret = mask[m_factorType];
return ret;
}
void SourceWordDeletionFeature::EvaluateInIsolation(const Phrase &source
2013-05-29 21:16:15 +04:00
, const TargetPhrase &targetPhrase
, ScoreComponentCollection &scoreBreakdown
, ScoreComponentCollection &estimatedFutureScore) const
{
const AlignmentInfo &alignmentInfo = targetPhrase.GetAlignTerm();
ComputeFeatures(source, targetPhrase, &scoreBreakdown, alignmentInfo);
}
void SourceWordDeletionFeature::ComputeFeatures(const Phrase &source,
2013-05-29 21:16:15 +04:00
const TargetPhrase& targetPhrase,
ScoreComponentCollection* accumulator,
const AlignmentInfo &alignmentInfo) const
{
// handle special case: unknown words (they have no word alignment)
2013-05-29 21:16:15 +04:00
size_t targetLength = targetPhrase.GetSize();
size_t sourceLength = source.GetSize();
if (targetLength == 1 && sourceLength == 1 && !alignmentInfo.GetSize()) return;
// flag aligned words
2014-10-15 17:20:40 +04:00
std::vector<bool> aligned(sourceLength, false);
for (AlignmentInfo::const_iterator alignmentPoint = alignmentInfo.begin(); alignmentPoint != alignmentInfo.end(); alignmentPoint++)
aligned[ alignmentPoint->first ] = true;
2013-05-29 21:16:15 +04:00
// process unaligned source words
for(size_t i=0; i<sourceLength; i++) {
if (!aligned[i]) {
2013-05-29 21:16:15 +04:00
const Word &w = source.GetWord(i);
if (!w.IsNonTerminal()) {
const StringPiece word = w.GetFactor(m_factorType)->GetString();
if (word != "<s>" && word != "</s>") {
if (!m_unrestricted && FindStringPiece(m_vocab, word ) == m_vocab.end()) {
accumulator->PlusEquals(this, StringPiece("OTHER"),1);
} else {
accumulator->PlusEquals(this,word,1);
}
}
}
}
}
}
}