2015-10-28 13:26:41 +03:00
|
|
|
// -*- mode: c++; indent-tabs-mode: nil; tab-width:2 -*-
|
2008-06-11 14:52:57 +04:00
|
|
|
/***********************************************************************
|
|
|
|
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
|
|
|
|
***********************************************************************/
|
|
|
|
|
2010-02-24 14:15:44 +03:00
|
|
|
#ifndef moses_TargetPhrase_h
|
|
|
|
#define moses_TargetPhrase_h
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2013-08-22 13:16:24 +04:00
|
|
|
#include <algorithm>
|
2008-06-11 14:52:57 +04:00
|
|
|
#include <vector>
|
2008-09-12 22:09:06 +04:00
|
|
|
#include "TypeDef.h"
|
2008-06-11 14:52:57 +04:00
|
|
|
#include "Phrase.h"
|
|
|
|
#include "ScoreComponentCollection.h"
|
2010-04-08 21:16:10 +04:00
|
|
|
#include "AlignmentInfo.h"
|
2014-10-07 21:08:31 +04:00
|
|
|
#include "AlignmentInfoCollection.h"
|
2014-05-20 00:54:08 +04:00
|
|
|
#include "moses/PP/PhraseProperty.h"
|
2011-10-14 20:40:30 +04:00
|
|
|
#include "util/string_piece.hh"
|
2015-06-25 18:36:18 +03:00
|
|
|
//#include "moses/TranslationTask.h"
|
2011-10-14 20:40:30 +04:00
|
|
|
|
2014-05-20 00:54:08 +04:00
|
|
|
#include <boost/shared_ptr.hpp>
|
2015-02-20 23:53:25 +03:00
|
|
|
#include <boost/unordered_map.hpp>
|
2014-05-20 00:54:08 +04:00
|
|
|
|
2008-09-24 20:48:23 +04:00
|
|
|
#ifdef HAVE_PROTOBUF
|
|
|
|
#include "rule.pb.h"
|
|
|
|
#endif
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
2013-02-22 00:03:35 +04:00
|
|
|
class FeatureFunction;
|
2013-08-13 23:36:32 +04:00
|
|
|
class InputPath;
|
2014-08-04 18:14:40 +04:00
|
|
|
class InputPath;
|
|
|
|
class PhraseDictionary;
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2008-09-12 22:09:06 +04:00
|
|
|
/** represents an entry on the target side of a phrase table (scores, translation, alignment)
|
2008-06-11 14:52:57 +04:00
|
|
|
*/
|
|
|
|
class TargetPhrase: public Phrase
|
|
|
|
{
|
2015-05-02 13:45:24 +03:00
|
|
|
public:
|
2015-06-29 10:30:26 +03:00
|
|
|
typedef std::map<FeatureFunction const*, boost::shared_ptr<Scores> > ScoreCache_t;
|
2015-03-09 03:30:01 +03:00
|
|
|
ScoreCache_t const& GetExtraScores() const;
|
|
|
|
Scores const* GetExtraScores(FeatureFunction const* ff) const;
|
2015-06-29 10:30:26 +03:00
|
|
|
void SetExtraScores(FeatureFunction const* ff,boost::shared_ptr<Scores> const& scores);
|
|
|
|
|
2015-04-30 08:05:11 +03:00
|
|
|
|
2015-05-02 13:45:24 +03:00
|
|
|
private:
|
2015-04-30 08:05:11 +03:00
|
|
|
ScoreCache_t m_cached_scores;
|
2015-10-28 13:26:41 +03:00
|
|
|
WPTR<ContextScope> m_scope;
|
2015-04-30 08:05:11 +03:00
|
|
|
|
2013-08-22 13:16:24 +04:00
|
|
|
private:
|
2011-02-24 16:14:42 +03:00
|
|
|
friend std::ostream& operator<<(std::ostream&, const TargetPhrase&);
|
2013-08-22 13:16:24 +04:00
|
|
|
friend void swap(TargetPhrase &first, TargetPhrase &second);
|
|
|
|
|
2015-11-03 08:55:26 +03:00
|
|
|
float m_futureScore, m_estimatedScore;
|
2011-02-24 16:14:42 +03:00
|
|
|
ScoreComponentCollection m_scoreBreakdown;
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
const AlignmentInfo* m_alignTerm, *m_alignNonTerm;
|
|
|
|
const Word *m_lhsTarget;
|
2013-09-17 20:22:00 +04:00
|
|
|
mutable Phrase *m_ruleSource; // to be set by the feature function that needs it.
|
2008-09-12 22:09:06 +04:00
|
|
|
|
2014-06-10 13:29:49 +04:00
|
|
|
typedef std::map<std::string, boost::shared_ptr<PhraseProperty> > Properties;
|
|
|
|
Properties m_properties;
|
2014-05-20 00:54:08 +04:00
|
|
|
|
2014-08-04 18:14:40 +04:00
|
|
|
const PhraseDictionary *m_container;
|
2014-05-20 00:54:08 +04:00
|
|
|
|
2015-02-20 23:53:25 +03:00
|
|
|
mutable boost::unordered_map<const std::string, boost::shared_ptr<void> > m_data;
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
public:
|
2015-06-25 17:47:39 +03:00
|
|
|
TargetPhrase(const PhraseDictionary *pt = NULL);
|
|
|
|
TargetPhrase(std::string out_string, const PhraseDictionary *pt = NULL);
|
2013-05-22 13:40:12 +04:00
|
|
|
TargetPhrase(const TargetPhrase ©);
|
2015-06-25 17:47:39 +03:00
|
|
|
explicit TargetPhrase(const Phrase &targetPhrase, const PhraseDictionary *pt);
|
2015-06-25 18:36:18 +03:00
|
|
|
|
|
|
|
/*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);
|
2015-10-28 13:26:41 +03:00
|
|
|
|
|
|
|
// ttasksptr GetTtask() const;
|
|
|
|
// bool HasTtaskSPtr() const;
|
|
|
|
|
|
|
|
bool HasScope() const;
|
|
|
|
SPTR<ContextScope> GetScope() const;
|
2015-06-25 18:36:18 +03:00
|
|
|
|
2013-05-22 13:57:19 +04:00
|
|
|
~TargetPhrase();
|
2008-06-11 14:52:57 +04:00
|
|
|
|
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
|
|
|
|
2013-08-13 23:36:32 +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-05-02 15:15:26 +04:00
|
|
|
|
2015-01-06 16:54:40 +03:00
|
|
|
void UpdateScore(ScoreComponentCollection *futureScoreBreakdown = NULL);
|
2015-01-14 14:07:42 +03:00
|
|
|
|
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
|
2013-05-02 20:44:19 +04:00
|
|
|
void SetXMLScore(float score);
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2008-09-24 20:48:23 +04:00
|
|
|
#ifdef HAVE_PROTOBUF
|
2011-02-24 16:14:42 +03:00
|
|
|
void WriteToRulePB(hgmert::Rule* pb) const;
|
2008-09-24 20:48:23 +04:00
|
|
|
#endif
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
/***
|
|
|
|
* 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)
|
2011-02-24 16:14:42 +03:00
|
|
|
*
|
2008-06-11 14:52:57 +04:00
|
|
|
*/
|
2011-02-24 16:14:42 +03:00
|
|
|
inline float GetFutureScore() const {
|
2015-11-03 08:55:26 +03:00
|
|
|
return m_futureScore;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
2013-05-01 21:43:59 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
inline const ScoreComponentCollection &GetScoreBreakdown() const {
|
|
|
|
return m_scoreBreakdown;
|
|
|
|
}
|
|
|
|
inline ScoreComponentCollection &GetScoreBreakdown() {
|
|
|
|
return m_scoreBreakdown;
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2014-01-15 19:42:02 +04:00
|
|
|
/*
|
|
|
|
//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;
|
|
|
|
}
|
|
|
|
*/
|
2013-05-29 21:16:15 +04:00
|
|
|
void SetTargetLHS(const Word *lhs) {
|
|
|
|
m_lhsTarget = lhs;
|
|
|
|
}
|
|
|
|
const Word &GetTargetLHS() const {
|
|
|
|
return *m_lhsTarget;
|
|
|
|
}
|
|
|
|
|
2011-10-14 20:40:30 +04:00
|
|
|
void SetAlignmentInfo(const StringPiece &alignString);
|
2012-10-19 18:10:10 +04:00
|
|
|
void SetAlignTerm(const AlignmentInfo *alignTerm) {
|
|
|
|
m_alignTerm = alignTerm;
|
2011-09-27 04:34:46 +04:00
|
|
|
}
|
2012-10-19 18:10:10 +04:00
|
|
|
void SetAlignNonTerm(const AlignmentInfo *alignNonTerm) {
|
|
|
|
m_alignNonTerm = alignNonTerm;
|
|
|
|
}
|
|
|
|
|
2015-01-14 14:07:42 +03:00
|
|
|
// ALNREP = alignment representation,
|
2014-10-07 21:08:31 +04:00
|
|
|
// see AlignmentInfo constructors for supported representations
|
|
|
|
template<typename ALNREP>
|
2015-01-14 14:07:42 +03:00
|
|
|
void
|
|
|
|
SetAlignTerm(const ALNREP &coll) {
|
2014-10-07 21:08:31 +04:00
|
|
|
m_alignTerm = AlignmentInfoCollection::Instance().Add(coll);
|
|
|
|
}
|
|
|
|
|
2015-01-14 14:07:42 +03:00
|
|
|
// ALNREP = alignment representation,
|
2014-10-07 21:08:31 +04:00
|
|
|
// see AlignmentInfo constructors for supported representations
|
2015-01-14 14:07:42 +03:00
|
|
|
template<typename ALNREP>
|
|
|
|
void
|
|
|
|
SetAlignNonTerm(const ALNREP &coll) {
|
2014-10-07 21:08:31 +04:00
|
|
|
m_alignNonTerm = AlignmentInfoCollection::Instance().Add(coll);
|
|
|
|
}
|
|
|
|
|
2012-10-19 18:10:10 +04:00
|
|
|
|
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;
|
|
|
|
}
|
2013-09-17 20:22:00 +04:00
|
|
|
|
2015-01-14 14:07:42 +03:00
|
|
|
const PhraseDictionary *GetContainer() const {
|
|
|
|
return m_container;
|
|
|
|
}
|
2014-08-05 13:26:42 +04:00
|
|
|
|
2015-02-20 23:53:25 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-04-30 08:05:11 +03:00
|
|
|
|
2015-03-09 03:30:01 +03:00
|
|
|
|
2013-09-17 20:22:00 +04:00
|
|
|
// 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;
|
|
|
|
|
2013-08-29 16:36:01 +04:00
|
|
|
void SetProperties(const StringPiece &str);
|
2014-05-20 00:54:08 +04:00
|
|
|
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;
|
2013-08-27 12:42:24 +04:00
|
|
|
|
2013-05-31 00:12:36 +04:00
|
|
|
void Merge(const TargetPhrase ©, const std::vector<FactorType>& factorVec);
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2013-12-13 21:23:39 +04:00
|
|
|
bool operator< (const TargetPhrase &compare) const; // NOT IMPLEMENTED
|
|
|
|
bool operator== (const TargetPhrase &compare) const; // NOT IMPLEMENTED
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
TO_STRING();
|
2008-06-11 14:52:57 +04:00
|
|
|
};
|
|
|
|
|
2013-08-22 13:16:24 +04:00
|
|
|
void swap(TargetPhrase &first, TargetPhrase &second);
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
std::ostream& operator<<(std::ostream&, const TargetPhrase&);
|
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
}
|
|
|
|
|
2010-02-24 14:15:44 +03:00
|
|
|
#endif
|