tagging release
[dasher.git] / Src / DasherCore / ConversionManager.h
blob83200d2ddacb5e6833dfbd122d05b7f8ad4e04c1
1 // ConversionManager.h
2 //
3 // Copyright (c) 2007 The Dasher Team
4 //
5 // This file is part of Dasher.
6 //
7 // Dasher is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
12 // Dasher is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with Dasher; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef __conversion_manager_h__
22 #define __conversion_manager_h__
24 #include "ConversionHelper.h"
25 #include "AlphabetManager.h"
26 #include "DasherModel.h"
27 #include "DasherTypes.h"
28 #include "LanguageModelling/LanguageModel.h" // Urgh - we really shouldn't need to know about language models here
29 #include "NodeManager.h"
31 #include <vector>
33 // TODO: Conversion manager needs to deal with offsets and contexts - Will: See Phil for an explanation.
35 namespace Dasher {
36 class CDasherNode; // Forward declaration
38 /// \ingroup Model
39 /// @{
41 /// This class manages nodes in conversion subtrees, typically used
42 /// for languages where complex characters are entered through a
43 /// composition process, for example Japanese and Chinese.
44 ///
45 /// A new CConversionManager is created for each subtree, and
46 /// therefore represents the conversion of a single phrase. The
47 /// phrase to be converted is read by recursing through the parent
48 /// tree. An instance of CConversionHelper is shared by several
49 /// CConversionManagers, and performs the language dependent aspects
50 /// of conversion. Specifically construction of the candidate
51 /// lattice and assignment of weights.
52 ///
53 /// The general policy is to delay computation as far as possible,
54 /// to avoid unnecessary computational load. The candidate lattice
55 /// is therefore built at the first call to PopulateChildren, and
56 /// weights are only assigned when the appropriate node has its
57 /// child list populated.
58 ///
59 /// See CConversionHelper for details of the language specific
60 /// aspects of conversion, and CNodeManager for details of the node
61 /// management process.
62 ///
63 class CConversionManager : public CNodeManager {
64 public:
65 // TODO: We shouldn't need to know about this stuff, but the code is somewhat in knots at the moment
66 CConversionManager(CNodeCreationManager *pNCManager, CConversionHelper *pHelper, CAlphabet *pAlphabet, int CMid);
67 ~CConversionManager();
69 ///
70 /// Increment reference count
71 ///
73 virtual void Ref() {
74 ++m_iRefCount;
77 ///
78 /// Decrement reference count
79 ///
81 virtual void Unref() {
82 --m_iRefCount;
85 // std::cout << "Unref, new count = " << m_iRefCount << std::endl;
87 if(m_iRefCount == 0) {
88 // std::cout << "Deleting " << this << std::endl;
89 delete this;
93 ///
94 /// Get a new root node owned by this manager
95 ///
97 virtual CDasherNode *GetRoot(CDasherNode *pParent, int iLower, int iUpper, void *pUserData);
99 ///
100 /// Calculate sizes for each of the children - default
101 /// implementation assigns decending probabilities in a power law
102 /// fashion (so assumes ordering), but specific subclasses are
103 /// free to implement their own behaviour. The only restriction is
104 /// that sizes should be posivive and sum to the appropriate
105 /// normalisation constant
108 virtual void AssignChildSizes(SCENode *pNode, CLanguageModel::Context context, int iNChildren);
111 /// Provide children for the supplied node
114 virtual void PopulateChildren( CDasherNode *pNode );
117 /// Delete any storage alocated for this node
120 virtual void ClearNode( CDasherNode *pNode );
123 /// Called whenever a node belonging to this manager first
124 /// moves under the crosshair
127 virtual void Output( CDasherNode *pNode, Dasher::VECTOR_SYMBOL_PROB* pAdded, int iNormalization);
130 /// Called when a node is left backwards
133 virtual void Undo( CDasherNode *pNode );
136 /// Entered backwards
139 virtual void Enter(CDasherNode *pNode) {};
141 ///
142 /// Left forwards
145 virtual void Leave(CDasherNode *pNode) {};
148 /// Rebuild the parent of a given node - used for when backoff occurs beyond the start of the tree
151 virtual CDasherNode *RebuildParent(CDasherNode *pNode, int iGeneration) {
152 return 0;
155 virtual void SetFlag(CDasherNode *pNode, int iFlag, bool bValue);
157 private:
159 ///
160 /// Build the conversion tree (lattice) for the given string -
161 /// evaluated late to prevent unnecessary conversions when the
162 /// children of the root node are never instantiated
165 void BuildTree(CDasherNode *pRoot);
167 ///
168 /// Recursively delete the conversion tree when we're done
171 bool RecursiveDelTree(SCENode* pNode);
173 ///
174 /// Dump tree to stdout (debug)
177 void RecursiveDumpTree(SCENode *pCurrent, unsigned int iDepth);
180 /// Flag whether the tree has already been built
183 bool m_bTreeBuilt;
186 /// Root of the tree (TODO: Why is this a double pointer?)
189 SCENode **m_pRoot;
192 /// Dasher model (TODO: We ideally shouldn't need to know about this)
195 CNodeCreationManager *m_pNCManager;
198 /// Language model (TODO: We don't need to know about this, surely)
201 CLanguageModel *m_pLanguageModel;
204 /// Conversion helper
207 CConversionHelper *m_pHelper;
209 CAlphabet *m_pAlphabet;
212 /// Reference count
216 CLanguageModel::Context m_iLearnContext;
219 int m_iRefCount;
222 /// Unique identifier, used to talk to the conversion helper so
223 /// that it can be shared between multiple conversion nodes
224 /// without state collisions
227 int m_iCMID;
229 //TODO: REVISE
230 struct SConversionData {
231 // symbol iSymbol;
232 // int iPhase;
233 CLanguageModel *pLanguageModel;
234 CLanguageModel::Context iContext;
235 SCENode * pSCENode;
236 bool bType; // True for termial nodes (bit of a hack)
237 int iOffset;
238 //int iGameOffset;
241 /// @}
244 #endif