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

PhraseDictionaryCache.h « TranslationModel « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 03d70fec9a0616e2f22e19e520deb81a73affd12 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/***********************************************************************
 Moses - statistical machine translation system
 Copyright (C) 2006-2011 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

#ifndef moses_PhraseDictionaryCache_H
#define moses_PhraseDictionaryCache_H

#include "moses/TypeDef.h"
#include "moses/TranslationModel/PhraseDictionary.h"
#include "moses/TranslationTask.h"

#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>

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

#define CBTM_SCORE_TYPE_UNDEFINED (-1)
#define CBTM_SCORE_TYPE_HYPERBOLA 0
#define CBTM_SCORE_TYPE_POWER 1
#define CBTM_SCORE_TYPE_EXPONENTIAL 2
#define CBTM_SCORE_TYPE_COSINE 3
#define CBTM_SCORE_TYPE_HYPERBOLA_REWARD 10
#define CBTM_SCORE_TYPE_POWER_REWARD 11
#define CBTM_SCORE_TYPE_EXPONENTIAL_REWARD 12
#define PI 3.14159265


namespace Moses
{
class ChartParser;
class ChartCellCollectionBase;
class ChartRuleLookupManager;
class TranslationTask;
class PhraseDictionary;

/** Implementation of a Cache-based phrase table.
 */
class PhraseDictionaryCache : public PhraseDictionary
{

  typedef std::pair<TargetPhraseCollection::shared_ptr, Scores*> TargetCollectionPair;
  typedef boost::unordered_map<Phrase, TargetCollectionPair> cacheMap;
  typedef std::map<long, cacheMap> sentCacheMap;

  // factored translation
  std::vector<FactorType> m_inputFactorsVec, m_outputFactorsVec;

  // data structure for the cache
  sentCacheMap m_cacheTM;
  long m_sentences;
  unsigned int m_numscorecomponent;
  size_t m_score_type; //scoring type of the match
  size_t m_entries; //total number of entries in the cache
  float m_lower_score; //lower_bound_score for no match
  bool m_constant; //flag for setting a non-decaying cache
  std::string m_initfiles; // vector of files loaded in the initialization phase
  std::string m_name; // internal name to identify this instance of the Cache-based phrase table

#ifdef WITH_THREADS
  //multiple readers - single writer lock
  mutable boost::shared_mutex m_cacheLock;
#endif

  friend std::ostream& operator<<(std::ostream&, const PhraseDictionaryCache&);

public:
  PhraseDictionaryCache(const std::string &line);
  ~PhraseDictionaryCache();

  inline const std::string GetName() {
    return m_name;
  };
  inline void SetName(const std::string name) {
    m_name = name;
  }

  static const PhraseDictionaryCache* Instance(const std::string& name) {
    if (s_instance_map.find(name) == s_instance_map.end()) {
      return NULL;
    }
    return s_instance_map[name];
  }

  static PhraseDictionaryCache* InstanceNonConst(const std::string& name) {
    if (s_instance_map.find(name) == s_instance_map.end()) {
      return NULL;
    }
    return s_instance_map[name];
  }


  static const PhraseDictionaryCache& Instance() {
    return *s_instance;
  }

  static PhraseDictionaryCache& InstanceNonConst() {
    return *s_instance;
  }

  TargetPhraseCollection::shared_ptr
    GetTargetPhraseCollectionLEGACY(ttasksptr const& ttask,
                                    Phrase const& src) const{
	  GetTargetPhraseCollection(src, ttask->GetSource()->GetTranslationId());
  }


  // for phrase-based model
  void GetTargetPhraseCollectionBatch(const InputPathList &inputPathQueue) const;

  TargetPhraseCollection::shared_ptr
  GetTargetPhraseCollection(const Phrase &src, long tID) const;

  void CleanUpAfterSentenceProcessing(const InputType& source);
  // for phrase-based model
//  virtual void GetTargetPhraseCollectionBatch(const InputPathList &inputPathQueue) const;

  // for syntax/hiero model (CKY+ decoding)
  ChartRuleLookupManager* CreateRuleLookupManager(const ChartParser&, const ChartCellCollectionBase&, std::size_t);

  void SetParameter(const std::string& key, const std::string& value);

  void InitializeForInput(ttasksptr const& ttask);

  void Print() const;             // prints the cache
  void Clear();           // clears the cache
  void Clear(long tID);		// clears cache of a sentence

  void ClearEntries(std::string &entries, long tID);
  void ClearSource(std::string &entries, long tID);
  void Insert(std::string &entries, long tID);
  void Execute(std::string command, long tID);
  void ExecuteDlt(std::map<std::string, std::string> dlt_meta, long tID);

protected:

  static PhraseDictionaryCache *s_instance;
  static std::map< const std::string, PhraseDictionaryCache * > s_instance_map;

  Scores Conv2VecFloats(std::string&);
  void Insert(std::vector<std::string> entries, long tID);

  void Update(long tID, std::vector<std::string> entries);
  void Update(long tID, std::string sourceString, std::string targetString, std::string ScoreString="", std::string waString="");
  void Update(long tID, Phrase p, TargetPhrase tp, Scores scores, std::string waString="");

  void ClearEntries(std::vector<std::string> entries, long tID);
  void ClearEntries(std::string sourceString, std::string targetString, long tID);
  void ClearEntries(Phrase p, Phrase tp, long tID);

  void ClearSource(std::vector<std::string> entries, long tID);
  void ClearSource(Phrase sp, long tID);

  void Execute(std::vector<std::string> commands, long tID);
  void Execute_Single_Command(std::string command);


  void SetPreComputedScores(const unsigned int numScoreComponent);
  Scores GetPreComputedScores(const unsigned int age);

  TargetPhrase *CreateTargetPhrase(const Phrase &sourcePhrase) const;
};

}  // namespace Moses

#endif /* moses_PhraseDictionaryCache_H_ */