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

Ken.h « LM « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 33590d65952e285cb0b94294c97a5c146255c9b1 (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
// $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_LanguageModelKen_h
#define moses_LanguageModelKen_h

#include <string>
#include <boost/shared_ptr.hpp>

#include "lm/word_index.hh"
#include "util/mmap.hh"

#include "moses/LM/Base.h"
#include "moses/Hypothesis.h"
#include "moses/TypeDef.h"
#include "moses/Word.h"



namespace Moses
{

//class LanguageModel;
class FFState;
class InMemoryPerSentenceOnDemandLM;

LanguageModel *ConstructKenLM(const std::string &line);

//! This will also load. Returns a templated KenLM class
LanguageModel *ConstructKenLM(const std::string &line, const std::string &file, FactorType factorType, util::LoadMethod load_method);

/*
 * An implementation of single factor LM using Kenneth's code.
 */
template <class Model> class LanguageModelKen : public LanguageModel
{
public:
  LanguageModelKen(const std::string &line, const std::string &file, FactorType factorType, util::LoadMethod load_method);

  virtual const FFState *EmptyHypothesisState(const InputType &/*input*/) const;

  virtual void CalcScore(const Phrase &phrase, float &fullScore, float &ngramScore, size_t &oovCount) const;

  virtual FFState *EvaluateWhenApplied(const Hypothesis &hypo, const FFState *ps, ScoreComponentCollection *out) const;

  virtual FFState *EvaluateWhenApplied(const ChartHypothesis& cur_hypo, int featureID, ScoreComponentCollection *accumulator) const;

  virtual FFState *EvaluateWhenApplied(const Syntax::SHyperedge& hyperedge, int featureID, ScoreComponentCollection *accumulator) const;

  virtual void IncrementalCallback(Incremental::Manager &manager) const;
  virtual void ReportHistoryOrder(std::ostream &out,const Phrase &phrase) const;

  virtual bool IsUseable(const FactorMask &mask) const;

  friend class InMemoryPerSentenceOnDemandLM;

protected:
  boost::shared_ptr<Model> m_ngram;

  const Factor *m_beginSentenceFactor;

  FactorType m_factorType;

  void LoadModel(const std::string &file, util::LoadMethod load_method);

  lm::WordIndex TranslateID(const Word &word) const {
    std::size_t factor = word.GetFactor(m_factorType)->GetId();
    return (factor >= m_lmIdLookup.size() ? 0 : m_lmIdLookup[factor]);
  }

  std::vector<lm::WordIndex> m_lmIdLookup;

private:
  LanguageModelKen();
  LanguageModelKen(const LanguageModelKen<Model> &copy_from);

  // Convert last words of hypothesis into vocab ids, returning an end pointer.
  lm::WordIndex *LastIDs(const Hypothesis &hypo, lm::WordIndex *indices) const {
    lm::WordIndex *index = indices;
    lm::WordIndex *end = indices + m_ngram->Order() - 1;
    int position = hypo.GetCurrTargetWordsRange().GetEndPos();
    for (; ; ++index, --position) {
      if (index == end) return index;
      if (position == -1) {
        *index = m_ngram->GetVocabulary().BeginSentence();
        return index + 1;
      }
      *index = TranslateID(hypo.GetWord(position));
    }
  }


protected:
  //bool m_oovFeatureEnabled; /// originally from LanguageModel, copied here to separate the interfaces. Called m_enableOOVFeature there
};

} // namespace Moses

#endif