bool CDictionary::IsExist(char *sWord, int nHandle) { char sWordFind[WORD_MAXLENGTH-2]; int nPos; if(!PreProcessing(sWord, &nPos,sWordFind)) return false; return(FindInOriginalTable(nPos,sWordFind,nHandle)||FindInModifyTable(nPos,sWordFind,nHandle)); }
bool CDictionary::DelItem(char *sWord,int nHandle) { char sWordDel[WORD_MAXLENGTH-2]; int nPos,nFoundPos,nTemp; PWORD_CHAIN pPre,pTemp,pCur; if(!PreProcessing(sWord, &nPos,sWordDel)) return false; if (nPos < 0) return false; if(FindInOriginalTable(nPos,sWordDel,nHandle,&nFoundPos)) { if(!m_pModifyTable)//Not prepare the buffer { m_pModifyTable=new MODIFY_TABLE[CC_NUM]; memset(m_pModifyTable,0,CC_NUM*sizeof(MODIFY_TABLE)); } m_IndexTable[nPos].pWordItemHead[nFoundPos].nFrequency=-1; m_pModifyTable[nPos].nDelete+=1; if(nHandle==-1)//Remove all items which word is sWordDel,ignoring the handle { /* nTemp=nFoundPos-1;//Check its previous position while(nTemp>0&&strcmp(m_IndexTable[nPos].pWordItemHead[nFoundPos].sWord,sWordDel)==0) { m_IndexTable[nPos].pWordItemHead[nTemp].nFrequency=-1; m_pModifyTable[nPos].nDelete+=1; nTemp-=1; } */ nTemp=nFoundPos+1;//Check its previous position while(nTemp<m_IndexTable[nPos].nCount&&strcmp(m_IndexTable[nPos].pWordItemHead[nFoundPos].sWord,sWordDel)==0) { m_IndexTable[nPos].pWordItemHead[nTemp].nFrequency=-1; m_pModifyTable[nPos].nDelete+=1; nTemp+=1; } } return true; } //Operation in the modify table and its items if(FindInModifyTable(nPos,sWordDel,nHandle,&pPre)) { pCur=m_pModifyTable[nPos].pWordItemHead; if(pPre!=NULL) pCur=pPre->next; while(pCur!=NULL && _stricmp(pCur->data.sWord,sWordDel)==0&&(pCur->data.nHandle==nHandle||nHandle<0)) { pTemp=pCur; if(pPre!=NULL)//pCur is the first item pPre->next=pCur->next; else m_pModifyTable[nPos].pWordItemHead=pCur->next; pCur=pCur->next; delete pTemp->data.sWord;//Delete the word delete pTemp; } return true; } return false; }
bool CDictionary::GetHandle(char *sWord,int *pnCount,int *pnHandle,int *pnFrequency) { char sWordGet[WORD_MAXLENGTH-2]; int nPos,nFoundPos,nTemp; PWORD_CHAIN pPre,pCur; *pnCount=0; if(!PreProcessing(sWord, &nPos,sWordGet)) return false; if (nPos < 0) return false; if(FindInOriginalTable(nPos,sWordGet,-1,&nFoundPos)) { pnHandle[*pnCount]=m_IndexTable[nPos].pWordItemHead[nFoundPos].nHandle; pnFrequency[*pnCount]=m_IndexTable[nPos].pWordItemHead[nFoundPos].nFrequency; *pnCount+=1; /* nTemp=nFoundPos-1;//Check its previous position while(nTemp>0&&strcmp(m_IndexTable[nPos].pWordItemHead[nTemp].sWord,sWordGet)==0) { pnHandle[*pnCount]=m_IndexTable[nPos].pWordItemHead[nTemp].nHandle; pnFrequency[*pnCount]=m_IndexTable[nPos].pWordItemHead[nTemp].nFrequency; *pnCount+=1; nTemp-=1; } */ nTemp=nFoundPos+1;//Check its previous position while(nTemp<m_IndexTable[nPos].nCount&&strcmp(m_IndexTable[nPos].pWordItemHead[nTemp].sWord,sWordGet)==0) { pnHandle[*pnCount]=m_IndexTable[nPos].pWordItemHead[nTemp].nHandle; pnFrequency[*pnCount]=m_IndexTable[nPos].pWordItemHead[nTemp].nFrequency; *pnCount+=1; nTemp+=1; } return true; } //Operation in the index table and its items if(FindInModifyTable(nPos,sWordGet,-1,&pPre)) { pCur=m_pModifyTable[nPos].pWordItemHead; if(pPre!=NULL) pCur=pPre->next; while(pCur!=NULL && _stricmp(pCur->data.sWord,sWordGet)==0) { pnHandle[*pnCount]=pCur->data.nHandle; pnFrequency[*pnCount]=pCur->data.nFrequency; *pnCount+=1; pCur=pCur->next; } return true; } return false; }
int CDictionary::GetFrequency(char *sWord, int nHandle) { char sWordFind[WORD_MAXLENGTH-2]; int nPos,nIndex; PWORD_CHAIN pFound; if(!PreProcessing(sWord, &nPos,sWordFind)) return 0; if(FindInOriginalTable(nPos,sWordFind,nHandle,&nIndex)) { return m_IndexTable[nPos].pWordItemHead[nIndex].nFrequency; } if(FindInModifyTable(nPos,sWordFind,nHandle,&pFound)) { return pFound->data.nFrequency; } return 0; }
/********************************************************************* * * Func Name : GetMaxMatch * * Description: Get the max match to the word * * * Parameters : nHandle: the only handle which will be attached to the word * Returns : success or fail * Author : Kevin Zhang * History : * 1.create 2002-1-21 *********************************************************************/ bool CDictionary::GetMaxMatch(char *sWord, char *sWordRet,int *npHandleRet) { char sWordGet[WORD_MAXLENGTH-2],sFirstChar[3]; int nPos,nFoundPos,nTemp; PWORD_CHAIN pCur; *npHandleRet=-1; if(!PreProcessing(sWord, &nPos,sWordGet)) return false; if (nPos < 0) return false; sWordRet[0]=0; strncpy(sFirstChar,sWord,strlen(sWord)-strlen(sWordGet));//Get the first char sFirstChar[strlen(sWord)-strlen(sWordGet)]=0;//Set the end flag FindInOriginalTable(nPos,sWordGet,-1,&nFoundPos); nTemp=nFoundPos;//Check its previous position if(nFoundPos==-1) nTemp=0; while(nTemp<m_IndexTable[nPos].nCount&&CC_Find(m_IndexTable[nPos].pWordItemHead[nTemp].sWord,sWordGet)!=m_IndexTable[nPos].pWordItemHead[nTemp].sWord) {//Get the next nTemp+=1; } if(nTemp<m_IndexTable[nPos].nCount&&CC_Find(m_IndexTable[nPos].pWordItemHead[nTemp].sWord,sWordGet)==m_IndexTable[nPos].pWordItemHead[nTemp].sWord) { strcpy(sWordRet,sFirstChar); strcat(sWordRet,m_IndexTable[nPos].pWordItemHead[nTemp].sWord); *npHandleRet=m_IndexTable[nPos].pWordItemHead[nTemp].nHandle; return true; }//Cannot get the item and retrieve the modified data if exists //Operation in the index table and its items if(m_pModifyTable&&m_pModifyTable[nPos].pWordItemHead)//Exists pCur=m_pModifyTable[nPos].pWordItemHead; else pCur=NULL; while(pCur!=NULL&&strcmp(pCur->data.sWord,sWordGet)<=0&&CC_Find(pCur->data.sWord,sWordGet)!=pCur->data.sWord)// { pCur=pCur->next; } if(pCur!=NULL&&CC_Find(pCur->data.sWord,sWordGet)!=pCur->data.sWord) {//Get it strcpy(sWordRet,sFirstChar); strcat(sWordRet,pCur->data.sWord); *npHandleRet=pCur->data.nHandle; return true; } return false; }
bool CDictionary::AddItem(char *sWord, int nHandle,int nFrequency) { char sWordAdd[WORD_MAXLENGTH-2]; int nPos,nFoundPos; PWORD_CHAIN pRet,pTemp,pNext; int i=0; if(!PreProcessing(sWord, &nPos,sWordAdd,true)) return false; if(FindInOriginalTable(nPos,sWordAdd,nHandle,&nFoundPos)) {//The word exists in the original table, so add the frequency //Operation in the index table and its items if(m_IndexTable[nPos].pWordItemHead[nFoundPos].nFrequency==-1) {//The word item has been removed m_IndexTable[nPos].pWordItemHead[nFoundPos].nFrequency=nFrequency; if(!m_pModifyTable)//Not prepare the buffer { m_pModifyTable=new MODIFY_TABLE[CC_NUM]; memset(m_pModifyTable,0,CC_NUM*sizeof(MODIFY_TABLE)); } m_pModifyTable[nPos].nDelete-=1; } else m_IndexTable[nPos].pWordItemHead[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(!m_pModifyTable)//Not prepare the buffer { m_pModifyTable=new MODIFY_TABLE[CC_NUM]; memset(m_pModifyTable,0,CC_NUM*sizeof(MODIFY_TABLE)); } if(FindInModifyTable(nPos,sWordAdd,nHandle,&pRet)) { if(pRet!=NULL) pRet=pRet->next; else pRet=m_pModifyTable[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 WORD_CHAIN;//Allocate the word chain node if(pTemp==NULL)//Allocate memory failure return false; memset(pTemp,0,sizeof(WORD_CHAIN));//init it with 0 pTemp->data.nHandle=nHandle;//store the handle pTemp->data.nWordLen=strlen(sWordAdd); pTemp->data.sWord=new char[1+pTemp->data.nWordLen]; strcpy(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=m_pModifyTable[nPos].pWordItemHead; m_pModifyTable[nPos].pWordItemHead=pTemp;//Set the pAdd as the head node } pTemp->next=pNext;//Very important!!!! or else it will lose some node //Modify in 2001-10-29 m_pModifyTable[nPos].nCount++;//the number increase by one return true; }