[dasher] Refactoring Convert(string, SCENode**)



commit 0e232514bd9dde99e253e4d2a3a27261c06ffcf8
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Fri Apr 30 17:02:41 2010 +0100

    Refactoring Convert(string, SCENode**)
    
    Moved CTrieNode out of CPinyinParser, and GetTrieNode out of latter's Convert
    CPinyinConversionHelper::BuildTree calls PinyinParser::Convert directly,
    CPinyinConversionHelper::Convert throwing exn (should never be called)

 Src/DasherCore/PinYinConversionHelper.cpp |   12 +--
 Src/DasherCore/PinyinParser.cpp           |   75 +++++-------
 Src/DasherCore/PinyinParser.h             |  183 +++++++++++++++--------------
 3 files changed, 126 insertions(+), 144 deletions(-)
---
diff --git a/Src/DasherCore/PinYinConversionHelper.cpp b/Src/DasherCore/PinYinConversionHelper.cpp
index 270b7ab..b4be0ae 100644
--- a/Src/DasherCore/PinYinConversionHelper.cpp
+++ b/Src/DasherCore/PinYinConversionHelper.cpp
@@ -60,19 +60,11 @@ void CPinYinConversionHelper::BuildTree(CConvHNode *pRoot) {
 
   std::string strCurrentString(m_pAlphabet->GetDisplayText(pRoot->iSymbol));
 
-  SCENode *pStartTemp;
-  Convert(strCurrentString, &pStartTemp);
-
-  // Store all conversion trees (SCENode trees) in the pUserData->pSCENode
-  // of each Conversion Root.
-
-  pRoot->pSCENode = pStartTemp;
+  pRoot->pSCENode = pParser->Convert(pParser->GetTrieNode(strCurrentString));
 }
 
 bool CPinYinConversionHelper::Convert(const std::string &strSource, SCENode ** pRoot) {
-
-  *pRoot = 0;
-  return (pParser && pParser->Convert(strSource, pRoot));
+  throw "Should never be called, we've overridden BuildTree!";
 }
 
 void CPinYinConversionHelper::AssignSizes(const std::vector<SCENode *> &vChildren, Dasher::CLanguageModel::Context context, long normalization, int uniform){
diff --git a/Src/DasherCore/PinyinParser.cpp b/Src/DasherCore/PinyinParser.cpp
index 564a9bd..f21fdda 100644
--- a/Src/DasherCore/PinyinParser.cpp
+++ b/Src/DasherCore/PinyinParser.cpp
@@ -144,55 +144,44 @@ std::set<std::string> *CPinyinParser::ParseGroupName(const std::string &strName)
   }
 }
 
