00001 #pragma once 00002 00003 #include <string> 00004 #include <map> 00005 #include "StatefulFeatureFunction.h" 00006 #include "FFState.h" 00007 #include "moses/Phrase.h" 00008 00009 namespace Moses 00010 { 00011 class ConstrainedDecodingState : public FFState 00012 { 00013 public: 00014 ConstrainedDecodingState() { 00015 } 00016 00017 ConstrainedDecodingState(const Hypothesis &hypo); 00018 ConstrainedDecodingState(const ChartHypothesis &hypo); 00019 00020 virtual size_t hash() const; 00021 virtual bool operator==(const FFState& other) const; 00022 00023 const Phrase &GetPhrase() const { 00024 return m_outputPhrase; 00025 } 00026 00027 protected: 00028 Phrase m_outputPhrase; 00029 }; 00030 00032 00033 // only allow hypotheses which match reference 00034 class ConstrainedDecoding : public StatefulFeatureFunction 00035 { 00036 public: 00037 ConstrainedDecoding(const std::string &line); 00038 00039 void Load(AllOptions::ptr const& opts); 00040 00041 bool IsUseable(const FactorMask &mask) const { 00042 return true; 00043 } 00044 00045 FFState* EvaluateWhenApplied( 00046 const Hypothesis& cur_hypo, 00047 const FFState* prev_state, 00048 ScoreComponentCollection* accumulator) const; 00049 00050 FFState* EvaluateWhenApplied( 00051 const ChartHypothesis& /* cur_hypo */, 00052 int /* featureID - used to index the state in the previous hypotheses */, 00053 ScoreComponentCollection* accumulator) const; 00054 00055 virtual const FFState* EmptyHypothesisState(const InputType &input) const { 00056 return new ConstrainedDecodingState(); 00057 } 00058 00059 std::vector<float> DefaultWeights() const; 00060 00061 void SetParameter(const std::string& key, const std::string& value); 00062 00063 protected: 00064 std::vector<std::string> m_paths; 00065 std::map<long, std::vector<Phrase> > m_constraints; 00066 int m_maxUnknowns; 00067 bool m_negate; // only keep translations which DON'T match the reference 00068 bool m_soft; 00069 00070 }; 00071 00072 00073 } 00074