File: MixtureLanguageModel.h

package info (click to toggle)
dasher 4.11%2Bgit20130508.adc653-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 40,248 kB
  • ctags: 5,158
  • sloc: xml: 185,479; cpp: 32,301; sh: 11,207; makefile: 828; ansic: 483
file content (176 lines) | stat: -rw-r--r-- 5,745 bytes parent folder | download | duplicates (6)
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
// MixtureLanguageModel.h
//
/////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2001-2005 David Ward
//
/////////////////////////////////////////////////////////////////////////////

#ifndef __LanguageModelling_MixtureLanguageModel_h__
#define __LanguageModelling_MixtureLanguageModel_h__

#include "LanguageModel.h"
#include "PPMLanguageModel.h"
#include "DictLanguageModel.h"

//#include <iostream>
#include <vector>

/////////////////////////////////////////////////////////////////////////////

namespace Dasher {
  class CMixtureLanguageModel;

  /// \ingroup LM
  /// \{
  class CMixtureLanguageModel:public CLanguageModel, protected CSettingsUser {
  public:

    /////////////////////////////////////////////////////////////////////////////

    CMixtureLanguageModel(CSettingsUser *pCreator, const CAlphInfo *pAlph, const CAlphabetMap *pAlphMap)
    : CLanguageModel(pAlph->iEnd-1), CSettingsUser(pCreator) {

      //      std::cout << m_pAlphabet << std::endl;

      NextContext = 0;

      lma = new CPPMLanguageModel(this, m_iNumSyms);
      lmb = new CDictLanguageModel(this, pAlph, pAlphMap);

    };

      virtual ~ CMixtureLanguageModel() {
      delete lma;
      delete lmb;
    };

    /////////////////////////////////////////////////////////////////////////////
    // Context creation/destruction
    ////////////////////////////////////////////////////////////////////////////

    // FIXME - need to work out how to do this

    // Create a context (empty)
    virtual CLanguageModel::Context CreateEmptyContext();
    virtual CLanguageModel::Context CloneContext(CLanguageModel::Context Context);
    virtual void ReleaseContext(CLanguageModel::Context Context);

    /////////////////////////////////////////////////////////////////////////////
    // Context modifiers
    ////////////////////////////////////////////////////////////////////////////

    // Update context with a character - only modifies context
    virtual void EnterSymbol(CLanguageModel::Context context, int Symbol) {
      lma->EnterSymbol(ContextMap.find(context)->second->GetContextA(), Symbol);
      lmb->EnterSymbol(ContextMap.find(context)->second->GetContextB(), Symbol);
    };

    // Add character to the language model at the current context and update the context 
    // - modifies both the context and the LanguageModel
    virtual void LearnSymbol(CLanguageModel::Context context, int Symbol) {
      lma->LearnSymbol(ContextMap[context]->GetContextA(), Symbol);
      lmb->LearnSymbol(ContextMap[context]->GetContextB(), Symbol);
    };

    /////////////////////////////////////////////////////////////////////////////
    // Prediction
    /////////////////////////////////////////////////////////////////////////////

    // Get symbol probability distribution
    virtual void GetProbs(CLanguageModel::Context context, std::vector < unsigned int >&Probs, int iNorm, int iUniform) const {

      int iNumSymbols = GetSize();

        Probs.resize(iNumSymbols);

        std::vector < unsigned int >ProbsA(iNumSymbols);
        std::vector < unsigned int >ProbsB(iNumSymbols);

      int iNormA(iNorm * GetLongParameter(LP_LM_MIXTURE) / 100);
      int iNormB(iNorm - iNormA);
      
      // TODO: Fix uniform here
        lma->GetProbs(ContextMap.find(context)->second->GetContextA(), ProbsA, iNormA, 0);
        lmb->GetProbs(ContextMap.find(context)->second->GetContextB(), ProbsB, iNormB, 0);

      for(int i(1); i < iNumSymbols; i++) {
        Probs[i] = ProbsA[i] + ProbsB[i];
    }};

  private:
    CLanguageModel * lma;
    CLanguageModel *lmb;

    class CMixtureContext {
    public:
      CMixtureContext(CLanguageModel * _lma, CLanguageModel * _lmb):lma(_lma), lmb(_lmb) {
        ca = lma->CreateEmptyContext();
        cb = lmb->CreateEmptyContext();
      };

    CMixtureContext(CLanguageModel * _lma, CLanguageModel * _lmb, CLanguageModel::Context _ca, CLanguageModel::Context _cb):lma(_lma), lmb(_lmb), ca(_ca), cb(_cb) {
      };

      ~CMixtureContext() {
        lma->ReleaseContext(ca);
        lmb->ReleaseContext(cb);
      };

      CLanguageModel::Context GetContextA() {
        return ca;
      }

      CLanguageModel::Context GetContextB() {
        return cb;
      }

    private:
      CLanguageModel * lma;
      CLanguageModel *lmb;

      CLanguageModel::Context ca;
      CLanguageModel::Context cb;
    };

    int NextContext;

    std::map < int, CMixtureContext * >ContextMap;

  };
  /// \}

///////////////////////////////////////////////////////////////////

  inline CLanguageModel::Context CMixtureLanguageModel::CreateEmptyContext() {
    CMixtureContext *pCont = new CMixtureContext(lma, lmb);
    ContextMap[NextContext] = pCont;
    ++NextContext;
    return NextContext - 1;
  }

///////////////////////////////////////////////////////////////////

  inline CLanguageModel::Context CMixtureLanguageModel::CloneContext(CLanguageModel::Context Copy) {
    CMixtureContext *pCopy = ContextMap[Copy];
    CMixtureContext *pCont = new CMixtureContext(lma, lmb, lma->CloneContext(pCopy->GetContextA()), lmb->CloneContext(pCopy->GetContextB()));

    ContextMap[NextContext] = pCont;
    ++NextContext;
    return NextContext - 1;
  }

///////////////////////////////////////////////////////////////////

  inline void CMixtureLanguageModel::ReleaseContext(CLanguageModel::Context release) {
    // m_ContextAlloc.Free( (CMixtureContext*) release );
    delete ContextMap[release];
    ContextMap[release] = NULL;
  }
}

///////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////

#endif // ndef __LanguageModelling_MixtureLanguageModel_h__