26
CAlphabetManager::CAlphabetManager( CNodeCreationManager *pNCManager, CLanguageModel *pLanguageModel, CLanguageModel::Context iLearnContext, bool bGameMode, const std::string &strGameModeText )
26
CAlphabetManager::CAlphabetManager(CDasherInterfaceBase *pInterface, CNodeCreationManager *pNCManager, CLanguageModel *pLanguageModel, CLanguageModel::Context iLearnContext, bool bGameMode, const std::string &strGameModeText )
27
27
: CNodeManager(0), m_pLanguageModel(pLanguageModel), m_pNCManager(pNCManager) {
29
m_pInterface = pInterface;
29
31
m_iLearnContext = iLearnContext;
30
32
m_bGameMode = bGameMode;
31
33
m_strGameString = strGameModeText;
34
36
CDasherNode *CAlphabetManager::GetRoot(CDasherNode *pParent, int iLower, int iUpper, void *pUserData) {
35
37
CDasherNode *pNewNode;
39
// TODO: iOffset has gotten a bit hacky here
40
iSymbol = *((int *)pUserData);
44
// FIXME - Make this a CDasherComponent
45
std::string strContext;
47
CLanguageModel::Context iContext;
49
if(pUserData && static_cast<SRootData *>(pUserData)->szContext) {
50
std::string strRootText(static_cast<SRootData *>(pUserData)->szContext);
52
int iMaxContextLength = m_pLanguageModel->GetContextLength() + 1;
54
// TODO: No need to explicitly pass context
55
// TODO: Utility function for looking up symbolic context
57
int iStart = static_cast<SRootData *>(pUserData)->iOffset - iMaxContextLength;
61
strContext = m_pInterface->GetContext(iStart, static_cast<SRootData *>(pUserData)->iOffset - iStart);
62
BuildContext(strContext, false, iContext, iSymbol);
64
iOffset = static_cast<SRootData *>(pUserData)->iOffset - 1;
65
iColour = m_pNCManager->GetColour(iSymbol);
71
iOffset = static_cast<SRootData *>(pUserData)->iOffset;
51
iColour = m_pNCManager->GetColour(iSymbol);
76
strContext = m_pNCManager->GetAlphabet()->GetDefaultContext();
77
BuildContext(strContext, true, iContext, iSymbol);
80
// FIXME - Make this a CDasherComponent
53
82
pNewNode = new CDasherNode(pParent, iSymbol, 0, Opts::Nodes1, iLower, iUpper, m_pLanguageModel, iColour);
55
// pNewNode->SetContext(m_pLanguageModel->CreateEmptyContext()); // FIXME - handle context properly
56
83
pNewNode->m_pNodeManager = this;
57
pNewNode->m_bShove = true;
84
pNewNode->SetShove(true);
58
85
pNewNode->m_pBaseGroup = m_pNCManager->GetAlphabet()->m_pBaseGroup;
59
86
pNewNode->m_strDisplayText = m_pNCManager->GetAlphabet()->GetDisplayText(iSymbol);
87
pNewNode->SetFlag(NF_SEEN, true);
62
89
SAlphabetData *pNodeUserData = new SAlphabetData;
63
90
pNewNode->m_pUserData = pNodeUserData;
92
pNodeUserData->iOffset = iOffset;
65
93
pNodeUserData->iPhase = 0;
66
94
pNodeUserData->iSymbol = iSymbol;
67
95
pNodeUserData->pLanguageModel = m_pLanguageModel;
69
// Hopefully this will obsolete any need to handle contexts outside
70
// of the alphabet manager - check this and remove resulting
72
CLanguageModel::Context iContext;
73
iContext = m_pLanguageModel->CreateEmptyContext();
75
std::string strContext = m_pNCManager->GetAlphabet()->GetDefaultContext();
77
std::vector<symbol> vContextSymbols;
78
m_pNCManager->GetAlphabet()->GetSymbols(&vContextSymbols, &strContext, false);
80
for(std::vector<symbol>::iterator it(vContextSymbols.begin()); it != vContextSymbols.end(); ++it)
81
m_pLanguageModel->EnterSymbol(iContext, *it);
83
97
pNodeUserData->iContext = iContext;
86
100
pNodeUserData->iGameOffset = -1;
87
pNewNode->SetGame(true);
101
pNewNode->SetFlag(NF_GAME, true);
121
135
for(int j = 0; j < iChildCount; j++) {
122
136
CDasherNode *pNewNode;
124
if(newchars[j] == m_pNCManager->GetControlSymbol())
125
pNewNode = m_pNCManager->GetRoot(1, pNode, iLbnd, cum[j], NULL);
138
if(newchars[j] == m_pNCManager->GetControlSymbol()) {
139
pNewNode = m_pNCManager->GetRoot(1, pNode, iLbnd, cum[j], &(pParentUserData->iOffset));
126
141
else if(newchars[j] == m_pNCManager->GetStartConversionSymbol()) {
127
142
// TODO: Need to consider the case where there is no compile-time support for this
128
pNewNode = m_pNCManager->GetRoot(2, pNode, iLbnd, cum[j], NULL);
143
pNewNode = m_pNCManager->GetRoot(2, pNode, iLbnd, cum[j], &(pParentUserData->iOffset));
130
145
else if( newchars[j] == iExistingSymbol) {
131
146
pNewNode = pExistingChild;
148
// std::cout << "Marker" << std::endl;
132
150
pNewNode->SetRange(iLbnd, cum[j]);
155
173
pNewNode = new CDasherNode(pNode, newchars[j], 0, Nodes1, iLbnd, cum[j], pParentUserData->pLanguageModel, iColour);
156
174
pNewNode->m_pNodeManager = this;
157
pNewNode->m_bShove = true;
175
pNewNode->SetShove(true);
158
176
pNewNode->m_pBaseGroup = m_pNCManager->GetAlphabet()->m_pBaseGroup;
160
178
SAlphabetData *pNodeUserData = new SAlphabetData;
161
179
pNewNode->m_pUserData = pNodeUserData;
181
pNodeUserData->iOffset = pParentUserData->iOffset + 1;
163
182
pNodeUserData->iPhase = iNewPhase;
164
183
pNodeUserData->iSymbol = newchars[j];
231
249
CDasherNode *CAlphabetManager::RebuildParent(CDasherNode *pNode, int iGeneration) {
233
// TODO: Reimplement -----
235
// Ask for context information
236
// m_pNCManager->m_strContextBuffer = "";
237
// Dasher::CEditContextEvent oEvent(10);
238
// m_pNCManager->InsertEvent(&oEvent);
240
// std::string strContext(m_pNCManager->m_strContextBuffer);
241
std::vector<symbol> vSymbols;
242
// m_pLanguageModel->SymbolAlphabet().GetAlphabetPointer()->GetSymbols(&vSymbols, &strContext, false);
251
// TODO: iGeneration obsolete?
253
int iOffset(static_cast<SAlphabetData *>(pNode->m_pUserData)->iOffset);
254
int iNewOffset = iOffset - 1;
246
256
CDasherNode *pNewNode;
251
261
symbol iNewSymbol;
253
CLanguageModel::Context oContext(m_pLanguageModel->CreateEmptyContext());
255
if(static_cast<int>(vSymbols.size()) <= iGeneration + 1) {
256
// Create a root node if there isn't any more context
263
std::string strContext;
264
CLanguageModel::Context iContext;
267
// pNode is already a root
270
else if(iOffset == 0) {
271
// TODO: Creating a root node, Shouldn't be a special case
274
strContext = m_pNCManager->GetAlphabet()->GetDefaultContext();
275
BuildContext(strContext, true, iContext, iNewSymbol);
260
277
pNewNode = new CDasherNode(0, 0, 0, Opts::Nodes1, 0, 0, m_pLanguageModel, 7);
262
// TODO: Horrible hard coded default context
263
m_pNCManager->EnterText(oContext, ". ");
280
int iMaxContextLength = m_pLanguageModel->GetContextLength() + 1;
282
int iStart = iOffset - iMaxContextLength;
286
strContext = m_pInterface->GetContext(iStart, iOffset - iStart);
287
BuildContext(strContext, false, iContext, iNewSymbol);
267
289
iNewPhase = ((iOldPhase + 2 - 1) % 2);
268
iNewSymbol = vSymbols[vSymbols.size() - iGeneration - 2];
270
int iColour(m_pNCManager->GetColour(vSymbols[vSymbols.size() - iGeneration - 2]));
291
int iColour(m_pNCManager->GetColour(iNewSymbol));
272
293
// Loop colours if necessary for the colour scheme
273
294
if(iNewPhase == 1)
276
297
pNewNode = new CDasherNode(0, iNewSymbol, 0, Nodes1, 0, 0, m_pLanguageModel, iColour);
278
for(unsigned int i(0); i < vSymbols.size() - iGeneration -1; ++i)
279
m_pLanguageModel->EnterSymbol(oContext, vSymbols[i]);
300
// TODO: Some of this context stuff could be consolidated
282
302
pNewNode->m_pNodeManager = this;
283
pNewNode->m_bShove = true;
284
pNewNode->Seen(true);
303
pNewNode->SetShove(true);
304
pNewNode->SetFlag(NF_SEEN, true);
285
305
pNewNode->m_pBaseGroup = m_pNCManager->GetAlphabet()->m_pBaseGroup;
287
307
SAlphabetData *pNodeUserData = new SAlphabetData;
288
308
pNewNode->m_pUserData = pNodeUserData;
310
pNodeUserData->iOffset = iNewOffset;
290
311
pNodeUserData->iPhase = iNewPhase;
291
312
pNodeUserData->iSymbol = iNewSymbol;
292
313
pNodeUserData->pLanguageModel = m_pLanguageModel;
293
pNodeUserData->iContext = oContext;
314
pNodeUserData->iContext = iContext;
295
316
PopulateChildrenWithSymbol(pNewNode, static_cast<SAlphabetData *>(pNode->m_pUserData)->iSymbol, pNode);
333
void CAlphabetManager::BuildContext(std::string strContext, bool bRoot, CLanguageModel::Context &oContext, symbol &iSymbol) {
334
// Hopefully this will obsolete any need to handle contexts outside
335
// of the alphabet manager - check this and remove resulting
338
std::vector<symbol> vContextSymbols;
339
m_pNCManager->GetAlphabet()->GetSymbols(&vContextSymbols, &strContext, false);
341
oContext = m_pLanguageModel->CreateEmptyContext();
343
for(std::vector<symbol>::iterator it(vContextSymbols.begin()); it != vContextSymbols.end(); ++it)
344
m_pLanguageModel->EnterSymbol(oContext, *it);
346
if((vContextSymbols.size() == 0) || bRoot)
349
iSymbol = vContextSymbols[vContextSymbols.size() - 1];