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_PhraseDictionary_h
|
|
|
|
#define moses_PhraseDictionary_h
|
2008-06-11 14:52:57 +04:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
2009-08-11 13:37:29 +04:00
|
|
|
#include <memory>
|
2008-06-11 14:52:57 +04:00
|
|
|
#include <list>
|
2012-09-13 21:16:13 +04:00
|
|
|
#include <stdexcept>
|
2008-06-11 14:52:57 +04:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2009-08-07 20:47:54 +04:00
|
|
|
|
|
|
|
#ifdef WITH_THREADS
|
|
|
|
#include <boost/thread/tss.hpp>
|
|
|
|
#endif
|
|
|
|
|
2012-11-27 19:08:31 +04:00
|
|
|
#include "moses/Phrase.h"
|
|
|
|
#include "moses/TargetPhrase.h"
|
|
|
|
#include "moses/Dictionary.h"
|
|
|
|
#include "moses/TargetPhraseCollection.h"
|
|
|
|
#include "moses/DecodeFeature.h"
|
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 StaticData;
|
|
|
|
class InputType;
|
|
|
|
class WordsRange;
|
2012-10-11 17:27:30 +04:00
|
|
|
class ChartCellCollectionBase;
|
2010-08-10 17:12:00 +04:00
|
|
|
class TranslationSystem;
|
2011-04-13 14:38:27 +04:00
|
|
|
class ChartRuleLookupManager;
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2009-08-07 20:47:54 +04:00
|
|
|
class PhraseDictionaryFeature;
|
2011-09-20 19:32:26 +04:00
|
|
|
class SparsePhraseDictionaryFeature;
|
2011-04-13 14:38:27 +04:00
|
|
|
|
2009-08-07 20:47:54 +04:00
|
|
|
/**
|
|
|
|
* Abstract base class for phrase dictionaries (tables).
|
|
|
|
**/
|
2011-02-24 16:14:42 +03:00
|
|
|
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;
|
|
|
|
}
|
2013-02-05 22:47:25 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
const PhraseDictionaryFeature* GetFeature() const;
|
2011-08-19 20:09:36 +04:00
|
|
|
|
2011-02-24 16:14:42 +03: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;
|
2012-12-24 20:52:40 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
//! Create entry for translation of source to targetPhrase
|
2012-12-24 20:52:40 +04:00
|
|
|
virtual void InitializeForInput(InputType const& source)
|
|
|
|
{}
|
2012-12-24 19:17:13 +04:00
|
|
|
// clean up temporary memory, called after processing each sentence
|
|
|
|
virtual void CleanUpAfterSentenceProcessing(const InputType& source)
|
|
|
|
{}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
//! Create a sentence-specific manager for SCFG rule lookup.
|
|
|
|
virtual ChartRuleLookupManager *CreateRuleLookupManager(
|
|
|
|
const InputType &,
|
2012-10-11 17:27:30 +04:00
|
|
|
const ChartCellCollectionBase &) = 0;
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
size_t m_tableLimit;
|
|
|
|
const PhraseDictionaryFeature* m_feature;
|
2009-08-07 20:47:54 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a feature derived from a phrase table.
|
|
|
|
*/
|
2010-08-10 17:12:00 +04:00
|
|
|
class PhraseDictionaryFeature : public DecodeFeature
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
public:
|
2013-02-01 19:50:57 +04:00
|
|
|
PhraseDictionaryFeature(const std::string &line);
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
virtual ~PhraseDictionaryFeature();
|
|
|
|
|
|
|
|
virtual bool ComputeValueInTranslationOption() const;
|
|
|
|
|
2011-09-20 19:32:26 +04:00
|
|
|
SparsePhraseDictionaryFeature* GetSparsePhraseDictionaryFeature() const {
|
|
|
|
return m_sparsePhraseDictionaryFeature;
|
|
|
|
}
|
2013-02-01 20:59:30 +04:00
|
|
|
void SetSparsePhraseDictionaryFeature(SparsePhraseDictionaryFeature *spdf) {
|
|
|
|
m_sparsePhraseDictionaryFeature = spdf;
|
|
|
|
}
|
2011-09-20 19:32:26 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
//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;
|
2011-08-19 20:09:36 +04:00
|
|
|
PhraseDictionary* GetDictionary();
|
2012-06-01 04:49:42 +04:00
|
|
|
|
2012-09-13 21:16:13 +04:00
|
|
|
//Usual feature function methods are not implemented
|
2012-09-19 21:00:53 +04:00
|
|
|
virtual void Evaluate(const PhraseBasedFeatureContext& context,
|
2012-09-13 21:16:13 +04:00
|
|
|
ScoreComponentCollection* accumulator) const
|
|
|
|
{
|
2012-09-21 14:56:01 +04:00
|
|
|
throw std::logic_error("PhraseDictionary.Evaluate() Not implemented");
|
2012-09-13 21:16:13 +04:00
|
|
|
}
|
|
|
|
|
2012-09-21 14:56:01 +04:00
|
|
|
virtual void EvaluateChart(const ChartBasedFeatureContext& context,
|
2012-09-13 21:16:13 +04:00
|
|
|
ScoreComponentCollection* accumulator) const
|
|
|
|
{
|
2012-09-21 14:56:01 +04:00
|
|
|
throw std::logic_error("PhraseDictionary.EvaluateChart() Not implemented");
|
2012-09-13 21:16:13 +04:00
|
|
|
}
|
|
|
|
|
2012-09-21 18:00:24 +04:00
|
|
|
virtual bool ComputeValueInTranslationTable() const {return true;}
|
|
|
|
|
2012-12-24 20:52:40 +04:00
|
|
|
void InitializeForInput(const InputType& source);
|
2012-12-24 19:17:13 +04:00
|
|
|
// clean up temporary memory, called after processing each sentence
|
|
|
|
void CleanUpAfterSentenceProcessing(const InputType& source);
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
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;
|
2011-09-20 19:32:26 +04:00
|
|
|
SparsePhraseDictionaryFeature* m_sparsePhraseDictionaryFeature;
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2009-08-07 20:47:54 +04:00
|
|
|
};
|
2009-02-06 18:43:06 +03:00
|
|
|
|
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
|