3 // Copyright (c) 2007 The Dasher Team
5 // This file is part of Dasher.
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"
33 // TODO: Conversion manager needs to deal with offsets and contexts - Will: See Phil for an explanation.
36 class CDasherNode
; // Forward declaration
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.
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.
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.
59 /// See CConversionHelper for details of the language specific
60 /// aspects of conversion, and CNodeManager for details of the node
61 /// management process.
63 class CConversionManager
: public CNodeManager
{
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();
70 /// Increment reference count
78 /// Decrement reference count
81 virtual void Unref() {
85 // std::cout << "Unref, new count = " << m_iRefCount << std::endl;
87 if(m_iRefCount
== 0) {
88 // std::cout << "Deleting " << this << std::endl;
94 /// Get a new root node owned by this manager
97 virtual CDasherNode
*GetRoot(CDasherNode
*pParent
, int iLower
, int iUpper
, void *pUserData
);
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
) {};
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
) {
155 virtual void SetFlag(CDasherNode
*pNode
, int iFlag
, bool bValue
);
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
);
168 /// Recursively delete the conversion tree when we're done
171 bool RecursiveDelTree(SCENode
* pNode
);
174 /// Dump tree to stdout (debug)
177 void RecursiveDumpTree(SCENode
*pCurrent
, unsigned int iDepth
);
180 /// Flag whether the tree has already been built
186 /// Root of the tree (TODO: Why is this a double pointer?)
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
;
216 CLanguageModel::Context m_iLearnContext
;
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
230 struct SConversionData
{
233 CLanguageModel
*pLanguageModel
;
234 CLanguageModel::Context iContext
;
236 bool bType
; // True for termial nodes (bit of a hack)