[dasher] First try at learn-as-you-write for MandarinDasher



commit 50e6533c0ea8514b639f339a35691fd716e3442b
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Thu Feb 10 20:18:10 2011 +0000

    First try at learn-as-you-write for MandarinDasher
    
    Training data not written to user file, so on per-session basis only
    Similar problems with context switches as standard (AlphabetManager)
    
    CConvRoot stores m_iPYsym rather than m_pConversions
    AlphabetManager code rewritten to check NF_COMMITTED not already set.

 Src/DasherCore/AlphabetManager.cpp |    8 +++-----
 Src/DasherCore/MandarinAlphMgr.cpp |   30 +++++++++++++++---------------
 Src/DasherCore/MandarinAlphMgr.h   |   14 ++++++--------
 3 files changed, 24 insertions(+), 28 deletions(-)
---
diff --git a/Src/DasherCore/AlphabetManager.cpp b/Src/DasherCore/AlphabetManager.cpp
index 21bdf51..cfe4b72 100644
--- a/Src/DasherCore/AlphabetManager.cpp
+++ b/Src/DasherCore/AlphabetManager.cpp
@@ -540,11 +540,9 @@ CDasherNode *CAlphabetManager::CAlphNode::RebuildParent(int iNewOffset) {
 // TODO: Shouldn't there be an option whether or not to learn as we write?
 // For want of a better solution, game mode exemption explicit in this function
 void CAlphabetManager::CSymbolNode::SetFlag(int iFlag, bool bValue) {
-  CDasherNode::SetFlag(iFlag, bValue);
-  switch(iFlag) {
-  case NF_COMMITTED:
-    if(bValue && !GetFlag(NF_GAME) && m_pMgr->m_pInterface->GetBoolParameter(BP_LM_ADAPTIVE))
+  if (iFlag==NF_COMMITTED && bValue && !GetFlag(NF_COMMITTED)
+      && !GetFlag(NF_GAME) && m_pMgr->m_pInterface->GetBoolParameter(BP_LM_ADAPTIVE)) {
       m_pMgr->m_pLanguageModel->LearnSymbol(m_pMgr->m_iLearnContext, iSymbol);
-    break;
   }
+  CDasherNode::SetFlag(iFlag, bValue);
 }
diff --git a/Src/DasherCore/MandarinAlphMgr.cpp b/Src/DasherCore/MandarinAlphMgr.cpp
index 1f35f34..153da25 100644
--- a/Src/DasherCore/MandarinAlphMgr.cpp
+++ b/Src/DasherCore/MandarinAlphMgr.cpp
@@ -169,26 +169,27 @@ CMandarinAlphMgr::CConvRoot *CMandarinAlphMgr::CreateConvRoot(CAlphNode *pParent
   
   // the same offset as we've still not entered/selected a symbol (leaf);
   // Colour is always 9 so ignore iBkgCol
-  CConvRoot *pConv = new CConvRoot(pParent, pParent->offset(), iLbnd, iHbnd, strGroup, this, &m_pConversionsBySymbol[iPYsym]);
-  
+  CConvRoot *pConv = new CConvRoot(pParent, pParent->offset(), iLbnd, iHbnd, strGroup, this, iPYsym);
+    
   // and use the same context too (pinyin syll+tone is _not_ used as part of the LM context)
   pConv->iContext = m_pLanguageModel->CloneContext(pParent->iContext);
   return pConv;
 }
 
-CMandarinAlphMgr::CConvRoot::CConvRoot(CDasherNode *pParent, int iOffset, unsigned int iLbnd, unsigned int iHbnd, const std::string &strGroup, CMandarinAlphMgr *pMgr, const set<symbol> *pConversions)
-: CDasherNode(pParent, iOffset, iLbnd, iHbnd, 9, strGroup), m_pMgr(pMgr), m_pConversions(pConversions) {
-  DASHER_ASSERT(pConversions && pConversions->size()>1);
+CMandarinAlphMgr::CConvRoot::CConvRoot(CDasherNode *pParent, int iOffset, unsigned int iLbnd, unsigned int iHbnd, const std::string &strGroup, CMandarinAlphMgr *pMgr, symbol pySym)
+: CDasherNode(pParent, iOffset, iLbnd, iHbnd, 9, strGroup), m_pMgr(pMgr), m_pySym(pySym) {
+  DASHER_ASSERT(m_pMgr->m_pConversionsBySymbol[pySym].size()>1);
   //colour + label from ConversionManager.
 }
 
 int CMandarinAlphMgr::CConvRoot::ExpectedNumChildren() {
-  return m_pConversions->size();
+  return m_pMgr->m_pConversionsBySymbol[m_pySym].size();
 }
 
 void CMandarinAlphMgr::CConvRoot::PopulateChildren() {
   if (m_vChInfo.empty()) {
-    for(set<symbol>::const_iterator it = m_pConversions->begin(); it != m_pConversions->end(); ++it) {
+    const set<symbol> &convs(m_pMgr->m_pConversionsBySymbol[m_pySym]);
+    for(set<symbol>::const_iterator it = convs.begin(); it != convs.end(); ++it) {
       m_vChInfo.push_back(std::pair<symbol, unsigned int>(*it,0));
     }
     //ACL I think it's a good idea to keep those in a consistent order - symbol order will do nicely
@@ -223,6 +224,13 @@ CMandarinAlphMgr::CMandSym *CMandarinAlphMgr::CreateCHSymbol(CDasherNode *pParen
   return pNewNode;
 }
 
+void CMandarinAlphMgr::CConvRoot::SetFlag(int iFlag, bool bValue) {
+  if (iFlag==NF_COMMITTED && bValue && !GetFlag(NF_COMMITTED))
+    if (!GetFlag(NF_GAME) && m_pMgr->m_pNCManager->GetBoolParameter(BP_LM_ADAPTIVE))
+      static_cast<CPPMPYLanguageModel *>(m_pMgr->m_pLanguageModel)->LearnPYSymbol(m_pMgr->m_iLearnContext, m_pySym);
+  CDasherNode::SetFlag(iFlag,bValue);
+}
+
 void CMandarinAlphMgr::AssignSizes(std::vector<pair<symbol,unsigned int> > &vChildren, Dasher::CLanguageModel::Context context) {
 
   const uint64 iNorm(m_pNCManager->GetLongParameter(LP_NORMALIZATION));
@@ -305,14 +313,6 @@ CMandarinAlphMgr::CMandSym::CMandSym(CDasherNode *pParent, int iOffset, unsigned
 : CSymbolNode(pParent, iOffset, iLbnd, iHbnd, pMgr->GetCHColour(iSymbol,iOffset), strGroup+pMgr->m_pCHAlphabet->GetDisplayText(iSymbol), pMgr, iSymbol) {
 }
 
-void CMandarinAlphMgr::CMandSym::SetFlag(int iFlag, bool bValue) {
-  //``disable learn-as-you-write for Mandarin Dasher''
-   if (iFlag==NF_COMMITTED)
-     CDasherNode::SetFlag(iFlag, bValue); //bypass CAlphNode setter!
-  else
-      CAlphNode::SetFlag(iFlag, bValue);
-}
-
 const std::string &CMandarinAlphMgr::CMandSym::outputText() {
   //use chinese, not pinyin, alphabet...
   return mgr()->m_pCHAlphabet->GetText(iSymbol);
diff --git a/Src/DasherCore/MandarinAlphMgr.h b/Src/DasherCore/MandarinAlphMgr.h
index cc2ed14..881923f 100644
--- a/Src/DasherCore/MandarinAlphMgr.h
+++ b/Src/DasherCore/MandarinAlphMgr.h
@@ -49,17 +49,14 @@ namespace Dasher {
     
   protected:
     ///Subclass of CSymbolNode for (converted) chinese-alphabet symbols:
-    /// these (a) disable learn-as-you-write (not supported in Mandarin Dasher),
-    /// (b) use the chinese (not pinyin, as CSymbolNode would) alphabet for text to display/enter
-    /// (c) determine their colour using GetCHColour rather than GetColour.
+    /// these use the chinese alphabet in place of the pinyin one for text to display/enter,
+    /// and get their colour using GetCHColour rather than GetColour.
     class CMandSym : public CSymbolNode {
     public:
       CMandarinAlphMgr *mgr() {return static_cast<CMandarinAlphMgr *>(CSymbolNode::mgr());}
       ///Symbol constructor: display text from CHAlphabet, colour from GetCHColour
       /// \param strGroup caption of any group(s) containing this symbol for which no nodes created; prepended to display text.
       CMandSym(CDasherNode *pParent, int iOffset, unsigned int iLbnd, unsigned int iHbnd, const std::string &strGroup, CMandarinAlphMgr *pMgr, symbol iSymbol);
-      ///Disable learn-as-you-write
-      virtual void SetFlag(int iFlag, bool bValue);
       ///Rebuilding not supported
       virtual CDasherNode *RebuildParent() {return 0;}
     private:
@@ -71,15 +68,16 @@ namespace Dasher {
     class CConvRoot : public CDasherNode {
     public:
       CMandarinAlphMgr *mgr() {return m_pMgr;}
-      /// \param pConversions set of chinese-alphabet symbol numbers that the PY can convert to; must have >1 element.
-      CConvRoot(CDasherNode *pParent, int iOffset, unsigned int iLbnd, unsigned int iHbnd, const std::string &strGroup, CMandarinAlphMgr *pMgr, const std::set<symbol> *pConversions);
+      /// \param pySym symbol in pinyin alphabet; must have >1 possible chinese conversion.
+      CConvRoot(CDasherNode *pParent, int iOffset, unsigned int iLbnd, unsigned int iHbnd, const std::string &strGroup, CMandarinAlphMgr *pMgr, symbol pySym);
       void PopulateChildren();
       int ExpectedNumChildren();
       CLanguageModel::Context iContext;
+      void SetFlag(int iFlag, bool bValue);
     private:        
       std::vector<std::pair<symbol, unsigned int> > m_vChInfo;
       CMandarinAlphMgr *m_pMgr;
-      const std::set<symbol> *m_pConversions;
+      symbol m_pySym;
     };
     ///Called to create the node for a pinyin leaf symbol;
     /// Overridden to call either CreateConvRoot or CreateCHSymbol, according to #chinese symbols under specified pinyin



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