mosesdecoder/moses/TargetPhrase.h

187 lines
5.6 KiB
C
Raw Normal View History

// $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
2013-08-22 13:16:24 +04:00
#include <algorithm>
#include <vector>
#include "TypeDef.h"
#include "Phrase.h"
#include "ScoreComponentCollection.h"
#include "AlignmentInfo.h"
#include "moses/PP/PhraseProperty.h"
#include "util/string_piece.hh"
#include <boost/shared_ptr.hpp>
#ifdef HAVE_PROTOBUF
#include "rule.pb.h"
#endif
namespace Moses
{
2013-02-22 00:03:35 +04:00
class FeatureFunction;
class InputPath;
2014-08-04 18:14:40 +04:00
class InputPath;
class PhraseDictionary;
/** represents an entry on the target side of a phrase table (scores, translation, alignment)
*/
class TargetPhrase: public Phrase
{
2013-08-22 13:16:24 +04:00
private:
friend std::ostream& operator<<(std::ostream&, const TargetPhrase&);
2013-08-22 13:16:24 +04:00
friend void swap(TargetPhrase &first, TargetPhrase &second);
float m_fullScore, m_futureScore;
ScoreComponentCollection m_scoreBreakdown;
2013-05-29 21:16:15 +04:00
const AlignmentInfo* m_alignTerm, *m_alignNonTerm;
const Word *m_lhsTarget;
mutable Phrase *m_ruleSource; // to be set by the feature function that needs it.
2014-06-10 13:29:49 +04:00
typedef std::map<std::string, boost::shared_ptr<PhraseProperty> > Properties;
Properties m_properties;
2014-08-04 18:14:40 +04:00
const PhraseDictionary *m_container;
public:
TargetPhrase(const PhraseDictionary *pt = NULL);
2014-08-04 18:46:40 +04:00
TargetPhrase(const TargetPhrase &copy);
2014-08-04 18:59:32 +04:00
explicit TargetPhrase(const Phrase &targetPhrase, const PhraseDictionary *pt);
2013-05-22 13:57:19 +04:00
~TargetPhrase();
2013-08-08 15:26:25 +04:00
// 1st evaluate method. Called during loading of phrase table.
2014-08-08 18:59:34 +04:00
void EvaluateInIsolation(const Phrase &source, const std::vector<FeatureFunction*> &ffs);
2013-05-30 15:51:40 +04:00
2013-09-25 19:57:01 +04:00
// as above, score with ALL FFs
// Used only for OOV processing. Doesn't have a phrase table connect with it
2014-08-08 18:59:34 +04:00
void EvaluateInIsolation(const Phrase &source);
2013-08-08 15:26:25 +04:00
// 'inputPath' is guaranteed to be the raw substring from the input. No factors were added or taken away
2014-08-08 18:42:23 +04:00
void EvaluateWithSourceContext(const InputType &input, const InputPath &inputPath);
2013-04-30 20:27:35 +04:00
void SetSparseScore(const FeatureFunction* translationScoreProducer, const StringPiece &sparseString);
2013-05-02 20:25:29 +04:00
// 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;
}
2013-05-29 21:16:15 +04:00
inline const ScoreComponentCollection &GetScoreBreakdown() const {
return m_scoreBreakdown;
}
inline ScoreComponentCollection &GetScoreBreakdown() {
return m_scoreBreakdown;
}
2013-05-29 21:16:15 +04:00
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;
}
void SetAlignTerm(const AlignmentInfo::CollType &coll);
void SetAlignNonTerm(const AlignmentInfo::CollType &coll);
2013-05-29 21:16:15 +04:00
const AlignmentInfo &GetAlignTerm() const {
return *m_alignTerm;
}
const AlignmentInfo &GetAlignNonTerm() const {
return *m_alignNonTerm;
}
2013-09-27 12:35:24 +04:00
const Phrase *GetRuleSource() const {
return m_ruleSource;
}
const PhraseDictionary *GetContainer() const
{ return m_container; }
// 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);
2014-06-13 18:34:24 +04:00
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();
};
2013-08-22 13:16:24 +04:00
void swap(TargetPhrase &first, TargetPhrase &second);
std::ostream& operator<<(std::ostream&, const TargetPhrase&);
/**
* Hasher that looks at source and target phrase.
**/
2013-05-29 21:16:15 +04:00
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;
}
};
2013-05-29 21:16:15 +04:00
struct TargetPhraseComparator {
inline bool operator()(const TargetPhrase& lhs, const TargetPhrase& rhs) const {
return lhs.Compare(rhs) == 0 &&
2013-05-29 21:16:15 +04:00
lhs.GetAlignTerm() == rhs.GetAlignTerm() &&
lhs.GetAlignNonTerm() == rhs.GetAlignNonTerm();
}
};
}
#endif