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_LanguageModel_h
|
|
|
|
#define moses_LanguageModel_h
|
2008-06-11 14:52:57 +04:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include "Factor.h"
|
|
|
|
#include "TypeDef.h"
|
|
|
|
#include "Util.h"
|
2009-02-06 18:43:06 +03:00
|
|
|
#include "FeatureFunction.h"
|
2008-06-11 14:52:57 +04:00
|
|
|
#include "Word.h"
|
2010-11-17 17:06:21 +03:00
|
|
|
#include "LanguageModelImplementation.h"
|
|
|
|
|
|
|
|
#ifdef WITH_THREADS
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
#endif
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
class FactorCollection;
|
|
|
|
class Factor;
|
|
|
|
class Phrase;
|
|
|
|
|
|
|
|
//! Abstract base class which represent a language model on a contiguous phrase
|
2009-02-06 18:43:06 +03:00
|
|
|
class LanguageModel : public StatefulFeatureFunction
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
protected:
|
2010-11-17 17:06:21 +03:00
|
|
|
#ifdef WITH_THREADS
|
2011-02-24 16:14:42 +03:00
|
|
|
// if we have threads, we also have boost and can let it handle thread-safe reference counting
|
|
|
|
boost::shared_ptr<LanguageModelImplementation> m_implementation;
|
2010-11-17 17:06:21 +03:00
|
|
|
#else
|
2011-02-24 16:14:42 +03:00
|
|
|
LanguageModelImplementation *m_implementation;
|
2010-11-17 17:06:21 +03:00
|
|
|
#endif
|
2011-09-09 22:03:00 +04:00
|
|
|
bool m_enableOOVFeature;
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
void ShiftOrPush(std::vector<const Word*> &contextFactor, const Word &word) const;
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2010-11-17 17:06:21 +03:00
|
|
|
public:
|
2011-02-24 16:14:42 +03:00
|
|
|
/**
|
|
|
|
* Create a new language model
|
|
|
|
*/
|
|
|
|
LanguageModel(ScoreIndexManager &scoreIndexManager, LanguageModelImplementation *implementation);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new language model reusing an already loaded implementation
|
|
|
|
*/
|
|
|
|
LanguageModel(ScoreIndexManager &scoreIndexManager, LanguageModel *implementation);
|
|
|
|
|
|
|
|
virtual ~LanguageModel();
|
|
|
|
|
|
|
|
//! see ScoreProducer.h
|
|
|
|
size_t GetNumScoreComponents() const;
|
|
|
|
|
|
|
|
/* whether this LM can be used on a particular phrase.
|
|
|
|
* Should return false if phrase size = 0 or factor types required don't exists
|
|
|
|
*/
|
|
|
|
bool Useable(const Phrase &phrase) const {
|
|
|
|
return m_implementation->Useable(phrase);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calc total unweighted LM score of this phrase and return score via arguments.
|
|
|
|
* Return scores should always be in natural log, regardless of representation with LM implementation.
|
|
|
|
* Uses GetValue() of inherited class.
|
|
|
|
* Useable() should be called beforehand on the phrase
|
|
|
|
* \param fullScore scores of all unigram, bigram... of contiguous n-gram of the phrase
|
|
|
|
* \param ngramScore score of only n-gram of order m_nGramOrder
|
2011-09-09 22:03:00 +04:00
|
|
|
* \param oovCount number of LM OOVs
|
2011-02-24 16:14:42 +03:00
|
|
|
*/
|
|
|
|
void CalcScore(
|
|
|
|
const Phrase &phrase,
|
|
|
|
float &fullScore,
|
2011-09-09 22:03:00 +04:00
|
|
|
float &ngramScore,
|
|
|
|
size_t &oovCount) const;
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
//! max n-gram order of LM
|
|
|
|
size_t GetNGramOrder() const {
|
|
|
|
return m_implementation->GetNGramOrder();
|
|
|
|
}
|
|
|
|
|
2011-08-30 16:25:50 +04:00
|
|
|
virtual std::string GetScoreProducerDescription(unsigned idx=0) const {
|
|
|
|
return m_implementation->GetScoreProducerDescription(idx);
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
float GetWeight() const;
|
2011-09-09 22:03:00 +04:00
|
|
|
float GetOOVWeight() const;
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2011-08-30 16:25:50 +04:00
|
|
|
std::string GetScoreProducerWeightShortName(unsigned) const {
|
2011-02-24 16:14:42 +03:00
|
|
|
return "lm";
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeBeforeSentenceProcessing() {
|
|
|
|
m_implementation->InitializeBeforeSentenceProcessing();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CleanUpAfterSentenceProcessing() {
|
|
|
|
m_implementation->CleanUpAfterSentenceProcessing();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const FFState* EmptyHypothesisState(const InputType &input) const;
|
2009-02-06 18:43:06 +03:00
|
|
|
|
|
|
|
virtual FFState* Evaluate(
|
|
|
|
const Hypothesis& cur_hypo,
|
|
|
|
const FFState* prev_state,
|
|
|
|
ScoreComponentCollection* accumulator) const;
|
|
|
|
|
2011-09-21 20:06:48 +04:00
|
|
|
FFState* EvaluateChart(
|
2011-06-16 01:31:27 +04:00
|
|
|
const ChartHypothesis& cur_hypo,
|
|
|
|
int featureID,
|
2011-09-21 20:06:48 +04:00
|
|
|
ScoreComponentCollection* accumulator) const {
|
|
|
|
return m_implementation->EvaluateChart(cur_hypo, featureID, accumulator, this);
|
|
|
|
}
|
2011-06-16 01:31:27 +04:00
|
|
|
|
2011-06-09 21:27:48 +04:00
|
|
|
#ifdef WITH_THREADS
|
|
|
|
// if multi-threaded return boost ptr
|
|
|
|
boost::shared_ptr<LanguageModelImplementation>
|
|
|
|
#else // return normal LM ptr
|
|
|
|
LanguageModelImplementation*
|
|
|
|
#endif
|
|
|
|
GetLMImplementation() const {
|
|
|
|
return m_implementation;
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
};
|
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
}
|
|
|
|
|
2010-02-24 14:15:44 +03:00
|
|
|
#endif
|