//==================================================================== // Func Name : FindInModifyTable // Description: judge the word and handle exist in the modified table and its items // Parameters : nInnerCode: the inner code of the first CHines char // sWord: the word // nHandle:the handle number // *pFindRet: the node found // Returns : success or fail //==================================================================== private bool FindInModifyTable(int nInnerCode, string sWord, out WordChain pFindRet) { WordChain pCur, pPre; if (modifyTable != null) { pCur = modifyTable[nInnerCode].pWordItemHead; pPre = null; while (pCur != null && (Utility.CCStringCompare(pCur.data.sWord, sWord) < 0)) { pPre = pCur; pCur = pCur.next; } pFindRet = pPre; if (pCur != null && string.Compare(pCur.data.sWord, sWord, true) == 0) { return(true); } else { return(false); } } pFindRet = null; return(false); }
//==================================================================== // Func Name : FindInModifyTable // Description: judge the word and handle exist in the modified table and its items // Parameters : nInnerCode: the inner code of the first CHines char // sWord: the word // nHandle:the handle number // *pFindRet: the node found // Returns : success or fail //==================================================================== private bool FindInModifyTable(int nInnerCode, string sWord, int nPOS, out WordChain pFindRet) { WordChain pCur, pPre; if (modifyTable != null) { pCur = modifyTable[nInnerCode].pWordItemHead; pPre = null; while (pCur != null && (Utility.CCStringCompare(pCur.data.sWord, sWord) < 0 || (string.Compare(pCur.data.sWord, sWord, true) == 0 && pCur.data.nPOS < nPOS))) //sort the link chain as alphabet { pPre = pCur; pCur = pCur.next; } pFindRet = pPre; if (pCur != null && string.Compare(pCur.data.sWord, sWord, true) == 0 && pCur.data.nPOS == nPOS) { //The node exists, delete the node and return return(true); } else { return(false); } } pFindRet = null; return(false); }
//==================================================================== // Func Name : AddItem // Description: Add a word item to the dictionary // Parameters : sWord: the word // nHandle:the handle number // nFrequency: the frequency // Returns : success or fail //==================================================================== public bool AddItem(string sWord, int nPOS, int nFrequency) { int nPos, nFoundPos; WordChain pRet, pTemp, pNext; string sWordAdd; //预处理,去掉词的前后的空格 if (!PreProcessing(ref sWord, out nPos, out sWordAdd)) { return(false); } if (FindInOriginalTable(nPos, sWordAdd, nPOS, out nFoundPos)) { //The word exists in the original table, so add the frequency //Operation in the index table and its items if (indexTable[nPos].WordItems[nFoundPos].nFrequency == -1) { //The word item has been removed indexTable[nPos].WordItems[nFoundPos].nFrequency = nFrequency; if (modifyTable == null) { modifyTable = new ModifyTableItem[Predefine.CC_NUM]; } modifyTable[nPos].nDelete -= 1; } else { indexTable[nPos].WordItems[nFoundPos].nFrequency += nFrequency; } return(true); } //The items not exists in the index table. //As following, we have to find the item whether exists in the modify data region //If exists, change the frequency .or else add a item if (modifyTable == null) { modifyTable = new ModifyTableItem[Predefine.CC_NUM]; for (int i = 0; i < Predefine.CC_NUM; i++) { modifyTable[i] = new ModifyTableItem(); } } if (FindInModifyTable(nPos, sWordAdd, nPOS, out pRet)) { if (pRet != null) { pRet = pRet.next; } else { pRet = modifyTable[nPos].pWordItemHead; } pRet.data.nFrequency += nFrequency; return(true); } //find the proper position to add the word to the modify data table and link pTemp = new WordChain(); //Allocate the word chain node pTemp.data = new WordItem(); pTemp.data.nPOS = nPOS; //store the handle pTemp.data.nWordLen = Utility.GetWordLength(sWordAdd); pTemp.data.sWord = sWordAdd; pTemp.data.nFrequency = nFrequency; pTemp.next = null; if (pRet != null) { pNext = pRet.next; //Get the next item before the current item pRet.next = pTemp; //link the node to the chain } else { pNext = modifyTable[nPos].pWordItemHead; modifyTable[nPos].pWordItemHead = pTemp; //Set the pAdd as the head node } pTemp.next = pNext; //Very important!!!! or else it will lose some node modifyTable[nPos].nCount++; //the number increase by one return(true); }
//==================================================================== // Func Name : FindInModifyTable // Description: judge the word and handle exist in the modified table and its items // Parameters : nInnerCode: the inner code of the first CHines char // sWord: the word // nHandle:the handle number // *pFindRet: the node found // Returns : success or fail //==================================================================== private bool FindInModifyTable(int nInnerCode, string sWord, int nPOS, out WordChain pFindRet) { WordChain pCur, pPre; if (modifyTable != null) { pCur = modifyTable[nInnerCode].pWordItemHead; pPre = null; while (pCur != null && (Utility.CCStringCompare(pCur.data.sWord, sWord) < 0 || (string.Compare(pCur.data.sWord, sWord, true) == 0 && pCur.data.nPOS < nPOS))) //sort the link chain as alphabet { pPre = pCur; pCur = pCur.next; } pFindRet = pPre; if (pCur != null && string.Compare(pCur.data.sWord, sWord, true) == 0 && pCur.data.nPOS == nPOS) //The node exists, delete the node and return return true; else return false; } pFindRet = null; return false; }
//==================================================================== // Func Name : AddItem // Description: Add a word item to the dictionary // Parameters : sWord: the word // nHandle:the handle number // nFrequency: the frequency // Returns : success or fail //==================================================================== public bool AddItem(string sWord, int nPOS, int nFrequency) { int nPos, nFoundPos; WordChain pRet, pTemp, pNext; string sWordAdd; //预处理,去掉词的前后的空格 if (!PreProcessing(ref sWord, out nPos, out sWordAdd)) return false; if (FindInOriginalTable(nPos, sWordAdd, nPOS, out nFoundPos)) { //The word exists in the original table, so add the frequency //Operation in the index table and its items if (indexTable[nPos].WordItems[nFoundPos].nFrequency == -1) { //The word item has been removed indexTable[nPos].WordItems[nFoundPos].nFrequency = nFrequency; if (modifyTable == null) modifyTable = new ModifyTableItem[Predefine.CC_NUM]; modifyTable[nPos].nDelete -= 1; } else indexTable[nPos].WordItems[nFoundPos].nFrequency += nFrequency; return true; } //The items not exists in the index table. //As following, we have to find the item whether exists in the modify data region //If exists, change the frequency .or else add a item if (modifyTable == null) { modifyTable = new ModifyTableItem[Predefine.CC_NUM]; for (int i = 0; i < Predefine.CC_NUM; i++) modifyTable[i] = new ModifyTableItem(); } if (FindInModifyTable(nPos, sWordAdd, nPOS, out pRet)) { if (pRet != null) pRet = pRet.next; else pRet = modifyTable[nPos].pWordItemHead; pRet.data.nFrequency += nFrequency; return true; } //find the proper position to add the word to the modify data table and link pTemp = new WordChain(); //Allocate the word chain node pTemp.data = new WordItem(); pTemp.data.nPOS = nPOS; //store the handle pTemp.data.nWordLen = Utility.GetWordLength(sWordAdd); pTemp.data.sWord = sWordAdd; pTemp.data.nFrequency = nFrequency; pTemp.next = null; if (pRet != null) { pNext = pRet.next; //Get the next item before the current item pRet.next = pTemp; //link the node to the chain } else { pNext = modifyTable[nPos].pWordItemHead; modifyTable[nPos].pWordItemHead = pTemp; //Set the pAdd as the head node } pTemp.next = pNext; //Very important!!!! or else it will lose some node modifyTable[nPos].nCount++; //the number increase by one return true; }
//==================================================================== // Func Name : FindInModifyTable // Description: judge the word and handle exist in the modified table and its items // Parameters : nInnerCode: the inner code of the first CHines char // sWord: the word // nHandle:the handle number // *pFindRet: the node found // Returns : success or fail //==================================================================== private bool FindInModifyTable(int nInnerCode, string sWord, out WordChain pFindRet) { WordChain pCur, pPre; if (modifyTable != null) { pCur = modifyTable[nInnerCode].pWordItemHead; pPre = null; while (pCur != null && (Utility.CCStringCompare(pCur.data.sWord, sWord) < 0)) { pPre = pCur; pCur = pCur.next; } pFindRet = pPre; if (pCur != null && string.Compare(pCur.data.sWord, sWord, true) == 0) return true; else return false; } pFindRet = null; return false; }