//==================================================================== // 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); }
private void MergeAndSaveIndexTableItem(BinaryWriter writer, IndexTableItem item, ModifyTableItem modifyItem) { int j, nCount; //频率、词长、读取词性 WordChain pCur; //计算修改后有效词块的数目 nCount = item.nCount + modifyItem.nCount - modifyItem.nDelete; writer.Write(nCount); pCur = modifyItem.pWordItemHead; j = 0; //对原表中的词块和修改表中的词块进行遍历,并把修改后的添加到原表中 while (pCur != null && j < item.nCount) { //如果修改表中的词小于原表中对应位置的词或者长度相等但nHandle值比原表中的小,则把修改表中的写入到词典文件当中. if (Utility.CCStringCompare(pCur.data.sWord, item.WordItems[j].sWord) < 0 || ((pCur.data.sWord == item.WordItems[j].sWord) && (pCur.data.nPOS < item.WordItems[j].nPOS))) { //Output the modified data to the file SaveWordItem(writer, pCur.data); pCur = pCur.next; } //频度nFrequecy等于-1说明该词已被删除,跳过它 else if (item.WordItems[j].nFrequency == -1) { j++; } //如果修改表中的词长度比原表中的长度大或 长度相等但句柄值要多,就把原表的词写入的词典文件中 else if (Utility.CCStringCompare(pCur.data.sWord, item.WordItems[j].sWord) > 0 || ((pCur.data.sWord == item.WordItems[j].sWord) && (pCur.data.nPOS > item.WordItems[j].nPOS))) { //Output the index table data to the file SaveWordItem(writer, item.WordItems[j]); j++; } } //如果归并结束后indexTable有剩余,则继续写完indexTable中的数据 if (j < item.nCount) { for (int i = j; i < item.nCount; i++) { if (item.WordItems[j].nFrequency != -1) { SaveWordItem(writer, item.WordItems[i]); } } } //否则继续写完modifyTable中的数据 else { while (pCur != null) { //Output the modified data to the file SaveWordItem(writer, pCur.data); pCur = pCur.next; } } }
private void MergeAndSaveIndexTableItem(BinaryWriter writer, IndexTableItem item, ModifyTableItem modifyItem) { int j, nCount; //频率、词长、读取词性 WordChain pCur; //计算修改后有效词块的数目 nCount = item.nCount + modifyItem.nCount - modifyItem.nDelete; writer.Write(nCount); pCur = modifyItem.pWordItemHead; j = 0; //对原表中的词块和修改表中的词块进行遍历,并把修改后的添加到原表中 while (pCur != null && j < item.nCount) { //如果修改表中的词小于原表中对应位置的词或者长度相等但nHandle值比原表中的小,则把修改表中的写入到词典文件当中. if (Utility.CCStringCompare(pCur.data.sWord, item.WordItems[j].sWord) < 0 || ((pCur.data.sWord == item.WordItems[j].sWord) && (pCur.data.nPOS < item.WordItems[j].nPOS))) { //Output the modified data to the file SaveWordItem(writer, pCur.data); pCur = pCur.next; } //频度nFrequecy等于-1说明该词已被删除,跳过它 else if (item.WordItems[j].nFrequency == -1) j++; //如果修改表中的词长度比原表中的长度大或 长度相等但句柄值要多,就把原表的词写入的词典文件中 else if (Utility.CCStringCompare(pCur.data.sWord, item.WordItems[j].sWord) > 0 || ((pCur.data.sWord == item.WordItems[j].sWord) && (pCur.data.nPOS > item.WordItems[j].nPOS))) { //Output the index table data to the file SaveWordItem(writer, item.WordItems[j]); j++; } } //如果归并结束后indexTable有剩余,则继续写完indexTable中的数据 if (j < item.nCount) { for (int i = j; i < item.nCount; i++) if (item.WordItems[j].nFrequency != -1) SaveWordItem(writer, item.WordItems[i]); } //否则继续写完modifyTable中的数据 else while (pCur != null) { //Output the modified data to the file SaveWordItem(writer, pCur.data); pCur = pCur.next; } }
//==================================================================== // 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; }
private void MergeAndSaveIndexTableItem(BinaryWriter writer, IndexTableItem item, ModifyTableItem modifyItem) { int j, nCount; //Ƶ�ʡ��ʳ�����ȡ���� WordChain pCur; //�����ĺ���Ч�ʿ����Ŀ nCount = item.nCount + modifyItem.nCount - modifyItem.nDelete; writer.Write(nCount); pCur = modifyItem.pWordItemHead; j = 0; //��ԭ���еĴʿ���ı��еĴʿ���б���,�����ĺ����ӵ�ԭ���� while (pCur != null && j < item.nCount) { //����ı��еĴ�С��ԭ���ж�Ӧλ�õĴʻ��߳�����ȵ�nHandleֵ��ԭ���е�С,����ı��е�д�뵽�ʵ��ļ�����. if (Utility.CCStringCompare(pCur.data.sWord, item.WordItems[j].sWord) < 0 || ((pCur.data.sWord == item.WordItems[j].sWord) && (pCur.data.nPOS < item.WordItems[j].nPOS))) { //Output the modified data to the file SaveWordItem(writer, pCur.data); pCur = pCur.next; } //Ƶ��nFrequecy����-1˵���ô��ѱ�ɾ��,������ else if (item.WordItems[j].nFrequency == -1) j++; //����ı��еĴʳ��ȱ�ԭ���еij��ȴ�� ������ȵ����ֵҪ��,�Ͱ�ԭ��Ĵ�д��Ĵʵ��ļ��� else if (Utility.CCStringCompare(pCur.data.sWord, item.WordItems[j].sWord) > 0 || ((pCur.data.sWord == item.WordItems[j].sWord) && (pCur.data.nPOS > item.WordItems[j].nPOS))) { //Output the index table data to the file SaveWordItem(writer, item.WordItems[j]); j++; } } //����鲢������indexTable��ʣ�࣬�����д��indexTable�е����� if (j < item.nCount) { for (int i = j; i < item.nCount; i++) if (item.WordItems[j].nFrequency != -1) SaveWordItem(writer, item.WordItems[i]); } //�������д��modifyTable�е����� else while (pCur != null) { //Output the modified data to the file SaveWordItem(writer, pCur.data); pCur = pCur.next; } }