mosesdecoder/moses/TranslationModel/PhraseDictionary.h

171 lines
5.4 KiB
C
Raw Normal View History

// $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
***********************************************************************/
#ifndef moses_PhraseDictionary_h
#define moses_PhraseDictionary_h
#include <iostream>
#include <map>
#include <memory>
#include <list>
#include <stdexcept>
#include <vector>
#include <string>
#ifdef WITH_THREADS
#include <boost/thread/tss.hpp>
#endif
#include "moses/Phrase.h"
#include "moses/TargetPhrase.h"
#include "moses/Dictionary.h"
#include "moses/TargetPhraseCollection.h"
#include "moses/DecodeFeature.h"
namespace Moses
{
class StaticData;
class InputType;
class WordsRange;
class ChartCellCollectionBase;
class TranslationSystem;
class ChartRuleLookupManager;
class PhraseDictionaryFeature;
class SparsePhraseDictionaryFeature;
/**
* Abstract base class for phrase dictionaries (tables).
**/
class PhraseDictionary: public Dictionary
{
public:
PhraseDictionary(size_t numScoreComponent, const PhraseDictionaryFeature* feature):
Dictionary(numScoreComponent), m_tableLimit(0), m_feature(feature) {}
//! table limit number.
size_t GetTableLimit() const {
return m_tableLimit;
}
const PhraseDictionaryFeature* GetFeature() const;
Merge commit 'a0b6abdfd3599e7fbdc6aac76fcd2cb4483d63ce' into miramerge Conflicts: moses/src/ConfusionNet.cpp moses/src/DecodeFeature.cpp moses/src/DecodeFeature.h moses/src/DotChartOnDisk.cpp moses/src/DummyScoreProducers.cpp moses/src/DummyScoreProducers.h moses/src/DynSAInclude/vocab.h moses/src/FeatureFunction.h moses/src/GenerationDictionary.cpp moses/src/GenerationDictionary.h moses/src/GlobalLexicalModel.cpp moses/src/GlobalLexicalModel.h moses/src/LMList.cpp moses/src/LMList.h moses/src/LanguageModel.cpp moses/src/LanguageModel.h moses/src/LanguageModelFactory.cpp moses/src/LanguageModelFactory.h moses/src/LanguageModelImplementation.h moses/src/LanguageModelKen.h moses/src/LanguageModelMultiFactor.cpp moses/src/LanguageModelMultiFactor.h moses/src/LanguageModelParallelBackoff.h moses/src/LanguageModelRemote.h moses/src/LanguageModelSingleFactor.cpp moses/src/LanguageModelSingleFactor.h moses/src/LexicalReordering.cpp moses/src/LexicalReordering.h moses/src/LexicalReorderingState.cpp moses/src/LexicalReorderingState.h moses/src/Manager.cpp moses/src/PDTAimp.h moses/src/Parameter.cpp moses/src/Parameter.h moses/src/Phrase.h moses/src/PhraseDictionary.cpp moses/src/PhraseDictionary.h moses/src/PhraseDictionaryMemory.cpp moses/src/ScoreComponentCollection.cpp moses/src/ScoreComponentCollection.h moses/src/ScoreIndexManager.cpp moses/src/ScoreIndexManager.h moses/src/ScoreProducer.h moses/src/StaticData.cpp moses/src/StaticData.h moses/src/TargetPhrase.cpp moses/src/TargetPhrase.h moses/src/TranslationOption.cpp moses/src/TranslationOptionCollection.cpp moses/src/TranslationSystem.cpp moses/src/TranslationSystem.h moses/src/TrellisPath.h
2011-08-19 20:09:36 +04:00
//! find list of translations that can translates src. Only for phrase input
virtual const TargetPhraseCollection *GetTargetPhraseCollection(const Phrase& src) const=0;
//! find list of translations that can translates a portion of src. Used by confusion network decoding
virtual const TargetPhraseCollection *GetTargetPhraseCollection(InputType const& src,WordsRange const& range) const;
//! Create entry for translation of source to targetPhrase
virtual void InitializeForInput(InputType const& source)
{}
// clean up temporary memory, called after processing each sentence
virtual void CleanUpAfterSentenceProcessing(const InputType& source)
{}
//! Create a sentence-specific manager for SCFG rule lookup.
virtual ChartRuleLookupManager *CreateRuleLookupManager(
const InputType &,
const ChartCellCollectionBase &) = 0;
protected:
size_t m_tableLimit;
const PhraseDictionaryFeature* m_feature;
};
/**
* Represents a feature derived from a phrase table.
*/
class PhraseDictionaryFeature : public DecodeFeature
{
public:
2013-02-01 19:50:57 +04:00
PhraseDictionaryFeature(const std::string &line);
virtual ~PhraseDictionaryFeature();
virtual bool ComputeValueInTranslationOption() const;
SparsePhraseDictionaryFeature* GetSparsePhraseDictionaryFeature() const {
return m_sparsePhraseDictionaryFeature;
}
void SetSparsePhraseDictionaryFeature(SparsePhraseDictionaryFeature *spdf) {
m_sparsePhraseDictionaryFeature = spdf;
}
//Initialises the dictionary (may involve loading from file)
void InitDictionary(const TranslationSystem* system);
//Get the dictionary. Be sure to initialise it first.
const PhraseDictionary* GetDictionary() const;
Merge commit 'a0b6abdfd3599e7fbdc6aac76fcd2cb4483d63ce' into miramerge Conflicts: moses/src/ConfusionNet.cpp moses/src/DecodeFeature.cpp moses/src/DecodeFeature.h moses/src/DotChartOnDisk.cpp moses/src/DummyScoreProducers.cpp moses/src/DummyScoreProducers.h moses/src/DynSAInclude/vocab.h moses/src/FeatureFunction.h moses/src/GenerationDictionary.cpp moses/src/GenerationDictionary.h moses/src/GlobalLexicalModel.cpp moses/src/GlobalLexicalModel.h moses/src/LMList.cpp moses/src/LMList.h moses/src/LanguageModel.cpp moses/src/LanguageModel.h moses/src/LanguageModelFactory.cpp moses/src/LanguageModelFactory.h moses/src/LanguageModelImplementation.h moses/src/LanguageModelKen.h moses/src/LanguageModelMultiFactor.cpp moses/src/LanguageModelMultiFactor.h moses/src/LanguageModelParallelBackoff.h moses/src/LanguageModelRemote.h moses/src/LanguageModelSingleFactor.cpp moses/src/LanguageModelSingleFactor.h moses/src/LexicalReordering.cpp moses/src/LexicalReordering.h moses/src/LexicalReorderingState.cpp moses/src/LexicalReorderingState.h moses/src/Manager.cpp moses/src/PDTAimp.h moses/src/Parameter.cpp moses/src/Parameter.h moses/src/Phrase.h moses/src/PhraseDictionary.cpp moses/src/PhraseDictionary.h moses/src/PhraseDictionaryMemory.cpp moses/src/ScoreComponentCollection.cpp moses/src/ScoreComponentCollection.h moses/src/ScoreIndexManager.cpp moses/src/ScoreIndexManager.h moses/src/ScoreProducer.h moses/src/StaticData.cpp moses/src/StaticData.h moses/src/TargetPhrase.cpp moses/src/TargetPhrase.h moses/src/TranslationOption.cpp moses/src/TranslationOptionCollection.cpp moses/src/TranslationSystem.cpp moses/src/TranslationSystem.h moses/src/TrellisPath.h
2011-08-19 20:09:36 +04:00
PhraseDictionary* GetDictionary();
//Usual feature function methods are not implemented
virtual void Evaluate(const PhraseBasedFeatureContext& context,
ScoreComponentCollection* accumulator) const
{
2012-09-21 14:56:01 +04:00
throw std::logic_error("PhraseDictionary.Evaluate() Not implemented");
}
2012-09-21 14:56:01 +04:00
virtual void EvaluateChart(const ChartBasedFeatureContext& context,
ScoreComponentCollection* accumulator) const
{
2012-09-21 14:56:01 +04:00
throw std::logic_error("PhraseDictionary.EvaluateChart() Not implemented");
}
2012-09-21 18:00:24 +04:00
virtual bool ComputeValueInTranslationTable() const {return true;}
void InitializeForInput(const InputType& source);
// clean up temporary memory, called after processing each sentence
void CleanUpAfterSentenceProcessing(const InputType& source);
private:
/** Load the appropriate phrase table */
PhraseDictionary* LoadPhraseTable(const TranslationSystem* system);
unsigned m_numInputScores;
std::string m_filePath;
size_t m_tableLimit;
//We instantiate either the the thread-safe or non-thread-safe dictionary,
//but not both. The thread-safe one can be instantiated in the constructor and shared
//between threads, however the non-thread-safe one (eg PhraseDictionaryTree) must be instantiated
//on demand, and stored in thread-specific storage.
std::auto_ptr<PhraseDictionary> m_threadSafePhraseDictionary;
#ifdef WITH_THREADS
boost::thread_specific_ptr<PhraseDictionary> m_threadUnsafePhraseDictionary;
#else
std::auto_ptr<PhraseDictionary> m_threadUnsafePhraseDictionary;
#endif
bool m_useThreadSafePhraseDictionary;
PhraseTableImplementation m_implementation;
std::string m_targetFile;
std::string m_alignmentsFile;
SparsePhraseDictionaryFeature* m_sparsePhraseDictionaryFeature;
};
Feature function overhaul. Each feature function is computed in one of three ways: 1) Stateless feature functions from the phrase table/generation table: these are computed when the TranslationOption is created. They become part of the ScoreBreakdown object contained in the TranslationOption and are added to the feature value vector when a hypothesis is extended. 2) Stateless feature functions that are computed during state exploration. Currently, only WordPenalty falls into this category, but these functions implement a method Evaluate which do does not receive a Hypothesis or any contextual information. 3) Stateful feature functions: these features receive the arc information (translation option), compute some value and then return some context information. The context information created by a particular feature function is passed back to it as the previous context when a hypothesis originating at the node where the previous edge terminates is created. States in the search space may be recombined if the context information is identical. The context information must be stored in an object implementing the FFState interface. TODO: 1) the command line interface / MERT interface needs to go to named parameters that are otherwise opaque 2) StatefulFeatureFunction's Evaluate method should just take a TranslationOption and a context object. It is not good that it takes a hypothesis, because then people may be tempted to access information about the "previous" hypothesis without "declaring" this dependency. 3) Future cost estimates should be handled using feature functions. All stateful feature functions need some kind of future cost estimate. 4) Philipp's poor-man's cube pruning is broken. git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@2087 1f5c12ca-751b-0410-a591-d2e778427230
2009-02-06 18:43:06 +03:00
}
#endif