-
-bool CPinyinParser::Convert(const std::string &pystr, SCENode **pRoot) {
-  *pRoot = NULL;
-
-
-  //    std::cout<<"full string is "<<pystr<<std::endl;
-  
-  CTrieNode * pCurrentNode = m_pRoot;
-  CTrieNode * pCurrentChild;
-
+CTrieNode *CPinyinParser::GetTrieNode(const std::string &pystr) {
+  CTrieNode *pCurrentNode = m_pRoot;
   for(std::string::const_iterator it = pystr.begin();it!=pystr.end();++it){
-    pCurrentChild = pCurrentNode->LookupChild(*it);
+    pCurrentNode = pCurrentNode->LookupChild(*it);
     
-    if(!pCurrentChild)
-      return 0;
-    else
-      pCurrentNode = pCurrentChild;
+    if(!pCurrentNode)
+      return NULL;
   }
-  pCurrentNode = pCurrentNode->LookupChild('9');
-  if(!pCurrentNode)
-    return 0;
-  else{
-
-    if(!pCurrentNode->m_pList)
-      return 0;
+  //the digit '9' seems to be used as a marker for 'end-of-string' in the trie...
+  return pCurrentNode->LookupChild('9');
+}
 
-    *pRoot = new SCENode;
-    (*pRoot)->pszConversion = "";
+SCENode *CPinyinParser::Convert(CTrieNode *pCurrentNode) {
+  
+  if(!pCurrentNode || !pCurrentNode->m_pList)
+    return NULL;
+  
+  SCENode *pRoot = new SCENode;
+  pRoot->pszConversion = "";
 
-    for(std::set<std::string>::iterator it = pCurrentNode->m_pList->begin(); it != pCurrentNode->m_pList->end(); ++it) {
-      SCENode *pNewNode = new SCENode;
+  for(std::set<std::string>::iterator it = pCurrentNode->m_pList->begin(); it != pCurrentNode->m_pList->end(); ++it) {
+    SCENode *pNewNode = new SCENode;
         
-      pNewNode->pszConversion = new char[it->size() + 1];
-      strcpy(pNewNode->pszConversion, it->c_str());
-
-      //            std::string strChar(pNewNode->pszConversion);
-      // std::cout<<"Mandarin Char: "<<strChar<<std::endl;
-      // std::vector<int> CHSym;
-      // m_pCHAlphabet->GetSymbols(&CHSym, &strChar, 0);
-      // pNewNode->Symbol = CHSym[0];
+    pNewNode->pszConversion = new char[it->size() + 1];
+    strcpy(pNewNode->pszConversion, it->c_str());
+
+    //            std::string strChar(pNewNode->pszConversion);
+    // std::cout<<"Mandarin Char: "<<strChar<<std::endl;
+    // std::vector<int> CHSym;
+    // m_pCHAlphabet->GetSymbols(&CHSym, &strChar, 0);
+    // pNewNode->Symbol = CHSym[0];
       
-      (*pRoot)->AddChild(pNewNode);
-    }
-
-    //Test code: will make program crash
-    //    SCENode * pTemp = *pRoot;
-    // if(pTemp->GetChild()->GetChild())
-    //  std::cout<<"We have trouble in PYParser."<<std::endl;
-    return 1;    
-    
+    pRoot->AddChild(pNewNode);
   }
+
+  //Test code: will make program crash
+  //    SCENode * pTemp = *pRoot;
+  // if(pTemp->GetChild()->GetChild())
+  //  std::cout<<"We have trouble in PYParser."<<std::endl;
+  return pRoot;
 }
diff --git a/Src/DasherCore/PinyinParser.h b/Src/DasherCore/PinyinParser.h
index 1df4560..8f62229 100644
--- a/Src/DasherCore/PinyinParser.h
+++ b/Src/DasherCore/PinyinParser.h
@@ -29,6 +29,96 @@
 #include <set>
 #include <string>
 
+class CTrieNode {
+public:
+  CTrieNode(char cSymbol) {
+    m_pChild = NULL;
+    m_pNext = NULL;
+    m_pList = NULL;
+    
+    m_cSymbol = cSymbol;
+  };
+  
+  void AddChild(CTrieNode *pNewChild) {
+    pNewChild->SetNext(m_pChild);
+    m_pChild = pNewChild;
+  };
+  
+  void SetNext(CTrieNode *pNewNode) {
+    m_pNext = pNewNode;
+  };
+  
+  CTrieNode *GetNext() {
+    return m_pNext;
+  };
+  
+  char GetSymbol() {
+    return m_cSymbol;
+  };
+  
+  void SetList(std::set<std::string> *pList) {
+    m_pList = pList;
+  }
+  
+  std::set<std::string> *GetList() {
+    return m_pList;
+  }
+  
+  CTrieNode *LookupChild(char cSymbol) {
+    CTrieNode *pCurrentChild = m_pChild;
+    
+    while(pCurrentChild) {
+      if(pCurrentChild->GetSymbol() == cSymbol)
+        return pCurrentChild;
+      pCurrentChild = pCurrentChild->GetNext();
+    }
+    
+    return NULL;
+  };
+  
+  void RecursivelyDump(int iDepth) {
+    for(int i(0); i < iDepth; ++i)
+      std::cout << " ";
+    
+    std::cout << m_cSymbol << std::endl;
+    
+    CTrieNode *pCurrentChild = m_pChild;
+    
+    while(pCurrentChild) {
+      pCurrentChild->RecursivelyDump(iDepth + 1);
+      pCurrentChild = pCurrentChild->GetNext();
+    }
+  }
+  
+  void RecursiveDelete() {
+    if(m_pChild)
+      m_pChild->RecursiveDelete();
+    
+    if(m_pNext)
+      m_pNext->RecursiveDelete();
+    
+    delete this;
+  }
+  
+  //For debugging/testing
+  void PrintSymbol(){
+    if(m_cSymbol)
+      std::cout<<m_cSymbol<<std::endl;
+  }
+  
+  CTrieNode * GetChild(){
+    return m_pChild;
+  }
+  
+  std::set<std::string> *m_pList;
+  
+private:
+  CTrieNode *m_pChild;
+  CTrieNode *m_pNext;
+  
+  char m_cSymbol;
+};
+
 // TODO:
 // 1. Need a destructor
 // 2. This leaks memory badly
@@ -41,7 +131,8 @@ class CPinyinParser {
   CPinyinParser(const std::string &strAlphabetPath);
   ~CPinyinParser();
 
-  bool Convert(const std::string &pystr, SCENode **pRoot);
+  CTrieNode *GetTrieNode(const std::string &pystr);
+  SCENode *Convert(CTrieNode *pCurrentNode);
 
 
  private:
@@ -50,96 +141,6 @@ class CPinyinParser {
   
   std::set<std::string> *ParseGroupName(const std::string &strName);
 
-  class CTrieNode {
-  public:
-    CTrieNode(char cSymbol) {
-      m_pChild = NULL;
-      m_pNext = NULL;
-      m_pList = NULL;
-      
-      m_cSymbol = cSymbol;
-    };
-    
-    void AddChild(CTrieNode *pNewChild) {
-      pNewChild->SetNext(m_pChild);
-      m_pChild = pNewChild;
-    };
-    
-    void SetNext(CTrieNode *pNewNode) {
-      m_pNext = pNewNode;
-    };
-    
-    CTrieNode *GetNext() {
-      return m_pNext;
-    };
-    
-    char GetSymbol() {
-      return m_cSymbol;
-    };
-    
-    void SetList(std::set<std::string> *pList) {
-      m_pList = pList;
-    }
-    
-    std::set<std::string> *GetList() {
-      return m_pList;
-    }
-    
-    CTrieNode *LookupChild(char cSymbol) {
-      CTrieNode *pCurrentChild = m_pChild;
-      
-      while(pCurrentChild) {
-	if(pCurrentChild->GetSymbol() == cSymbol)
-	  return pCurrentChild;
-	pCurrentChild = pCurrentChild->GetNext();
-      }
-      
-      return NULL;
-    };
-        
-    void RecursivelyDump(int iDepth) {
-      for(int i(0); i < iDepth; ++i)
-	std::cout << " ";
-      
-      std::cout << m_cSymbol << std::endl;
-      
-      CTrieNode *pCurrentChild = m_pChild;
-      
-      while(pCurrentChild) {
-	pCurrentChild->RecursivelyDump(iDepth + 1);
-	pCurrentChild = pCurrentChild->GetNext();
-      }
-    }
-
-    void RecursiveDelete() {
-      if(m_pChild)
-	m_pChild->RecursiveDelete();
-      
-      if(m_pNext)
-	m_pNext->RecursiveDelete();
-
-      delete this;
-    }
-    
-    //For debugging/testing
-    void PrintSymbol(){
-      if(m_cSymbol)
-	std::cout<<m_cSymbol<<std::endl;
-    }
-
-    CTrieNode * GetChild(){
-      return m_pChild;
-    }
-    
-    std::set<std::string> *m_pList;
- 
- private:
-    CTrieNode *m_pChild;
-    CTrieNode *m_pNext;
-       
-    char m_cSymbol;
-  };
-
   std::set<std::string> *pCurrentList;
   std::string m_strCurrentGroup;
   std::string m_strLastGroup;



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