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 186 187 188 189 190 191 192 193 194
|
// PPMLanguageModel.h
//
/////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 1999-2004 David Ward
//
/////////////////////////////////////////////////////////////////////////////
#ifndef __WordLanguageModel_h__
#define __WordLanguageModel_h__
#include <cstdlib>
#include "../../Common/NoClones.h"
#include "../../Common/Allocators/PooledAlloc.h"
#include "PPMLanguageModel.h"
#include "../SettingsStore.h"
#include "../Alphabet/AlphInfo.h"
#include "../Alphabet/AlphabetMap.h"
#include <vector>
#include <map>
#include <string>
#include <stdio.h>
//static char dumpTrieStr[40000];
//const int maxcont =200;
namespace Dasher {
/// \ingroup LM
/// \{
///
/// Language model using words
///
class CWordLanguageModel:public CLanguageModel, protected CSettingsUser {
public:
CWordLanguageModel(CSettingsUser *pCreator, const CAlphInfo *pAlph, const CAlphabetMap *pAlphMap);
virtual ~ CWordLanguageModel();
Context CreateEmptyContext();
void ReleaseContext(Context context);
Context CloneContext(Context context);
virtual void GetProbs(Context Context, std::vector < unsigned int >&Probs, int iNorm, int iUniform) const;
virtual void EnterSymbol(Context context, int Symbol);
virtual void LearnSymbol(Context context, int Symbol);
private:
class CWordnode {
public:
CWordnode * find_symbol(int sym)const;
CWordnode *child;
CWordnode *next;
CWordnode *vine;
unsigned int count;
int sbl;
CWordnode(int sym);
CWordnode();
void RecursiveDump(std::ofstream & file);
};
class CWordContext {
public:
CWordContext(CWordContext const &input) {
head = input.head;
word_head = input.word_head;
current_word = input.current_word;
order = input.order;
word_order = input.word_order;
}
CWordContext(CWordnode * _head = 0, int _order = 0): head(_head), order(_order), word_head(_head), word_order(0)
{}; // FIXME - doesn't work if we're trying to create a non-empty context
~CWordContext() {
};
void dump();
CWordnode *head;
int order;
std::string current_word;
CWordnode *word_head;
int word_order;
std::vector < unsigned int >oSpellingProbs;
int m_iSpellingNorm;
double m_dSpellingFactor;
/// Pointer to the letter based model - note that we don't
/// actually own this, so don't delete it
CPPMLanguageModel *m_pSpellingModel;
///
/// The corresponding context in the spelling model
CPPMLanguageModel::Context oSpellingContext;
};
CWordnode *AddSymbolToNode(CWordnode * pNode, symbol sym, int *update, bool bLearn);
void AddSymbol(CWordContext & context, symbol sym, bool bLearn);
void CollapseContext(CWordContext & context, bool bLearn);
int lookup_word(const std::string & w);
int lookup_word_const(const std::string & w) const;
const CAlphabetMap *m_pAlphMap;
const int m_iSpaceSymbol;
CWordContext *m_rootcontext;
CWordnode *m_pRoot;
std::map < std::string, int >dict; // Dictionary
int nextid;
int iWordStart;
int wordidx;
int NodesAllocated;
int max_order;
CPPMLanguageModel *pSpellingModel; // Use this to predict the spellings of new words
mutable CSimplePooledAlloc < CWordnode > m_NodeAlloc;
CPooledAlloc < CWordContext > m_ContextAlloc;
};
/// \}
////////////////////////////////////////////////////////////////////////
// Inline functions
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
inline Dasher::CWordLanguageModel::CWordnode::CWordnode(symbol sym):sbl(sym) {
child = next = vine = 0;
count = 1;
}
////////////////////////////////////////////////////////////////////////
inline CWordLanguageModel::CWordnode::CWordnode() {
child = next = vine = 0;
count = 1;
}
///////////////////////////////////////////////////////////////////
inline CLanguageModel::Context CWordLanguageModel::CreateEmptyContext() {
return CloneContext((Context)m_rootcontext);
}
///////////////////////////////////////////////////////////////////
inline CLanguageModel::Context CWordLanguageModel::CloneContext(Context Copy) {
CWordContext *pCont = m_ContextAlloc.Alloc();
CWordContext *pCopy = (CWordContext *) Copy;
*pCont = *pCopy;
// Create a clone of the spelling context
pCont->oSpellingContext = pCont->m_pSpellingModel->CloneContext(pCopy->oSpellingContext);
return (Context) pCont;
}
///////////////////////////////////////////////////////////////////
inline void CWordLanguageModel::ReleaseContext(Context release) {
// Urgh!
CWordContext *pCont(reinterpret_cast<CWordContext *>(release));
pCont->m_pSpellingModel->ReleaseContext(pCont->oSpellingContext);
m_ContextAlloc.Free(pCont);
}
///////////////////////////////////////////////////////////////////
} // end namespace Dasher
#endif /* #ifndef __WordLanguageModel_H__ */
|