[dasher: 8/43] Tidy Alphabet/Control-node probability calcs



commit 8ebc46ed6913b745f933521093782319cefe006b
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Tue May 31 12:53:34 2011 +0100

    Tidy Alphabet/Control-node probability calcs
    
    NCManager leaves space for Control node, via GetAlphNodeNormalization();
      move AddExtras into NCManager which makes it.
    
    Also flesh out ConvertingAlphMgr: conversion root created by CreateSymbolNode
      for iSymbol == one beyond alphabet text symbols.

 Src/DasherCore/AlphabetManager.cpp     |   34 +++++++------------------------
 Src/DasherCore/AlphabetManager.h       |    7 +----
 Src/DasherCore/ConversionHelper.h      |    2 -
 Src/DasherCore/ConvertingAlphMgr.cpp   |   17 +++++++++------
 Src/DasherCore/ConvertingAlphMgr.h     |    9 +++++++-
 Src/DasherCore/NodeCreationManager.cpp |   30 ++++++++++++++++++++++++---
 Src/DasherCore/NodeCreationManager.h   |   15 +++++++------
 7 files changed, 62 insertions(+), 52 deletions(-)
---
diff --git a/Src/DasherCore/AlphabetManager.cpp b/Src/DasherCore/AlphabetManager.cpp
index dc1c72e..7654ec4 100644
--- a/Src/DasherCore/AlphabetManager.cpp
+++ b/Src/DasherCore/AlphabetManager.cpp
@@ -276,19 +276,14 @@ int CAlphabetManager::CAlphNode::ExpectedNumChildren() {
 
 void CAlphabetManager::GetProbs(vector<unsigned int> *pProbInfo, CLanguageModel::Context context) {
   const unsigned int iSymbols = m_pAlphabet->GetNumberTextSymbols();
-  unsigned long iNorm(m_pNCManager->GetLongParameter(LP_NORMALIZATION));
-
+  
   // TODO - sort out size of control node - for the timebeing I'll fix the control node at 5%
   // TODO: New method (see commented code) has been removed as it wasn' working.
 
-  const int iControlSpace = m_pNCManager->GetBoolParameter(BP_CONTROL_MODE) ? iNorm / 20 : 0;
-
-  iNorm -= iControlSpace;
-
-  const unsigned long uniform(m_pNCManager->GetLongParameter(LP_UNIFORM));
+  const unsigned long iNorm(m_pNCManager->GetAlphNodeNormalization());
   //the case for control mode on, generalizes to handle control mode off also,
   // as then iNorm - control_space == iNorm...
-  const unsigned int iUniformAdd = ((iNorm * uniform) / 1000) / iSymbols;
+  const unsigned int iUniformAdd = ((iNorm * m_pNCManager->GetLongParameter(LP_UNIFORM)) / 1000) / iSymbols;
   const unsigned long iNonUniformNorm = iNorm - iSymbols * iUniformAdd;
   //  m_pLanguageModel->GetProbs(context, Probs, iNorm, ((iNorm * uniform) / 1000));
 
@@ -297,17 +292,17 @@ void CAlphabetManager::GetProbs(vector<unsigned int> *pProbInfo, CLanguageModel:
   // to GetProbs as per ordinary language model, so no need to test....
   m_pLanguageModel->GetProbs(context, *pProbInfo, iNonUniformNorm, 0);
 
-  DASHER_ASSERT(pProbInfo->size() == m_pAlphabet->GetNumberTextSymbols()+1);//initial 0
+  DASHER_ASSERT(pProbInfo->size() == iSymbols+1);//initial 0
 
   for(unsigned int k(1); k < pProbInfo->size(); ++k)
     (*pProbInfo)[k] += iUniformAdd;
 
 #ifdef DEBUG
   {
-    int iTotal = 0;
-    for(int k = 0; k < pProbInfo->size(); ++k)
+    unsigned long iTotal = 0;
+    for(unsigned int k = 0; k < pProbInfo->size(); ++k)
       iTotal += (*pProbInfo)[k];
-    DASHER_ASSERT(iTotal == m_pNCManager->GetLongParameter(LP_NORMALIZATION) - iControlSpace);
+    DASHER_ASSERT(iTotal == iNorm);
   }
 #endif
 }
@@ -484,23 +479,10 @@ void CAlphabetManager::IterateChildGroups(CAlphNode *pParent, const SGroupInfo *
     DASHER_ASSERT(pParent->GetChildren().back()==pNewChild);
   }
 
-  if (!pParentGroup) AddExtras(pParent,pCProb);
+  if (!pParentGroup) m_pNCManager->AddExtras(pParent);
   pParent->SetFlag(NF_ALLCHILDREN, true);
 }
 
-void CAlphabetManager::AddExtras(CAlphNode *pParent, vector<unsigned int> *pCProb) {
-  //control mode:
-  DASHER_ASSERT((pParent->GetChildren().back()->Hbnd() == m_pNCManager->GetLongParameter(LP_NORMALIZATION)) ^ m_pNCManager->GetBoolParameter(BP_CONTROL_MODE));
-  if (m_pNCManager->GetBoolParameter(BP_CONTROL_MODE)) {
-#ifdef _WIN32_WCE
-    DASHER_ASSERT(false);
-#endif
-    //ACL leave offset as is - like its groupnode parent, but unlike its alphnode siblings,
-    //the control node does not enter a symbol....
-    m_pNCManager->GetControlManager()->GetRoot(pParent, pParent->GetChildren().back()->Hbnd(), m_pNCManager->GetLongParameter(LP_NORMALIZATION), pParent->offset());
-  }
-}
-
 CAlphabetManager::CAlphNode::~CAlphNode() {
   delete m_pProbInfo;
   m_pMgr->m_pLanguageModel->ReleaseContext(iContext);
diff --git a/Src/DasherCore/AlphabetManager.h b/Src/DasherCore/AlphabetManager.h
index 1b95573..a587d57 100644
--- a/Src/DasherCore/AlphabetManager.h
+++ b/Src/DasherCore/AlphabetManager.h
@@ -213,10 +213,6 @@ namespace Dasher {
     virtual CDasherNode *CreateSymbolNode(CAlphNode *pParent, unsigned int iLbnd, unsigned int iHbnd, const std::string &strGroup, int iBkgCol, symbol iSymbol);
     virtual CGroupNode *CreateGroupNode(CAlphNode *pParent, unsigned int iLbnd, unsigned int iHbnd, const std::string &strEnc, int iBkgCol, const SGroupInfo *pInfo);
 
-    ///Called to add any non-alphabet (non-symbol) children to a top-level node (root or symbol).
-    /// Default is just to add the control node, if appropriate.
-    virtual void AddExtras(CAlphNode *pParent, std::vector<unsigned int> *pCProb);
-
     ///Called to compute colour for a symbol at a specified offset.
     /// Wraps CAlphabet::GetColour(sym), but (a) implements a default
     ///  scheme for symbols not specifying a colour, and (b) implements
@@ -230,7 +226,8 @@ namespace Dasher {
     const CAlphabetMap *m_pAlphabetMap;
 
   private:
-    ///Wraps m_pLanguageModel->GetProbs to implement nonuniformity & leave space for control node.
+    ///Wraps m_pLanguageModel->GetProbs to implement nonuniformity
+    /// (also leaves space for NCManager::AddExtras to add control node)
     /// Returns array of non-cumulative probs. Should this be protected and/or virtual???
     void GetProbs(std::vector<unsigned int> *pProbs, CLanguageModel::Context iContext);
     ///Constructs child nodes under the specified parent according to provided group.
diff --git a/Src/DasherCore/ConversionHelper.h b/Src/DasherCore/ConversionHelper.h
index 4e07af7..ca5aa16 100644
--- a/Src/DasherCore/ConversionHelper.h
+++ b/Src/DasherCore/ConversionHelper.h
@@ -26,8 +26,6 @@
 #include <vector>
 #include "SCENode.h"
 #include "LanguageModelling/LanguageModel.h"
-#include "LanguageModelling/PPMLanguageModel.h"
-
 
 namespace Dasher{
 	class CDasherNode;  //forward declaration
diff --git a/Src/DasherCore/ConvertingAlphMgr.cpp b/Src/DasherCore/ConvertingAlphMgr.cpp
index 033be71..e2f28e3 100644
--- a/Src/DasherCore/ConvertingAlphMgr.cpp
+++ b/Src/DasherCore/ConvertingAlphMgr.cpp
@@ -20,11 +20,14 @@ CConvertingAlphMgr::~CConvertingAlphMgr() {
   m_pConvMgr->Unref();
 }
 
-void CConvertingAlphMgr::AddExtras(CAlphNode *pParent, std::vector<unsigned int> *pCProb) {
-    //should have another probability....
-  const unsigned int i(m_pNCManager->GetAlphabet()->GetNumberTextSymbols()+1);
-  DASHER_ASSERT(pCProb->size() == i+1);
-  //ACL setting m_iOffset+1 for consistency with "proper" symbol nodes...
-  m_pConvMgr->GetRoot(pParent, (*pCProb)[i-1], (*pCProb)[i], pParent->offset()+1);
-  CAlphabetManager::AddExtras(pParent, pCProb);
+CDasherNode *CConvertingAlphMgr::CreateSymbolNode(CAlphNode *pParent, unsigned int iLbnd, unsigned int iHbnd, const string &strGroup, int iBkgCol, symbol iSymbol) {
+  int i=m_pAlphabet->GetNumberTextSymbols()+1;
+  if (iSymbol == i) {
+    vector<unsigned int> *pCProb(pParent->GetProbInfo());
+    DASHER_ASSERT(pCProb->size() == m_pAlphabet->GetNumberTextSymbols()+2);//initial 0, final conversion prob
+    //ACL setting m_iOffset+1 for consistency with "proper" symbol nodes...
+    return m_pConvMgr->GetRoot(pParent, (*pCProb)[i-1], (*pCProb)[i], pParent->offset()+1);
+  } else {
+    return CAlphabetManager::CreateSymbolNode(pParent, iLbnd, iHbnd, strGroup, iBkgCol, iSymbol);
+  }
 }
diff --git a/Src/DasherCore/ConvertingAlphMgr.h b/Src/DasherCore/ConvertingAlphMgr.h
index 2146906..d6e4501 100644
--- a/Src/DasherCore/ConvertingAlphMgr.h
+++ b/Src/DasherCore/ConvertingAlphMgr.h
@@ -14,12 +14,19 @@
 #include "ConversionManager.h"
 
 namespace Dasher {
+  //TODO Need to override CreateLanguageModel to use something appropriate for conversion.
+  // The created model, needs to have a GetSize() _including_ the conversion node
+  // (as this is not included in the Alphabet's GetNumberTextSymbols).
+  //TODO in fact IterateChildGroups will not include the conversion symbol (expected by
+  // CreateSymbolNode below) either, as it stops at Alphabet GetNumberTextSymbols too...
+  //TODO do we also need to override GetProbs? Existing impl will add uniformity onto the conversion root too.
   class CConvertingAlphMgr : public CAlphabetManager {
   public:
     CConvertingAlphMgr(CDasherInterfaceBase *pInterface, CNodeCreationManager *pNCManager, CConversionManager *pConvMgr, const CAlphInfo *pAlphabet, const CAlphabetMap *pAlphabetMap);
     virtual ~CConvertingAlphMgr();
   protected:
-    void AddExtras(CAlphNode *pParent, std::vector<unsigned int> *pCProb);
+    ///Override to return a conversion root for iSymbol==(one beyond last alphabet symbol)
+    virtual CDasherNode *CreateSymbolNode(CAlphNode *pParent, unsigned int iLbnd, unsigned int iHbnd, const std::string &strGroup, int iBkgCol, symbol iSymbol);
   private:
     CConversionManager *m_pConvMgr;
     
diff --git a/Src/DasherCore/NodeCreationManager.cpp b/Src/DasherCore/NodeCreationManager.cpp
index 725ddf6..00187af 100644
--- a/Src/DasherCore/NodeCreationManager.cpp
+++ b/Src/DasherCore/NodeCreationManager.cpp
@@ -101,12 +101,20 @@ CNodeCreationManager::~CNodeCreationManager() {
 void CNodeCreationManager::HandleEvent(CEvent *pEvent) {
   if (pEvent->m_iEventType == EV_PARAM_NOTIFY) {
     switch (static_cast<CParameterNotificationEvent *>(pEvent)->m_iParameter) {
-      case BP_CONTROL_MODE:
+      case BP_CONTROL_MODE: {
         delete m_pControlManager;
-        m_pControlManager = (GetBoolParameter(BP_CONTROL_MODE))
-          ? new CControlManager(m_pEventHandler, m_pSettingsStore, this, m_pInterface)
-          : NULL;        
+        const unsigned long iNorm(GetLongParameter(LP_NORMALIZATION));
+        unsigned long iControlSpace;
+        if (GetBoolParameter(BP_CONTROL_MODE)) {
+          m_pControlManager = new CControlManager(m_pEventHandler, m_pSettingsStore, this, m_pInterface);
+          iControlSpace = iNorm / 20;
+        } else {
+          m_pControlManager = NULL;
+          iControlSpace = 0;
+        }
+        m_iAlphNorm = iNorm-iControlSpace;
         break;
+      }
       case LP_ORIENTATION: {
         const long iOverride(GetLongParameter(LP_ORIENTATION));
         SetLongParameter(LP_REAL_ORIENTATION,
@@ -116,6 +124,20 @@ void CNodeCreationManager::HandleEvent(CEvent *pEvent) {
   }
 }
 
+
+void CNodeCreationManager::AddExtras(CDasherNode *pParent) {
+  //control mode:
+  DASHER_ASSERT(pParent->GetChildren().back()->Hbnd() == m_iAlphNorm);
+  if (GetBoolParameter(BP_CONTROL_MODE)) {
+#ifdef _WIN32_WCE
+    DASHER_ASSERT(false);
+#endif
+    //ACL leave offset as is - like its groupnode parent, but unlike its alphnode siblings,
+    //the control node does not enter a symbol....
+    m_pControlManager->GetRoot(pParent, pParent->GetChildren().back()->Hbnd(), GetLongParameter(LP_NORMALIZATION), pParent->offset());
+  }
+}
+
 void 
 CNodeCreationManager::ImportTrainingText(const std::string &strPath) {
 	m_pTrainer->LoadFile(strPath);
diff --git a/Src/DasherCore/NodeCreationManager.h b/Src/DasherCore/NodeCreationManager.h
index 7f2157b..68213c6 100644
--- a/Src/DasherCore/NodeCreationManager.h
+++ b/Src/DasherCore/NodeCreationManager.h
@@ -50,6 +50,11 @@ class CNodeCreationManager : public Dasher::CDasherComponent {
 
   void ImportTrainingText(const std::string &strPath);
 
+  unsigned long GetAlphNodeNormalization() {return m_iAlphNorm;}
+  
+  ///Called to add any non-alphabet (non-symbol) children to a top-level node (root or symbol).
+  /// Default is just to add the control node, if appropriate.
+  void AddExtras(Dasher::CDasherNode *pParent);
  private:
   Dasher::CTrainer *m_pTrainer;
   
@@ -58,13 +63,9 @@ class CNodeCreationManager : public Dasher::CDasherComponent {
   Dasher::CAlphabetManager *m_pAlphabetManager;
   Dasher::CControlManager *m_pControlManager;
   
-  ///Probability to assign to control node (0 if control mode off)
-  unsigned long m_iControlSpace;
-  ///Amount of probability space remaining, i.e. for language model to assign to letters:
-  unsigned long m_iNonUniformNorm;
-  ///Amount of probability space we will add to _each_ letter (on top of fraction of
-  /// m_iNonUniformNorm) to effect smoothing/uniformity:
-  unsigned long m_iUniformAdd;
+  ///Amount of probability space to assign to letters (language model + smoothing),
+  /// i.e. remaining after taking away whatever we need for control mode (perhaps 0)
+  unsigned long m_iAlphNorm;
 };
 /// @}
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]