Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LanguageModel.h « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 296a01548ddb743d7d04ac7e534df6312a79d8a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// $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
***********************************************************************/

#pragma once

#include <string>
#include <vector>
#include "Factor.h"
#include "TypeDef.h"
#include "Util.h"
#include "ScoreProducer.h"
#include "Word.h"

class FactorCollection;
class Factor;
class Phrase;

//! Abstract base class which represent a language model on a contiguous phrase
class LanguageModel : public ScoreProducer
{
protected:	
	float				m_weight; //! scoring weight. Shouldn't this now be superceded by ScoreProducer???
	std::string	m_filePath; //! for debugging purposes
	size_t			m_nGramOrder; //! max n-gram length contained in this LM
	Word m_sentenceStartArray, m_sentenceEndArray; //! Contains factors which represents the beging and end words for this LM. 
																								//! Usually <s> and </s>

	/** constructor to be called by inherited class
	 * \param registerScore whether this LM will be directly used to score sentence. 
	 * 						Usually true, except where LM is a component in a composite LM, eg. LanguageModelJoint
	 */
	LanguageModel(bool registerScore, ScoreIndexManager &scoreIndexManager);

public:
	/* Returned from LM implementations which points at the state used. For example, if a trigram score was requested
	 * but the LM backed off to using the trigram, the State pointer will point to the bigram.
	 * Used for more agressive pruning of hypothesis
	 */
  typedef const void* State;

	virtual ~LanguageModel();

	//! see ScoreProducer.h
	size_t GetNumScoreComponents() const;

	//! Single or multi-factor
	virtual LMType GetLMType() const = 0;

	/* whether this LM can be used on a particular phrase. 
	 * Should return false if phrase size = 0 or factor types required don't exists
	 */
	virtual bool Useable(const Phrase &phrase) const = 0;

	/* 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
	 */
	void CalcScore(const Phrase &phrase
							, float &fullScore
							, float &ngramScore) const;
	/* 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 finalState state used by LM. Return arg
	 * \param len ???
	 */
	virtual float GetValue(const std::vector<const Word*> &contextFactor
												, State* finalState = 0
												, unsigned int* len = 0) const = 0;
	//! get State for a particular n-gram
	State GetState(const std::vector<const Word*> &contextFactor, unsigned int* len = 0) const;

	//! 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>
	const Word &GetSentenceStartArray() const
	{
		return m_sentenceStartArray;
	}
	const Word &GetSentenceEndArray() const
	{
		return m_sentenceEndArray;
	}
	
	//! scoring weight. Shouldn't this now be superceded by ScoreProducer???
	float GetWeight() const
	{
		return m_weight;
	}
	void SetWeight(float weight)
	{
		m_weight = weight;
	}
	
	virtual std::string GetScoreProducerDescription() const = 0;
  
  //! overrideable funtions for IRST LM to cleanup. Maybe something to do with on demand/cache loading/unloading
  virtual void InitializeBeforeSentenceProcessing(){};
  virtual void CleanUpAfterSentenceProcessing() {};  
};