00001 #ifndef moses_FF_LexicalReordering_SparseReordering_h
00002 #define moses_FF_LexicalReordering_SparseReordering_h
00003
00009 #include <functional>
00010 #include <map>
00011 #include <string>
00012 #include <vector>
00013
00014 #include <boost/unordered_set.hpp>
00015
00016 #include "util/murmur_hash.hh"
00017 #include "util/pool.hh"
00018 #include "util/string_piece.hh"
00019
00020 #include "moses/FeatureVector.h"
00021 #include "moses/ScoreComponentCollection.h"
00022 #include "LRState.h"
00023
00038 namespace Moses
00039 {
00040
00044 struct SparseReorderingFeatureKey {
00045 size_t id;
00046 enum Type {Stack, Phrase, Between} type;
00047 const Factor* word;
00048 bool isCluster;
00049 enum Position {First, Last} position;
00050 enum Side {Source, Target} side;
00051 LRState::ReorderingType reoType;
00052
00053 SparseReorderingFeatureKey(size_t id_, Type type_, const Factor* word_, bool isCluster_,
00054 Position position_, Side side_, LRState::ReorderingType reoType_)
00055 : id(id_), type(type_), word(word_), isCluster(isCluster_),
00056 position(position_), side(side_), reoType(reoType_) {
00057 }
00058
00059 const std::string& Name(const std::string& wordListId) ;
00060 };
00061
00062 struct HashSparseReorderingFeatureKey : public std::unary_function<SparseReorderingFeatureKey, std::size_t> {
00063 std::size_t operator()(const SparseReorderingFeatureKey& key) const {
00064
00065
00066 std::size_t seed = 0;
00067 seed = util::MurmurHashNative(&key.id, sizeof(key.id), seed);
00068 seed = util::MurmurHashNative(&key.type, sizeof(key.type), seed);
00069 seed = util::MurmurHashNative(&key.word, sizeof(key.word), seed);
00070 seed = util::MurmurHashNative(&key.isCluster, sizeof(key.isCluster), seed);
00071 seed = util::MurmurHashNative(&key.position, sizeof(key.position), seed);
00072 seed = util::MurmurHashNative(&key.side, sizeof(key.side), seed);
00073 seed = util::MurmurHashNative(&key.reoType, sizeof(key.reoType), seed);
00074 return seed;
00075 }
00076 };
00077
00078 struct EqualsSparseReorderingFeatureKey :
00079 public std::binary_function<SparseReorderingFeatureKey, SparseReorderingFeatureKey, bool> {
00080 bool operator()(const SparseReorderingFeatureKey& left, const SparseReorderingFeatureKey& right) const {
00081
00082 return left.id == right.id && left.type == right.type && left.word == right.word &&
00083 left.position == right.position && left.side == right.side &&
00084 left.reoType == right.reoType;
00085 }
00086 };
00087
00088 class SparseReordering
00089 {
00090 public:
00091 SparseReordering(const std::map<std::string,std::string>& config, const LexicalReordering* producer);
00092
00093
00094 void CopyScores(const TranslationOption& currentOpt,
00095 const TranslationOption* previousOpt,
00096 const InputType& input,
00097 LRModel::ReorderingType reoType,
00098 LRModel::Direction direction,
00099 ScoreComponentCollection* scores) const ;
00100
00101 private:
00102 const LexicalReordering* m_producer;
00103 typedef std::pair<std::string, boost::unordered_set<const Factor*> > WordList;
00104 std::vector<WordList> m_sourceWordLists;
00105 std::vector<WordList> m_targetWordLists;
00106 typedef std::pair<std::string, boost::unordered_map<const Factor*, const Factor*> > ClusterMap;
00107 std::vector<ClusterMap> m_sourceClusterMaps;
00108 std::vector<ClusterMap> m_targetClusterMaps;
00109 bool m_usePhrase;
00110 bool m_useBetween;
00111 bool m_useStack;
00112 typedef boost::unordered_map<SparseReorderingFeatureKey, FName, HashSparseReorderingFeatureKey, EqualsSparseReorderingFeatureKey> FeatureMap;
00113 FeatureMap m_featureMap;
00114
00115 typedef boost::unordered_map<std::string, float> WeightMap;
00116 WeightMap m_weightMap;
00117 bool m_useWeightMap;
00118 std::vector<FName> m_featureMap2;
00119
00120 void ReadWordList(const std::string& filename, const std::string& id,
00121 SparseReorderingFeatureKey::Side side, std::vector<WordList>* pWordLists);
00122 void ReadClusterMap(const std::string& filename, const std::string& id, SparseReorderingFeatureKey::Side side, std::vector<ClusterMap>* pClusterMaps);
00123 void PreCalculateFeatureNames(size_t index, const std::string& id, SparseReorderingFeatureKey::Side side, const Factor* factor, bool isCluster);
00124 void ReadWeightMap(const std::string& filename);
00125
00126 void AddFeatures(
00127 SparseReorderingFeatureKey::Type type, SparseReorderingFeatureKey::Side side,
00128 const Word& word, SparseReorderingFeatureKey::Position position,
00129 LRModel::ReorderingType reoType,
00130 ScoreComponentCollection* scores) const;
00131
00132 };
00133
00134
00135
00136 }
00137
00138
00139 #endif