00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #pragma once
00021
00022 #include <iostream>
00023 #include <ostream>
00024 #include <set>
00025 #include <vector>
00026 #include <cstdlib>
00027
00028 #include <boost/functional/hash.hpp>
00029 #include "TypeDef.h"
00030 namespace Moses
00031 {
00032
00033 class AlignmentInfoCollection;
00034
00038 class AlignmentInfo
00039 {
00040 friend std::ostream& operator<<(std::ostream &, const AlignmentInfo &);
00041 friend struct AlignmentInfoOrderer;
00042 friend struct AlignmentInfoHasher;
00043 friend class AlignmentInfoCollection;
00044 friend class VW;
00045
00046 public:
00047 typedef std::set<std::pair<size_t,size_t> > CollType;
00048 typedef std::vector<size_t> NonTermIndexMap;
00049 typedef CollType::const_iterator const_iterator;
00050
00051 const_iterator begin() const {
00052 return m_collection.begin();
00053 }
00054 const_iterator end() const {
00055 return m_collection.end();
00056 }
00057
00058 void Add(size_t sourcePos, size_t targetPos) {
00059 m_collection.insert(std::pair<size_t, size_t>(sourcePos, targetPos));
00060 }
00065 const NonTermIndexMap &GetNonTermIndexMap() const {
00066 return m_nonTermIndexMap;
00067 }
00068
00071 const NonTermIndexMap &GetNonTermIndexMap2() const {
00072 return m_nonTermIndexMap2;
00073 }
00074
00075 const CollType &GetAlignments() const {
00076 return m_collection;
00077 }
00078
00079 std::set<size_t> GetAlignmentsForSource(size_t sourcePos) const;
00080 std::set<size_t> GetAlignmentsForTarget(size_t targetPos) const;
00081
00082 size_t GetSize() const {
00083 return m_collection.size();
00084 }
00085
00086 std::vector< const std::pair<size_t,size_t>* >
00087 GetSortedAlignments(WordAlignmentSort SortOrder) const;
00088
00089 std::vector<size_t> GetSourceIndex2PosMap() const;
00090
00091 bool operator==(const AlignmentInfo& rhs) const {
00092 return m_collection == rhs.m_collection &&
00093 m_nonTermIndexMap == rhs.m_nonTermIndexMap;
00094 }
00095
00096 private:
00098 explicit AlignmentInfo(const std::set<std::pair<size_t,size_t> > &pairs);
00099 explicit AlignmentInfo(const std::vector<unsigned char> &aln);
00100
00101
00102 explicit AlignmentInfo(const std::string &str);
00103
00104 void BuildNonTermIndexMaps();
00105
00106 CollType m_collection;
00107 NonTermIndexMap m_nonTermIndexMap;
00108 NonTermIndexMap m_nonTermIndexMap2;
00109 };
00110
00114 struct AlignmentInfoOrderer {
00115 bool operator()(const AlignmentInfo &a, const AlignmentInfo &b) const {
00116 if (a.m_collection == b.m_collection) {
00117 return a.m_nonTermIndexMap < b.m_nonTermIndexMap;
00118 } else {
00119 return a.m_collection < b.m_collection;
00120 }
00121 }
00122 };
00123
00127 struct AlignmentInfoHasher {
00128 size_t operator()(const AlignmentInfo& a) const {
00129 size_t seed = 0;
00130 boost::hash_combine(seed,a.m_collection);
00131 boost::hash_combine(seed,a.m_nonTermIndexMap);
00132 return seed;
00133 }
00134
00135 };
00136
00137 inline size_t hash_value(const AlignmentInfo& a)
00138 {
00139 static AlignmentInfoHasher hasher;
00140 return hasher(a);
00141 }
00142
00143 }