00001 #include "TargetBigramFeature.h"
00002 #include "moses/Phrase.h"
00003 #include "moses/TargetPhrase.h"
00004 #include "moses/Hypothesis.h"
00005 #include "moses/ScoreComponentCollection.h"
00006 #include "util/string_piece_hash.hh"
00007 #include "util/exception.hh"
00008
00009 using namespace std;
00010
00011 namespace Moses
00012 {
00013
00014 size_t TargetBigramState::hash() const
00015 {
00016 std::size_t ret = hash_value(m_word);
00017 return ret;
00018 }
00019
00020 bool TargetBigramState::operator==(const FFState& other) const
00021 {
00022 const TargetBigramState& rhs = static_cast<const TargetBigramState&>(other);
00023 return m_word == rhs.m_word;
00024 }
00025
00027 TargetBigramFeature::TargetBigramFeature(const std::string &line)
00028 :StatefulFeatureFunction(0, line)
00029 {
00030 std::cerr << "Initializing target bigram feature.." << std::endl;
00031 ReadParameters();
00032
00033 FactorCollection& factorCollection = FactorCollection::Instance();
00034 const Factor* bosFactor =
00035 factorCollection.AddFactor(Output,m_factorType,BOS_);
00036 m_bos.SetFactor(m_factorType,bosFactor);
00037
00038 }
00039
00040 void TargetBigramFeature::SetParameter(const std::string& key, const std::string& value)
00041 {
00042 if (key == "factor") {
00043 m_factorType = Scan<FactorType>(value);
00044 } else if (key == "path") {
00045 m_filePath = value;
00046 } else {
00047 StatefulFeatureFunction::SetParameter(key, value);
00048 }
00049 }
00050
00051 void TargetBigramFeature::Load(AllOptions::ptr const& opts)
00052 {
00053 m_options = opts;
00054 if (m_filePath == "*")
00055 return ;
00056 ifstream inFile(m_filePath.c_str());
00057 UTIL_THROW_IF2(!inFile, "Can't open file " << m_filePath);
00058
00059 std::string line;
00060 m_vocab.insert(BOS_);
00061 m_vocab.insert(BOS_);
00062 while (getline(inFile, line)) {
00063 m_vocab.insert(line);
00064 }
00065
00066 inFile.close();
00067 }
00068
00069
00070 const FFState* TargetBigramFeature::EmptyHypothesisState(const InputType &) const
00071 {
00072 return new TargetBigramState(m_bos);
00073 }
00074
00075 FFState* TargetBigramFeature::EvaluateWhenApplied(const Hypothesis& cur_hypo,
00076 const FFState* prev_state,
00077 ScoreComponentCollection* accumulator) const
00078 {
00079 const TargetBigramState* tbState = static_cast<const TargetBigramState*>(prev_state);
00080 assert(tbState);
00081
00082
00083 const Phrase& targetPhrase = cur_hypo.GetCurrTargetPhrase();
00084 if (targetPhrase.GetSize() == 0) {
00085 return new TargetBigramState(*tbState);
00086 }
00087
00088
00089 for (size_t i = 0; i < targetPhrase.GetSize(); ++i) {
00090 const Factor* f1 = NULL;
00091 if (i == 0) {
00092 f1 = tbState->GetWord().GetFactor(m_factorType);
00093 } else {
00094 f1 = targetPhrase.GetWord(i-1).GetFactor(m_factorType);
00095 }
00096 const Factor* f2 = targetPhrase.GetWord(i).GetFactor(m_factorType);
00097 const StringPiece w1 = f1->GetString();
00098 const StringPiece w2 = f2->GetString();
00099
00100
00101 if (m_vocab.size() &&
00102 (FindStringPiece(m_vocab, w1) == m_vocab.end() || FindStringPiece(m_vocab, w2) == m_vocab.end())) {
00103 continue;
00104 }
00105
00106 string name(w1.data(), w1.size());
00107 name += ":";
00108 name.append(w2.data(), w2.size());
00109 accumulator->PlusEquals(this,name,1);
00110 }
00111
00112 if (cur_hypo.GetWordsBitmap().IsComplete()) {
00113 const StringPiece w1 = targetPhrase.GetWord(targetPhrase.GetSize()-1).GetFactor(m_factorType)->GetString();
00114 const string& w2 = EOS_;
00115 if (m_vocab.empty() || (FindStringPiece(m_vocab, w1) != m_vocab.end())) {
00116 string name(w1.data(), w1.size());
00117 name += ":";
00118 name += w2;
00119 accumulator->PlusEquals(this,name,1);
00120 }
00121 return NULL;
00122 }
00123 return new TargetBigramState(targetPhrase.GetWord(targetPhrase.GetSize()-1));
00124 }
00125
00126 bool TargetBigramFeature::IsUseable(const FactorMask &mask) const
00127 {
00128 bool ret = mask[m_factorType];
00129 return ret;
00130 }
00131
00132 }
00133