2015-10-24 04:27:58 +03:00
|
|
|
// -*- mode: c++; indent-tabs-mode: nil; tab-width:2 -*-
|
2010-11-17 17:06:21 +03: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
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
#ifndef moses_LanguageModelImplementation_h
|
|
|
|
#define moses_LanguageModelImplementation_h
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2012-11-12 23:56:18 +04:00
|
|
|
#include "moses/Factor.h"
|
|
|
|
#include "moses/TypeDef.h"
|
|
|
|
#include "moses/Util.h"
|
|
|
|
#include "moses/Word.h"
|
|
|
|
#include "Base.h"
|
2011-10-13 16:33:05 +04:00
|
|
|
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2010-11-17 17:06:21 +03:00
|
|
|
|
|
|
|
namespace Moses
|
|
|
|
{
|
|
|
|
|
|
|
|
class FactorCollection;
|
|
|
|
class Factor;
|
|
|
|
class Phrase;
|
|
|
|
|
2012-06-29 02:29:46 +04:00
|
|
|
//! to be returned from LM functions
|
2011-03-08 02:21:09 +03:00
|
|
|
struct LMResult {
|
|
|
|
// log probability
|
|
|
|
float score;
|
2013-05-29 21:16:15 +04:00
|
|
|
// Is the word unknown?
|
2011-03-08 02:21:09 +03:00
|
|
|
bool unknown;
|
|
|
|
};
|
|
|
|
|
2010-11-17 17:06:21 +03:00
|
|
|
//! Abstract base class which represent a language model on a contiguous phrase
|
2013-02-21 03:36:04 +04:00
|
|
|
class LanguageModelImplementation : public LanguageModel
|
2010-11-17 17:06:21 +03:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
// default constructor is ok
|
2011-10-17 13:30:30 +04:00
|
|
|
|
2011-09-21 20:06:48 +04:00
|
|
|
void ShiftOrPush(std::vector<const Word*> &contextFactor, const Word &word) const;
|
2010-11-17 17:06:21 +03:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
protected:
|
2013-05-31 23:21:02 +04:00
|
|
|
std::string m_filePath;
|
2011-02-24 16:14:42 +03:00
|
|
|
size_t m_nGramOrder; //! max n-gram length contained in this LM
|
2013-02-21 15:30:54 +04:00
|
|
|
Word m_sentenceStartWord, m_sentenceEndWord; //! Contains factors which represents the beging and end words for this LM.
|
2011-02-24 16:14:42 +03:00
|
|
|
//! Usually <s> and </s>
|
2010-11-17 17:06:21 +03:00
|
|
|
|
2013-10-29 22:59:53 +04:00
|
|
|
LanguageModelImplementation(const std::string &line);
|
2013-08-28 14:05:47 +04:00
|
|
|
|
2010-11-17 17:06:21 +03:00
|
|
|
public:
|
2013-02-21 02:44:11 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
virtual ~LanguageModelImplementation() {}
|
2010-11-17 17:06:21 +03:00
|
|
|
|
2013-08-28 14:05:47 +04:00
|
|
|
void SetParameter(const std::string& key, const std::string& value);
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
/* get score of n-gram. n-gram should not be bigger than m_nGramOrder
|
|
|
|
* Specific implementation can return State and len data to be used in hypothesis pruning
|
|
|
|
* \param contextFactor n-gram to be scored
|
|
|
|
* \param state LM state. Input and output. state must be initialized. If state isn't initialized, you want GetValueWithoutState.
|
|
|
|
*/
|
2011-03-08 02:21:09 +03:00
|
|
|
virtual LMResult GetValueGivenState(const std::vector<const Word*> &contextFactor, FFState &state) const;
|
2010-11-17 17:06:21 +03:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
// Like GetValueGivenState but state may not be initialized (however it is non-NULL).
|
|
|
|
// For example, state just came from NewState(NULL).
|
2011-03-08 02:21:09 +03:00
|
|
|
virtual LMResult GetValueForgotState(const std::vector<const Word*> &contextFactor, FFState &outState) const = 0;
|
2010-11-17 17:06:21 +03:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
//! get State for a particular n-gram. We don't care what the score is.
|
|
|
|
// This is here so models can implement a shortcut to GetValueAndState.
|
2011-08-18 01:13:21 +04:00
|
|
|
virtual void GetState(const std::vector<const Word*> &contextFactor, FFState &outState) const;
|
2010-11-17 17:06:21 +03:00
|
|
|
|
2011-08-24 14:15:29 +04:00
|
|
|
virtual const FFState *GetNullContextState() const = 0;
|
|
|
|
virtual const FFState *GetBeginSentenceState() const = 0;
|
2010-11-17 17:06:21 +03:00
|
|
|
virtual FFState *NewState(const FFState *from = NULL) const = 0;
|
|
|
|
|
2015-10-23 23:31:04 +03:00
|
|
|
virtual void CalcScore(const Phrase &phrase, float &fullScore, float &ngramScore, size_t &oovCount) const;
|
2011-09-21 20:06:48 +04:00
|
|
|
|
2014-07-10 02:41:08 +04:00
|
|
|
FFState *EvaluateWhenApplied(const Hypothesis &hypo, const FFState *ps, ScoreComponentCollection *out) const;
|
2011-10-11 17:50:44 +04:00
|
|
|
|
2014-07-10 02:54:16 +04:00
|
|
|
FFState* EvaluateWhenApplied(const ChartHypothesis& cur_hypo, int featureID, ScoreComponentCollection* accumulator) const;
|
2011-09-21 20:06:48 +04:00
|
|
|
|
2011-10-11 17:50:44 +04:00
|
|
|
void updateChartScore(float *prefixScore, float *finalScore, float score, size_t wordPos) const;
|
2011-09-21 20:06:48 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
//! max n-gram order of LM
|
|
|
|
size_t GetNGramOrder() const {
|
|
|
|
return m_nGramOrder;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Contains factors which represents the beging and end words for this LM. Usually <s> and </s>
|
2013-02-21 15:30:54 +04:00
|
|
|
const Word &GetSentenceStartWord() const {
|
|
|
|
return m_sentenceStartWord;
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2013-02-21 15:30:54 +04:00
|
|
|
const Word &GetSentenceEndWord() const {
|
|
|
|
return m_sentenceEndWord;
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
const FFState* EmptyHypothesisState(const InputType &/*input*/) const {
|
2013-02-21 03:36:04 +04:00
|
|
|
return NewState(GetBeginSentenceState());
|
|
|
|
}
|
2011-10-13 16:33:05 +04:00
|
|
|
|
2010-11-17 17:06:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|