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

PhraseDictionary.h « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e8f478079c345f53bf25e769ef414fe661a43171 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// $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 <vector>
#include <string>

#ifdef WITH_THREADS
#include <boost/thread/tss.hpp>
#endif

#include "Phrase.h"
#include "TargetPhrase.h"
#include "Dictionary.h"
#include "TargetPhraseCollection.h"
#include "DecodeFeature.h"

namespace Moses
{

class StaticData;
class InputType;
class WordsRange;
class ChartTranslationOptionList;
class ChartCellCollection;
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;
  }
  DecodeType GetDecodeType() const    {
    return Translate;
  }
  const PhraseDictionaryFeature* GetFeature() const;


  //! 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 &,
    const ChartCellCollection &) = 0;

protected:
  size_t m_tableLimit;
  const PhraseDictionaryFeature* m_feature;
};


/**
 * Represents a feature derived from a phrase table.
 */
class PhraseDictionaryFeature :  public DecodeFeature
{


public:
  PhraseDictionaryFeature(  PhraseTableImplementation implementation
                            , SparsePhraseDictionaryFeature* spdf
                            , size_t numScoreComponent
                            , unsigned numInputScores
                            , const std::vector<FactorType> &input
                            , const std::vector<FactorType> &output
                            , const std::string &filePath
                            , const std::vector<float> &weight
                            , size_t tableLimit
                            , const std::string &targetFile
                            , const std::string &alignmentsFile);


  virtual ~PhraseDictionaryFeature();

  virtual bool ComputeValueInTranslationOption() const;

  std::string GetScoreProducerWeightShortName(unsigned idx=0) const;

  size_t GetNumInputScores() const;

  SparsePhraseDictionaryFeature* GetSparsePhraseDictionaryFeature() const {
    return m_sparsePhraseDictionaryFeature;
  }

  //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;
  PhraseDictionary* GetDictionary();

private:
  /** Load the appropriate phrase table */
  PhraseDictionary* LoadPhraseTable(const TranslationSystem* system);

  unsigned m_numInputScores;
  std::string m_filePath;
  std::vector<float> m_weight;
  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;

};



}
#endif