Diffstat (limited to 'inputmethods/dasher/LanguageModel.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | inputmethods/dasher/LanguageModel.h | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/inputmethods/dasher/LanguageModel.h b/inputmethods/dasher/LanguageModel.h new file mode 100644 index 0000000..8ffe100 --- a/dev/null +++ b/inputmethods/dasher/LanguageModel.h | |||
@@ -0,0 +1,123 @@ | |||
1 | // LanguageModel.h | ||
2 | // | ||
3 | ///////////////////////////////////////////////////////////////////////////// | ||
4 | // | ||
5 | // Copyright (c) 2001-2002 David Ward | ||
6 | // | ||
7 | ///////////////////////////////////////////////////////////////////////////// | ||
8 | |||
9 | // Abstract language model class | ||
10 | // See PPMModel for an example implementation | ||
11 | |||
12 | // Contexts are indentified by a unique unsigned integer | ||
13 | |||
14 | #ifndef __LanguageModel_h__ | ||
15 | #define __LanguageModel_h__ | ||
16 | |||
17 | #include "MSVC_Unannoy.h" | ||
18 | #include <vector> | ||
19 | #include <string> | ||
20 | |||
21 | #include "Alphabet.h" | ||
22 | #include "Context.h" | ||
23 | |||
24 | namespace Dasher {class CLanguageModel;} | ||
25 | class Dasher::CLanguageModel | ||
26 | { | ||
27 | public: | ||
28 | CLanguageModel(CAlphabet* Alphabet, int Normalization); | ||
29 | |||
30 | // Interface for the Dasher code | ||
31 | // -------------------------------------------------------------------------- | ||
32 | class CNodeContext { | ||
33 | public: | ||
34 | CNodeContext() {}; | ||
35 | virtual ~CNodeContext() {}; | ||
36 | }; | ||
37 | |||
38 | // return the model's normalization - what the probabilities sum to | ||
39 | const int normalization() const { return m_iNorm;} | ||
40 | |||
41 | CNodeContext* GetRootNodeContext(); | ||
42 | CNodeContext* CloneNodeContext(CNodeContext* NodeContext); | ||
43 | void ReleaseNodeContext(CNodeContext* NodeContext); | ||
44 | void EnterNodeSymbol(CNodeContext* NodeContext, symbol Symbol); | ||
45 | void LearnNodeSymbol(CNodeContext* NodeContext, symbol Symbol); | ||
46 | void EnterText(CNodeContext* NodeContext, std::string TheText); | ||
47 | void LearnText(CNodeContext* NodeContext, std::string* TheText, bool IsMore); | ||
48 | bool GetNodeProbs(CNodeContext* Context, std::vector<symbol> &NewSymbols, | ||
49 | std::vector<unsigned int> &Groups, std::vector<unsigned int> &Probs, double AddProb); | ||
50 | |||
51 | // Alphabet pass-through functions for widely needed information | ||
52 | symbol GetSpaceSymbol() {return m_Alphabet->GetSpaceSymbol();} | ||
53 | |||
54 | int GetColour(int character); | ||
55 | |||
56 | protected: | ||
57 | int GetNumberModelChars() {return m_Alphabet->GetNumberSymbols();} | ||
58 | |||
59 | // Generic language model functions to be implemented | ||
60 | // -------------------------------------------------------------------------- | ||
61 | typedef unsigned int modelchar; | ||
62 | |||
63 | // return the id for the root context: | ||
64 | virtual CContext* GetRootContext()=0; | ||
65 | // clone a context and return the new id: | ||
66 | virtual CContext* CloneContext(CContext*)=0; | ||
67 | // delete a context: | ||
68 | virtual void ReleaseContext(CContext*)=0; | ||
69 | // diagnostic info: | ||
70 | virtual void dump()=0; | ||
71 | // add character to the language model: | ||
72 | virtual void LearnSymbol(CContext* Context, modelchar Symbol)=0; | ||
73 | // update context with a character: | ||
74 | virtual void EnterSymbol(CContext* context, modelchar Symbol)=0; | ||
75 | // get the probability distrubution at the given context: | ||
76 | virtual bool GetProbs(CContext* Context, std::vector<unsigned int> &Probs, double AddProb)=0; | ||
77 | |||
78 | private: | ||
79 | CAlphabet *m_Alphabet; | ||
80 | int m_iModelChars; // number of charater in the model 1...ModelChars | ||
81 | int m_iNorm; // normalization of probabilities | ||
82 | }; | ||
83 | |||
84 | using namespace Dasher; | ||
85 | |||
86 | /////////////////////////////////////////////////////////////////// | ||
87 | |||
88 | inline CLanguageModel::CNodeContext* CLanguageModel::GetRootNodeContext() | ||
89 | { | ||
90 | return (CNodeContext*) GetRootContext(); | ||
91 | } | ||
92 | |||
93 | /////////////////////////////////////////////////////////////////// | ||
94 | |||
95 | inline CLanguageModel::CNodeContext* CLanguageModel::CloneNodeContext(CNodeContext* NodeContext) | ||
96 | { | ||
97 | return (CNodeContext*) CloneContext((CContext*) NodeContext); | ||
98 | } | ||
99 | |||
100 | /////////////////////////////////////////////////////////////////// | ||
101 | |||
102 | inline void CLanguageModel::ReleaseNodeContext(CNodeContext* NodeContext) | ||
103 | { | ||
104 | ReleaseContext((CContext*) NodeContext); | ||
105 | } | ||
106 | |||
107 | /////////////////////////////////////////////////////////////////// | ||
108 | |||
109 | inline void CLanguageModel::EnterNodeSymbol(CNodeContext* NodeContext, symbol Symbol) | ||
110 | { | ||
111 | EnterSymbol((CContext*) NodeContext, (modelchar) Symbol); | ||
112 | } | ||
113 | |||
114 | /////////////////////////////////////////////////////////////////// | ||
115 | |||
116 | inline void CLanguageModel::LearnNodeSymbol(CNodeContext* NodeContext, symbol Symbol) | ||
117 | { | ||
118 | LearnSymbol((CContext*) NodeContext, (modelchar) Symbol); | ||
119 | } | ||
120 | |||
121 | |||
122 | |||
123 | #endif /* #ifndef __LanguageModel_h__ */ | ||