tagging release
[dasher.git] / trunk / Src / DasherCore / LanguageModelling / DictLanguageModel.h
blob7ca54f3b80d5ea4fee05f7ee5bca12883ebb2005
1 // PPMLanguageModel.h
2 //
3 /////////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (c) 1999-2004 David Ward
6 //
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"
18 #include <vector>
19 #include <map>
20 #include <string>
21 #include <stdio.h>
23 //static char dumpTrieStr[40000];
24 //const int maxcont =200;
26 namespace Dasher {
27 /// \ingroup LM
28 /// \{
29 class CDictLanguageModel:public CLanguageModel {
30 public:
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;
47 } private:
49 void MyLearnSymbol(Context context, int Symbol);
51 class CDictnode {
52 public:
53 CDictnode * find_symbol(int sym) const;
54 CDictnode *child;
55 CDictnode *next;
56 CDictnode *vine;
57 unsigned short int count;
58 int sbl;
60 CDictnode(int sym);
61 CDictnode();
64 class CDictContext {
65 public:
66 CDictContext(CDictContext const &input) {
67 head = input.head;
68 word_head = input.word_head;
69 current_word = input.current_word;
70 order = input.order;
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
74 ~CDictContext() {
76 void dump();
77 CDictnode *head;
78 int order;
80 std::string current_word;
81 CDictnode *word_head;
82 int word_order;
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;
96 CDictnode *m_pRoot;
98 std::map < std::string, int >dict; // Dictionary
99 int nextid;
101 int NodesAllocated;
103 int max_order;
105 mutable CSimplePooledAlloc < CDictnode > m_NodeAlloc;
106 CPooledAlloc < CDictContext > m_ContextAlloc;
108 /// \}
110 ////////////////////////////////////////////////////////////////////////
111 // Inline functions
112 ////////////////////////////////////////////////////////////////////////
114 ////////////////////////////////////////////////////////////////////////
116 inline Dasher::CDictLanguageModel::CDictnode::CDictnode(symbol sym):sbl(sym) {
117 child = next = vine = 0;
118 count = 1;
121 ////////////////////////////////////////////////////////////////////////
123 inline CDictLanguageModel::CDictnode::CDictnode() {
124 child = next = vine = 0;
125 count = 1;
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;
141 *pCont = *pCopy;
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__ */