3 /////////////////////////////////////////////////////////////////////////////
5 // Copyright (c) 1999-2004 David Ward
7 /////////////////////////////////////////////////////////////////////////////
9 #ifndef __DictLanguageModel_h__
10 #define __DictLanguageModel_h__
12 #include "../../Common/NoClones.h"
13 #include "../../Common/Allocators/PooledAlloc.h"
14 #include "LanguageModel.h"
15 #include "PPMLanguageModel.h"
16 #include "../DasherTypes.h"
23 //static char dumpTrieStr[40000];
24 //const int maxcont =200;
29 class CDictLanguageModel
:public CLanguageModel
{
31 CDictLanguageModel(Dasher::CEventHandler
* pEventHandler
, CSettingsStore
* pSettingsStore
, const CSymbolAlphabet
& Alphabet
);
32 virtual ~CDictLanguageModel();
34 Context
CreateEmptyContext();
35 void ReleaseContext(Context context
);
36 Context
CloneContext(Context context
);
38 virtual void GetProbs(Context Context
, std::vector
< unsigned int >&Probs
, int iNorm
) const;
40 virtual void EnterSymbol(Context context
, int Symbol
);
41 virtual void LearnSymbol(Context context
, int Symbol
) {
42 EnterSymbol(context
, Symbol
);
43 }; // Never learn in this model
45 virtual int GetMemory() {
46 return NodesAllocated
;
49 void MyLearnSymbol(Context context
, int Symbol
);
53 CDictnode
* find_symbol(int sym
) const;
57 unsigned short int count
;
66 CDictContext(CDictContext
const &input
) {
68 word_head
= input
.word_head
;
69 current_word
= input
.current_word
;
71 word_order
= input
.word_order
;
72 } CDictContext(CDictnode
* _head
= 0, int _order
= 0):head(_head
), order(_order
), word_head(_head
), word_order(0) {
73 }; // FIXME - doesn't work if we're trying to create a non-empty context
80 std::string current_word
;
86 CDictnode
*AddSymbolToNode(CDictnode
* pNode
, symbol sym
, int *update
);
88 void AddSymbol(CDictContext
& context
, symbol sym
);
90 void CollapseContext(CDictContext
& context
) const;
92 int lookup_word(const std::string
& w
);
93 int lookup_word_const(const std::string
& w
) const;
95 CDictContext
*m_rootcontext
;
98 std::map
< std::string
, int >dict
; // Dictionary
105 mutable CSimplePooledAlloc
< CDictnode
> m_NodeAlloc
;
106 CPooledAlloc
< CDictContext
> m_ContextAlloc
;
110 ////////////////////////////////////////////////////////////////////////
112 ////////////////////////////////////////////////////////////////////////
114 ////////////////////////////////////////////////////////////////////////
116 inline Dasher::CDictLanguageModel::CDictnode::CDictnode(symbol sym
):sbl(sym
) {
117 child
= next
= vine
= 0;
121 ////////////////////////////////////////////////////////////////////////
123 inline CDictLanguageModel::CDictnode::CDictnode() {
124 child
= next
= vine
= 0;
128 ///////////////////////////////////////////////////////////////////
130 inline CLanguageModel::Context
CDictLanguageModel::CreateEmptyContext() {
131 CDictContext
*pCont
= m_ContextAlloc
.Alloc();
132 *pCont
= *m_rootcontext
;
133 return (Context
) pCont
;
136 ///////////////////////////////////////////////////////////////////
138 inline CLanguageModel::Context
CDictLanguageModel::CloneContext(Context Copy
) {
139 CDictContext
*pCont
= m_ContextAlloc
.Alloc();
140 CDictContext
*pCopy
= (CDictContext
*) Copy
;
142 return (Context
) pCont
;
145 ///////////////////////////////////////////////////////////////////
147 inline void CDictLanguageModel::ReleaseContext(Context release
) {
148 m_ContextAlloc
.Free((CDictContext
*) release
);
151 ///////////////////////////////////////////////////////////////////
153 } // end namespace Dasher
155 #endif /* #ifndef __DictLanguageModel_H__ */