00001 #include <sstream>
00002 #include "TargetWordInsertionFeature.h"
00003 #include "moses/Phrase.h"
00004 #include "moses/TargetPhrase.h"
00005 #include "moses/Hypothesis.h"
00006 #include "moses/ChartHypothesis.h"
00007 #include "moses/ScoreComponentCollection.h"
00008 #include "moses/TranslationOption.h"
00009 #include "util/string_piece_hash.hh"
00010 #include "util/exception.hh"
00011
00012 namespace Moses
00013 {
00014
00015 using namespace std;
00016
00017 TargetWordInsertionFeature::TargetWordInsertionFeature(const std::string &line)
00018 :StatelessFeatureFunction(0, line),
00019 m_unrestricted(true)
00020 {
00021 VERBOSE(1, "Initializing feature " << GetScoreProducerDescription() << " ...");
00022 ReadParameters();
00023 VERBOSE(1, " Done." << std::endl);
00024 }
00025
00026 void TargetWordInsertionFeature::SetParameter(const std::string& key, const std::string& value)
00027 {
00028 if (key == "factor") {
00029 m_factorType = Scan<FactorType>(value);
00030 } else if (key == "path") {
00031 m_filename = value;
00032 } else {
00033 StatelessFeatureFunction::SetParameter(key, value);
00034 }
00035 }
00036
00037 void TargetWordInsertionFeature::Load(AllOptions::ptr const& opts)
00038 {
00039 m_options = opts;
00040 if (m_filename.empty())
00041 return;
00042
00043 FEATUREVERBOSE(1, "Loading target word insertion word list from " << m_filename << std::endl);
00044 ifstream inFile(m_filename.c_str());
00045 UTIL_THROW_IF2(!inFile, "could not open file " << m_filename);
00046
00047 std::string line;
00048 while (getline(inFile, line)) {
00049 m_vocab.insert(line);
00050 }
00051
00052 inFile.close();
00053
00054 m_unrestricted = false;
00055 }
00056
00057 void TargetWordInsertionFeature::EvaluateInIsolation(const Phrase &source
00058 , const TargetPhrase &targetPhrase
00059 , ScoreComponentCollection &scoreBreakdown
00060 , ScoreComponentCollection &estimatedScores) const
00061 {
00062 const AlignmentInfo &alignmentInfo = targetPhrase.GetAlignTerm();
00063 ComputeFeatures(source, targetPhrase, &scoreBreakdown, alignmentInfo);
00064 }
00065
00066 void TargetWordInsertionFeature::ComputeFeatures(const Phrase &source,
00067 const TargetPhrase& targetPhrase,
00068 ScoreComponentCollection* accumulator,
00069 const AlignmentInfo &alignmentInfo) const
00070 {
00071
00072 size_t targetLength = targetPhrase.GetSize();
00073 size_t sourceLength = source.GetSize();
00074 if (targetLength == 1 && sourceLength == 1 && !alignmentInfo.GetSize()) return;
00075
00076
00077 std::vector<bool> aligned(targetLength, false);
00078 for (AlignmentInfo::const_iterator alignmentPoint = alignmentInfo.begin(); alignmentPoint != alignmentInfo.end(); alignmentPoint++) {
00079 aligned[ alignmentPoint->second ] = true;
00080 }
00081
00082
00083 for(size_t i=0; i<targetLength; i++) {
00084 if (!aligned[i]) {
00085 Word w = targetPhrase.GetWord(i);
00086 if (!w.IsNonTerminal()) {
00087 const StringPiece word = w.GetFactor(m_factorType)->GetString();
00088 if (word != "<s>" && word != "</s>") {
00089 if (!m_unrestricted && FindStringPiece(m_vocab, word ) == m_vocab.end()) {
00090 accumulator->PlusEquals(this,StringPiece("OTHER"),1);
00091 } else {
00092 accumulator->PlusEquals(this,word,1);
00093 }
00094 }
00095 }
00096 }
00097 }
00098 }
00099
00100 bool TargetWordInsertionFeature::IsUseable(const FactorMask &mask) const
00101 {
00102 bool ret = mask[m_factorType];
00103 return ret;
00104 }
00105
00106 }