Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TargetPhrase.h « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 554ac2793775a4f80f52e300379cc53c9b1a2517 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// -*- c++ -*-
// $Id$

/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
***********************************************************************/

#ifndef moses_TargetPhrase_h
#define moses_TargetPhrase_h

#include <algorithm>
#include <vector>
#include "TypeDef.h"
#include "Phrase.h"
#include "ScoreComponentCollection.h"
#include "AlignmentInfo.h"
#include "AlignmentInfoCollection.h"
#include "moses/PP/PhraseProperty.h"
#include "util/string_piece.hh"
//#include "moses/TranslationTask.h"

#include <boost/shared_ptr.hpp>
#include <boost/unordered_map.hpp>

#ifdef HAVE_PROTOBUF
#include "rule.pb.h"
#endif

namespace Moses
{
class FeatureFunction;
class InputPath;
class InputPath;
class PhraseDictionary;

/** represents an entry on the target side of a phrase table (scores, translation, alignment)
 */
class TargetPhrase: public Phrase
{
public:
  typedef std::map<FeatureFunction const*, boost::shared_ptr<Scores> > ScoreCache_t;
  ScoreCache_t const& GetExtraScores() const;
  Scores const* GetExtraScores(FeatureFunction const* ff) const;
  void SetExtraScores(FeatureFunction const* ff,boost::shared_ptr<Scores> const& scores);


private:
  ScoreCache_t m_cached_scores;
  ttaskwptr m_ttask;
  bool m_ttask_flag;

private:
  friend std::ostream& operator<<(std::ostream&, const TargetPhrase&);
  friend void swap(TargetPhrase &first, TargetPhrase &second);

  float m_fullScore, m_futureScore;
  ScoreComponentCollection m_scoreBreakdown;

  const AlignmentInfo* m_alignTerm, *m_alignNonTerm;
  const Word *m_lhsTarget;
  mutable Phrase *m_ruleSource; // to be set by the feature function that needs it.

  typedef std::map<std::string, boost::shared_ptr<PhraseProperty> > Properties;
  Properties m_properties;

  const PhraseDictionary *m_container;

  mutable boost::unordered_map<const std::string, boost::shared_ptr<void> > m_data;

public:
  TargetPhrase(const PhraseDictionary *pt = NULL);
  TargetPhrase(std::string out_string, const PhraseDictionary *pt = NULL);
  TargetPhrase(const TargetPhrase &copy);
  explicit TargetPhrase(const Phrase &targetPhrase, const PhraseDictionary *pt);

  /*ttasksptr version*/
  TargetPhrase(ttasksptr &ttask, const PhraseDictionary *pt = NULL);
  TargetPhrase(ttasksptr &ttask, std::string out_string, const PhraseDictionary *pt = NULL);
  explicit TargetPhrase(ttasksptr &ttask, const Phrase &targetPhrase, const PhraseDictionary *pt);
  ttasksptr const GetTtask() const;
  bool HasTtaskSPtr() const;

  ~TargetPhrase();

  // 1st evaluate method. Called during loading of phrase table.
  void EvaluateInIsolation(const Phrase &source, const std::vector<FeatureFunction*> &ffs);

  // as above, score with ALL FFs
  // Used only for OOV processing. Doesn't have a phrase table connect with it
  void EvaluateInIsolation(const Phrase &source);

  // 'inputPath' is guaranteed to be the raw substring from the input. No factors were added or taken away
  void EvaluateWithSourceContext(const InputType &input, const InputPath &inputPath);

  void UpdateScore(ScoreComponentCollection *futureScoreBreakdown = NULL);

  void SetSparseScore(const FeatureFunction* translationScoreProducer, const StringPiece &sparseString);

  // used to set translation or gen score
  void SetXMLScore(float score);

#ifdef HAVE_PROTOBUF
  void WriteToRulePB(hgmert::Rule* pb) const;
#endif

  /***
   * return the estimated score resulting from our being added to a sentence
   * (it's an estimate because we don't have full n-gram info for the language model
   *  without using the (unknown) full sentence)
   *
   */
  inline float GetFutureScore() const {
    return m_fullScore;
  }

