00001 #include "UnalignedWordCountFeature.h"
00002 #include "moses/Phrase.h"
00003 #include "moses/TargetPhrase.h"
00004 #include "moses/ScoreComponentCollection.h"
00005 #include "moses/StaticData.h"
00006 #include "moses/Util.h"
00007
00008 namespace Moses
00009 {
00010
00011 using namespace std;
00012
00013 UnalignedWordCountFeature::UnalignedWordCountFeature(const std::string &line)
00014 : StatelessFeatureFunction(2, line)
00015 {
00016 VERBOSE(1, "Initializing feature " << GetScoreProducerDescription() << " ...");
00017 ReadParameters();
00018 VERBOSE(1, " Done." << std::endl);
00019 }
00020
00021 void UnalignedWordCountFeature::EvaluateInIsolation(const Phrase &source
00022 , const TargetPhrase &targetPhrase
00023 , ScoreComponentCollection &scoreBreakdown
00024 , ScoreComponentCollection &estimatedScores) const
00025 {
00026 const AlignmentInfo &alignmentInfo = targetPhrase.GetAlignTerm();
00027 const size_t sourceLength = source.GetSize();
00028 const size_t targetLength = targetPhrase.GetSize();
00029
00030 std::vector<bool> alignedSource(sourceLength, false);
00031 std::vector<bool> alignedTarget(targetLength, false);
00032
00033 for (AlignmentInfo::const_iterator alignmentPoint = alignmentInfo.begin(); alignmentPoint != alignmentInfo.end(); ++alignmentPoint) {
00034 alignedSource[ alignmentPoint->first ] = true;
00035 alignedTarget[ alignmentPoint->second ] = true;
00036 }
00037
00038 size_t sourceUnalignedCount = 0;
00039
00040 for (size_t j=0; j<sourceLength; ++j) {
00041 if (!alignedSource[j]) {
00042 if (!source.GetWord(j).IsNonTerminal()) {
00043 ++sourceUnalignedCount;
00044 }
00045 }
00046 }
00047
00048 size_t targetUnalignedCount = 0;
00049
00050 for (size_t i=0; i<targetLength; i++) {
00051 if (!alignedTarget[i]) {
00052 if (!targetPhrase.GetWord(i).IsNonTerminal()) {
00053 ++targetUnalignedCount;
00054 }
00055 }
00056 }
00057
00058 scoreBreakdown.PlusEquals(m_index, sourceUnalignedCount);
00059 scoreBreakdown.PlusEquals(m_index+1, targetUnalignedCount);
00060
00061 IFFEATUREVERBOSE(2) {
00062 FEATUREVERBOSE(2, source << std::endl);
00063 FEATUREVERBOSE(2, targetPhrase << std::endl);
00064
00065 for (AlignmentInfo::const_iterator it=targetPhrase.GetAlignTerm().begin();
00066 it!=targetPhrase.GetAlignTerm().end(); ++it) {
00067 FEATUREVERBOSE(2, "alignTerm " << it->first << " " << it->second << std::endl);
00068 }
00069
00070 for (AlignmentInfo::const_iterator it=targetPhrase.GetAlignNonTerm().begin();
00071 it!=targetPhrase.GetAlignNonTerm().end(); ++it) {
00072 FEATUREVERBOSE(2, "alignNonTerm " << it->first << " " << it->second << std::endl);
00073 }
00074
00075 FEATUREVERBOSE(2, "sourceLength= " << sourceLength << std::endl);
00076 FEATUREVERBOSE(2, "targetLength= " << targetLength << std::endl);
00077 FEATUREVERBOSE(2, "sourceUnalignedCount= " << sourceUnalignedCount << std::endl);
00078 FEATUREVERBOSE(2, "targetUnalignedCount= " << targetUnalignedCount << std::endl);
00079 }
00080 }
00081
00082 }