2015-02-06 02:34:34 +03:00
|
|
|
// -*- c++ -*-
|
2008-06-11 14:52:57 +04:00
|
|
|
// $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
|
|
|
|
***********************************************************************/
|
|
|
|
|
2010-02-24 14:15:44 +03:00
|
|
|
#ifndef moses_TranslationOption_h
|
|
|
|
#define moses_TranslationOption_h
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2010-02-20 12:55:02 +03:00
|
|
|
#include <map>
|
2008-06-11 14:52:57 +04:00
|
|
|
#include <vector>
|
2012-09-10 17:57:45 +04:00
|
|
|
#include <boost/functional/hash.hpp>
|
2015-10-25 16:07:25 +03:00
|
|
|
#include "Bitmap.h"
|
2015-10-25 16:37:59 +03:00
|
|
|
#include "Range.h"
|
2008-06-11 14:52:57 +04:00
|
|
|
#include "Phrase.h"
|
|
|
|
#include "TargetPhrase.h"
|
|
|
|
#include "Hypothesis.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "TypeDef.h"
|
|
|
|
#include "ScoreComponentCollection.h"
|
|
|
|
#include "StaticData.h"
|
2008-10-09 03:51:26 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
class PhraseDictionary;
|
|
|
|
class GenerationDictionary;
|
2013-05-08 15:34:56 +04:00
|
|
|
class FeatureFunction;
|
2013-05-08 15:37:16 +04:00
|
|
|
class LexicalReordering;
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
|
|
|
|
/** Available phrase translation for a particular sentence pair.
|
2011-02-24 16:14:42 +03:00
|
|
|
* In a multi-factor model, this is expanded from the entries in the
|
2008-06-11 14:52:57 +04:00
|
|
|
* translation tables and generation tables (and pruned to the maximum
|
|
|
|
* number allowed). By pre-computing the allowable phrase translations,
|
|
|
|
* efficient beam search in Manager is possible when expanding instances
|
|
|
|
* of the class Hypothesis - the states in the search.
|
2011-02-24 16:14:42 +03:00
|
|
|
*
|
2008-06-11 14:52:57 +04:00
|
|
|
* A translation option contains source and target phrase, aggregate
|
2011-02-24 16:14:42 +03:00
|
|
|
* and details scores (in m_scoreBreakdown), including an estimate
|
|
|
|
* how expensive this option will be in search (used to build the
|
2008-06-11 14:52:57 +04:00
|
|
|
* future cost matrix.)
|
|
|
|
*
|
|
|
|
* m_targetPhrase points to a phrase-table entry.
|
|
|
|
* The source word range is zero-indexed, so it can't refer to an empty range. The target phrase may be empty.
|
|
|
|
*/
|
|
|
|
class TranslationOption
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
friend std::ostream& operator<<(std::ostream& out, const TranslationOption& possibleTranslation);
|
2008-06-11 14:52:57 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2013-05-23 21:12:08 +04:00
|
|
|
TargetPhrase m_targetPhrase; /*< output phrase when using this translation option */
|
2013-08-13 23:36:32 +04:00
|
|
|
const InputPath *m_inputPath;
|
2015-10-25 16:37:59 +03:00
|
|
|
const Range m_sourceWordsRange; /*< word position in the input that are covered by this translation option */
|
2013-05-23 21:12:08 +04:00
|
|
|
float m_futureScore; /*< estimate of total cost when using this translation option, includes language model probabilities */
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2015-03-09 03:30:01 +03:00
|
|
|
// typedef std::map<const LexicalReordering*, Scores> _ScoreCacheMap;
|
2015-04-30 08:05:11 +03:00
|
|
|
// _ScoreCacheMap m_lexReorderingScores;
|
|
|
|
// m_lexReorderingScores was moved to TargetPhrase.h so that phrase tables
|
2015-03-09 03:30:01 +03:00
|
|
|
// can add information (such as lexical reordering scores) to target phrases
|
|
|
|
// during lookup.
|
2008-06-11 14:52:57 +04:00
|
|
|
|
|
|
|
public:
|
2015-02-19 15:27:23 +03:00
|
|
|
struct Better {
|
|
|
|
bool operator()(TranslationOption const& a, TranslationOption const& b) const {
|
|
|
|
return a.GetFutureScore() > b.GetFutureScore();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(TranslationOption const* a, TranslationOption const* b) const {
|
|
|
|
return a->GetFutureScore() > b->GetFutureScore();
|
|
|
|
}
|
2015-02-06 02:34:34 +03:00
|
|
|
};
|
2015-02-19 15:27:23 +03:00
|
|
|
|
2015-02-06 02:34:34 +03:00
|
|
|
|
2013-08-07 13:28:28 +04:00
|
|
|
explicit TranslationOption(); // For initial hypo that does translate nothing
|
2013-08-05 19:53:15 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
/** constructor. Used by initial translation step */
|
2015-10-25 16:37:59 +03:00
|
|
|
TranslationOption(const Range &range
|
2013-05-08 14:28:31 +04:00
|
|
|
, const TargetPhrase &targetPhrase);
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
/** returns true if all feature types in featuresToCheck are compatible between the two phrases */
|
|
|
|
bool IsCompatible(const Phrase& phrase, const std::vector<FactorType>& featuresToCheck) const;
|
|
|
|
|
|
|
|
/** returns target phrase */
|
|
|
|
inline const TargetPhrase &GetTargetPhrase() const {
|
|
|
|
return m_targetPhrase;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** returns source word range */
|
2015-10-25 16:37:59 +03:00
|
|
|
inline const Range &GetSourceWordsRange() const {
|
2011-02-24 16:14:42 +03:00
|
|
|
return m_sourceWordsRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** returns source phrase */
|
2013-08-13 23:36:32 +04:00
|
|
|
const InputPath &GetInputPath() const;
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2013-08-13 23:36:32 +04:00
|
|
|
void SetInputPath(const InputPath &inputPath);
|
2013-08-06 20:09:53 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
/** whether source span overlaps with those of a hypothesis */
|
|
|
|
bool Overlap(const Hypothesis &hypothesis) const;
|
|
|
|
|
|
|
|
/** return start index of source phrase */
|
|
|
|
inline size_t GetStartPos() const {
|
|
|
|
return m_sourceWordsRange.GetStartPos();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** return end index of source phrase */
|
|
|
|
inline size_t GetEndPos() const {
|
|
|
|
return m_sourceWordsRange.GetEndPos();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** return length of source phrase */
|
|
|
|
inline size_t GetSize() const {
|
|
|
|
return m_sourceWordsRange.GetEndPos() - m_sourceWordsRange.GetStartPos() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** return estimate of total cost of this option */
|
|
|
|
inline float GetFutureScore() const {
|
|
|
|
return m_futureScore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** return true if the source phrase translates into nothing */
|
|
|
|
inline bool IsDeletionOption() const {
|
|
|
|
return m_targetPhrase.GetSize() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** returns detailed component scores */
|
|
|
|
inline const ScoreComponentCollection &GetScoreBreakdown() const {
|
2013-05-08 15:00:01 +04:00
|
|
|
return m_targetPhrase.GetScoreBreakdown();
|
2013-07-11 19:19:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ScoreComponentCollection &GetScoreBreakdown() {
|
|
|
|
return m_targetPhrase.GetScoreBreakdown();
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:42:23 +04:00
|
|
|
void EvaluateWithSourceContext(const InputType &input);
|
2013-05-23 21:12:08 +04:00
|
|
|
|
2015-01-06 16:54:40 +03:00
|
|
|
void UpdateScore(ScoreComponentCollection *futureScoreBreakdown = NULL) {
|
|
|
|
m_targetPhrase.UpdateScore(futureScoreBreakdown);
|
|
|
|
}
|
2015-01-14 14:07:42 +03:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
/** returns cached scores */
|
2015-04-30 08:05:11 +03:00
|
|
|
// inline
|
2015-03-09 03:30:01 +03:00
|
|
|
const Scores*
|
|
|
|
GetLexReorderingScores(const LexicalReordering *scoreProducer) const;
|
|
|
|
// {
|
|
|
|
// return m_targetPhrase.GetExtraScores(scoreProducer);
|
|
|
|
// }
|
|
|
|
|
2015-04-30 08:05:11 +03:00
|
|
|
void CacheLexReorderingScores(const LexicalReordering &scoreProducer,
|
2015-05-02 13:45:24 +03:00
|
|
|
const Scores &score);
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
TO_STRING();
|
2013-05-29 21:16:15 +04:00
|
|
|
|
|
|
|
bool operator== (const TranslationOption &rhs) const {
|
2012-09-10 17:57:45 +04:00
|
|
|
return m_sourceWordsRange == rhs.m_sourceWordsRange &&
|
2013-05-29 21:16:15 +04:00
|
|
|
m_targetPhrase == rhs.m_targetPhrase;
|
|
|
|
}
|
2012-09-10 17:57:45 +04:00
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
};
|
|
|
|
|
2012-09-10 17:57:45 +04:00
|
|
|
|
2012-09-21 18:00:24 +04:00
|
|
|
//XXX: This doesn't look at the alignment. Is this correct?
|
2013-05-29 21:16:15 +04:00
|
|
|
inline size_t hash_value(const TranslationOption& translationOption)
|
|
|
|
{
|
2012-09-10 17:57:45 +04:00
|
|
|
size_t seed = 0;
|
|
|
|
boost::hash_combine(seed, translationOption.GetTargetPhrase());
|
|
|
|
boost::hash_combine(seed, translationOption.GetStartPos());
|
|
|
|
boost::hash_combine(seed, translationOption.GetEndPos());
|
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
}
|
|
|
|
|
2010-02-24 14:15:44 +03:00
|
|
|
#endif
|
2008-06-11 14:52:57 +04:00
|
|
|
|
|
|
|
|