  inline const ScoreComponentCollection &GetScoreBreakdown() const {
    return m_scoreBreakdown;
  }
  inline ScoreComponentCollection &GetScoreBreakdown() {
    return m_scoreBreakdown;
  }

  /*
    //TODO: Probably shouldn't copy this, but otherwise ownership is unclear
    void SetSourcePhrase(const Phrase&  p) {
      m_sourcePhrase=p;
    }
    const Phrase& GetSourcePhrase() const {
      return m_sourcePhrase;
    }
  */
  void SetTargetLHS(const Word *lhs) {
    m_lhsTarget = lhs;
  }
  const Word &GetTargetLHS() const {
    return *m_lhsTarget;
  }

  void SetAlignmentInfo(const StringPiece &alignString);
  void SetAlignTerm(const AlignmentInfo *alignTerm) {
    m_alignTerm = alignTerm;
  }
  void SetAlignNonTerm(const AlignmentInfo *alignNonTerm) {
    m_alignNonTerm = alignNonTerm;
  }

  // ALNREP = alignment representation,
  // see AlignmentInfo constructors for supported representations
  template<typename ALNREP>
  void
  SetAlignTerm(const ALNREP &coll) {
    m_alignTerm = AlignmentInfoCollection::Instance().Add(coll);
  }

  // ALNREP = alignment representation,
  // see AlignmentInfo constructors for supported representations
  template<typename ALNREP>
  void
  SetAlignNonTerm(const ALNREP &coll) {
    m_alignNonTerm = AlignmentInfoCollection::Instance().Add(coll);
  }


  const AlignmentInfo &GetAlignTerm() const {
    return *m_alignTerm;
  }
  const AlignmentInfo &GetAlignNonTerm() const {
    return *m_alignNonTerm;
  }

  const Phrase *GetRuleSource() const {
    return m_ruleSource;
  }

  const PhraseDictionary *GetContainer() const {
    return m_container;
  }

  bool SetData(const std::string& key, boost::shared_ptr<void> value) const {
    std::pair< boost::unordered_map<const std::string, boost::shared_ptr<void> >::iterator, bool > inserted =
      m_data.insert( std::pair<const std::string, boost::shared_ptr<void> >(key,value) );
    if (!inserted.second) {
      return false;
    }
    return true;
  }

  boost::shared_ptr<void> GetData(const std::string& key) const {
    boost::unordered_map<const std::string, boost::shared_ptr<void> >::const_iterator found = m_data.find(key);
    if (found == m_data.end()) {
      return boost::shared_ptr<void>();
    }
    return found->second;
  }



  // To be set by the FF that needs it, by default the rule source = NULL
  // make a copy of the source side of the rule
  void SetRuleSource(const Phrase &ruleSource) const;

  void SetProperties(const StringPiece &str);
  void SetProperty(const std::string &key, const std::string &value);
  const PhraseProperty *GetProperty(const std::string &key) const;

  void Merge(const TargetPhrase &copy, const std::vector<FactorType>& factorVec);

  bool operator< (const TargetPhrase &compare) const; // NOT IMPLEMENTED
  bool operator== (const TargetPhrase &compare) const; // NOT IMPLEMENTED

  TO_STRING();
};

void swap(TargetPhrase &first, TargetPhrase &second);

std::ostream& operator<<(std::ostream&, const TargetPhrase&);

/**
 * Hasher that looks at source and target phrase.
 **/
struct TargetPhraseHasher {
  inline size_t operator()(const TargetPhrase& targetPhrase) const {
    size_t seed = 0;
    boost::hash_combine(seed, targetPhrase);
    boost::hash_combine(seed, targetPhrase.GetAlignTerm());
    boost::hash_combine(seed, targetPhrase.GetAlignNonTerm());

    return seed;
  }
};

struct TargetPhraseComparator {
  inline bool operator()(const TargetPhrase& lhs, const TargetPhrase& rhs) const {
    return lhs.Compare(rhs) == 0 &&
           lhs.GetAlignTerm() == rhs.GetAlignTerm() &&
           lhs.GetAlignNonTerm() == rhs.GetAlignNonTerm();
  }

};

}

#endif