00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef moses_Phrase_h
00024 #define moses_Phrase_h
00025
00026 #include <iostream>
00027 #include <vector>
00028 #include <list>
00029 #include <string>
00030
00031 #include <boost/functional/hash.hpp>
00032
00033 #include "Word.h"
00034 #include "Util.h"
00035
00036 #include "util/string_piece.hh"
00037 #include "util/exception.hh"
00038 #include "parameters/AllOptions.h"
00039
00040 namespace Moses
00041 {
00042 class FactorMask;
00043 class Range;
00044 class ContextScope;
00045
00049 class Phrase
00050 {
00051 friend std::ostream& operator<<(std::ostream&, const Phrase&);
00052
00053 protected:
00054 std::vector<Word> m_words;
00055
00056 public:
00057
00058 virtual bool HasScope() const {
00059 return false;
00060 }
00061
00062 virtual SPTR<ContextScope> GetScope() const {
00063 return SPTR<ContextScope>();
00064 }
00065
00066
00068 static void InitializeMemPool();
00069 static void FinalizeMemPool();
00070
00073 Phrase();
00074 explicit Phrase(size_t reserveSize);
00076 explicit Phrase(const std::vector< const Word* > &mergeWords);
00077
00078
00079
00080
00081 void SwapWords(Phrase &other) {
00082 swap(m_words, other.m_words);
00083 }
00084
00086 virtual ~Phrase();
00087
00095 void CreateFromString(FactorDirection direction,
00096 const std::vector<FactorType> &factorOrder,
00097 const StringPiece &phraseString,
00098 Word **lhs);
00099
00103 void MergeFactors(const Phrase ©);
00105 void MergeFactors(const Phrase ©, FactorType factorType);
00107 void MergeFactors(const Phrase ©, const std::vector<FactorType>& factorVec);
00108
00112 bool IsCompatible(const Phrase &inputPhrase) const;
00113 bool IsCompatible(const Phrase &inputPhrase, FactorType factorType) const;
00114 bool IsCompatible(const Phrase &inputPhrase, const std::vector<FactorType>& factorVec) const;
00115
00117 inline size_t GetSize() const {
00118 return m_words.size();
00119 }
00120
00122 inline const Word &GetWord(size_t pos) const {
00123 return m_words[pos];
00124 }
00125 inline Word &GetWord(size_t pos) {
00126 return m_words[pos];
00127 }
00128
00129 inline Word &Front() {
00130 return m_words[0];
00131 }
00132
00133 inline Word &Back() {
00134 return m_words[GetSize() - 1];
00135 }
00136
00137 inline const Word &Front() const {
00138 return m_words[0];
00139 }
00140
00141 inline const Word &Back() const {
00142 return m_words[GetSize() - 1];
00143 }
00144
00146 inline const Factor *GetFactor(size_t pos, FactorType factorType) const {
00147 const Word &ptr = m_words[pos];
00148 return ptr[factorType];
00149 }
00150 inline void SetFactor(size_t pos, FactorType factorType, const Factor *factor) {
00151 Word &ptr = m_words[pos];
00152 ptr[factorType] = factor;
00153 }
00154
00155 size_t GetNumTerminals() const;
00156 size_t GetNumNonTerminals() const {
00157 return GetSize() - GetNumTerminals();
00158 }
00159
00161 bool Contains(const std::vector< std::vector<std::string> > &subPhraseVector
00162 , const std::vector<FactorType> &inputFactor) const;
00163
00164 size_t Find(const Phrase &sought, int maxUnknown) const;
00165
00167 Word &AddWord();
00169 void AddWord(const Word &newWord) {
00170 AddWord() = newWord;
00171 }
00172
00174 void Append(const Phrase &endPhrase);
00175 void PrependWord(const Word &newWord);
00176
00177 void Clear() {
00178 m_words.clear();
00179 }
00180
00181 void RemoveWord(size_t pos) {
00182 UTIL_THROW_IF2(pos >= m_words.size(),
00183 "Referencing position " << pos << " out of bound");
00184 m_words.erase(m_words.begin() + pos);
00185 }
00186
00187 void InitStartEndWord();
00188
00190 Phrase GetSubString(const Range &range) const;
00191 Phrase GetSubString(const Range &range, FactorType factorType) const;
00192
00194
00195 std::string
00196 GetStringRep(std::vector<FactorType> const& factorsToPrint,
00197 AllOptions const* opts=NULL) const;
00198
00199 TO_STRING();
00200
00201
00202 int Compare(const Phrase &other) const;
00203
00207 bool operator< (const Phrase &compare) const {
00208 return Compare(compare) < 0;
00209 }
00210
00211 size_t hash() const;
00212
00213 bool operator==(const Phrase &compare) const;
00214 bool operator!=(const Phrase &compare) const {
00215 return ! (*this == compare);
00216 }
00217
00218 void OnlyTheseFactors(const FactorMask &factors);
00219
00220 };
00221
00222 inline size_t hash_value(const Phrase& phrase)
00223 {
00224 return phrase.hash();
00225 }
00226
00227 struct PhrasePtrComparator {
00228 inline bool operator()(const Phrase* lhs, const Phrase* rhs) const {
00229 return *lhs == *rhs;
00230 }
00231 };
00232
00233 struct PhrasePtrHasher {
00234 inline size_t operator()(const Phrase* phrase) const {
00235 size_t seed = 0;
00236 boost::hash_combine(seed,*phrase);
00237 return seed;
00238 }
00239
00240 };
00241
00242 }
00243
00244 #endif