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

Joint.h « LM « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e000409be710ecebce8137a5fd1de5fb3ebec8cc (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
// $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_LanguageModelJoint_h
#define moses_LanguageModelJoint_h

#include <vector>
#include <string>
#include <sstream>
#include "LM/SingleFactor.h"
#include "LM/MultiFactor.h"
#include "Word.h"
#include "FactorTypeSet.h"
#include "FactorCollection.h"

namespace Moses
{

class Phrase;
class FactorCollection;

/** LM of multiple factors. A simple extension of single factor LM - factors backoff together.
 *	Rather slow as this uses string concatenation/split
*/
class LanguageModelJoint : public LanguageModelMultiFactor
{
protected:
  LanguageModelSingleFactor *m_lmImpl;
  std::vector<FactorType> m_factorTypesOrdered;

  size_t m_implFactor;
public:
  LanguageModelJoint(LanguageModelSingleFactor *lmImpl) {
    m_lmImpl = lmImpl;
  }

  ~LanguageModelJoint() {
    delete m_lmImpl;
  }

  bool Load(const std::string &filePath
            , const std::vector<FactorType> &factorTypes
            , size_t nGramOrder) {
    m_factorTypes				= FactorMask(factorTypes);
    m_filePath 					= filePath;
    m_nGramOrder 				= nGramOrder;

    m_factorTypesOrdered= factorTypes;
    m_implFactor				= 0;

    FactorCollection &factorCollection = FactorCollection::Instance();

    // sentence markers
    for (size_t index = 0 ; index < factorTypes.size() ; ++index) {
      FactorType factorType = factorTypes[index];
      m_sentenceStartArray[factorType] 	= factorCollection.AddFactor(Output, factorType, BOS_);
      m_sentenceEndArray[factorType] 		= factorCollection.AddFactor(Output, factorType, EOS_);
    }

    return m_lmImpl->Load(filePath, m_implFactor, nGramOrder);
  }

  LMResult GetValueForgotState(const std::vector<const Word*> &contextFactor, FFState &outState) const {
    if (contextFactor.size() == 0) {
      LMResult ret;
      ret.score = 0.0;
      ret.unknown = false;
      return ret;
    }

    // joint context for internal LM
    std::vector<const Word*> jointContext;

    for (size_t currPos = 0 ; currPos < m_nGramOrder ; ++currPos ) {
      const Word &word = *contextFactor[currPos];

      // add word to chunked context
      std::stringstream stream("");

      const Factor *factor = word[ m_factorTypesOrdered[0] ];
      stream << factor->GetString();

      for (size_t index = 1 ; index < m_factorTypesOrdered.size() ; ++index) {
        FactorType factorType = m_factorTypesOrdered[index];
        const Factor *factor = word[factorType];
        stream << "|" << factor->GetString();
      }

      factor = FactorCollection::Instance().AddFactor(Output, m_implFactor, stream.str());

      Word* jointWord = new Word;
      jointWord->SetFactor(m_implFactor, factor);
      jointContext.push_back(jointWord);
    }

    // calc score on chunked phrase
    LMResult ret = m_lmImpl->GetValueForgotState(jointContext, outState);

    RemoveAllInColl(jointContext);

    return ret;
  }

  const FFState *GetNullContextState() const {
    return m_lmImpl->GetNullContextState();
  }

  const FFState *GetBeginSentenceState() const {
    return m_lmImpl->GetBeginSentenceState();
  }

  FFState *NewState(const FFState *from) const {
    return m_lmImpl->NewState(from);
  }

};

}
#endif