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
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
#include "Phrase.h"
|
|
|
|
#include "TargetPhrase.h"
|
|
|
|
#include "Dictionary.h"
|
|
|
|
#include "TargetPhraseCollection.h"
|
2010-08-10 17:12:00 +04:00
|
|
|
#include "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;
|
2010-09-23 21:39:32 +04:00
|
|
|
class ChartTranslationOptionList;
|
2011-04-05 00:43:02 +04:00
|
|
|
class ChartCellCollection;
|
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;
|
|
|
|
}
|
|
|
|
DecodeType GetDecodeType() const {
|
|
|
|
return Translate;
|
|
|
|
}
|
|
|
|
const PhraseDictionaryFeature* GetFeature() const;
|
2012-06-01 04:49:42 +04:00
|
|
|
size_t GetDictIndex() 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;
|
|
|
|
//! Create entry for translation of source to targetPhrase
|
|
|
|
virtual void InitializeForInput(InputType const& source) = 0;
|
|
|
|
|
|
|
|
//! Create a sentence-specific manager for SCFG rule lookup.
|
|
|
|
virtual ChartRuleLookupManager *CreateRuleLookupManager(
|
|
|
|
const InputType &,
|
2011-04-05 00:43:02 +04:00
|
|
|
const ChartCellCollection &) = 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:
|
|
|
|
PhraseDictionaryFeature( PhraseTableImplementation implementation
|
2011-09-20 19:32:26 +04:00
|
|
|
, SparsePhraseDictionaryFeature* spdf
|
2011-02-24 16:14:42 +03:00
|
|
|
, size_t numScoreComponent
|
2009-08-07 20:47:54 +04:00
|
|
|
, unsigned numInputScores
|
|
|
|
, const std::vector<FactorType> &input
|
|
|
|
, const std::vector<FactorType> &output
|
|
|
|
, const std::string &filePath
|
|
|
|
, const std::vector<float> &weight
|
2012-06-01 04:49:42 +04:00
|
|
|
, size_t dictIndex
|
2010-04-08 21:57:38 +04:00
|
|
|
, size_t tableLimit
|
2011-02-24 16:14:42 +03:00
|
|
|
, const std::string &targetFile
|
|
|
|
, const std::string &alignmentsFile);
|
|
|
|
|
|
|
|
|
|
|
|
virtual ~PhraseDictionaryFeature();
|
|
|
|
|
|
|
|
virtual bool ComputeValueInTranslationOption() const;
|
|
|
|
|
2011-08-30 16:25:50 +04:00
|
|
|
std::string GetScoreProducerWeightShortName(unsigned idx=0) const;
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
size_t GetNumInputScores() const;
|
|
|
|
|
2011-09-20 19:32:26 +04:00
|
|
|
SparsePhraseDictionaryFeature* GetSparsePhraseDictionaryFeature() const {
|
|
|
|
return m_sparsePhraseDictionaryFeature;
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
//Initialises the dictionary (may involve loading from file)
|
|
|
|
void InitDictionary(const TranslationSystem* system);
|
|
|
|
|
|
|
|
//Initialise the dictionary for this source (in this thread)
|
|
|
|
void InitDictionary(const TranslationSystem* system,const InputType& source);
|
|
|
|
|
|
|
|
//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
|
|
|
size_t GetDictIndex() const;
|
|
|
|
|
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-09-13 21:16:13 +04:00
|
|
|
|
2012-06-01 04:49:42 +04:00
|
|
|
protected:
|
|
|
|
size_t m_dictIndex;
|
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
|