-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContextStat.cpp
267 lines (251 loc) · 8.15 KB
/
ContextStat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//////////////////////////////////////////////////////////////////////
//ICTCLAS简介:计算所汉语词法分析系统ICTCLAS(Institute of Computing Technology, Chinese Lexical Analysis System),
// 功能有:中文分词;词性标注;未登录词识别。
// 分词正确率高达97.58%(973专家评测结果),
// 未登录词识别召回率均高于90%,其中中国人名的识别召回率接近98%;
// 处理速度为31.5Kbytes/s。
//著作权: Copyright?2002-2005中科院计算所 职务著作权人:张华平 刘群
//遵循协议:自然语言处理开放资源许可证1.0
//Email: zhanghp@software.ict.ac.cn
//Homepage:www.nlp.org.cn;mtgroup.ict.ac.cn
/****************************************************************************
*
* Copyright (c) 2000, 2001
* Machine Group
* Software Research Lab.
* Institute of Computing Tech.
* Chinese Academy of Sciences
* All rights reserved.
*
* This file is the confidential and proprietary property of
* Institute of Computing Tech. and the posession or use of this file requires
* a written license from the author.
* Filename: ContextStat.cpp
* Abstract:
* implementation of the CContextStat class.
* Author: Kevin Zhang
* (zhanghp@software.ict.ac.cn)
* Date: 2002-1-24
*
* Notes:
*
****************************************************************************/
#include "ContextStat.h"
#include "Utility.h"
#include <memory.h>
#include <stdio.h>
#include <string.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CContextStat::CContextStat()
{
m_pSymbolTable=0;//new buffer for symbol
m_pContext=0;//init with empty
}
CContextStat::~CContextStat()
{
delete [] m_pSymbolTable;
PMYCONTEXT pCur=m_pContext,pTemp;
while(pCur!=NULL)
{//delete the context array
pTemp=pCur->next;
for(int i=0;i<m_nTableLen;i++)
delete [] pCur->aContextArray[i];
delete [] pCur->aContextArray;
delete [] pCur->aTagFreq;
delete pCur;
pCur=pTemp;
}
}
bool CContextStat::SetSymbol(int *nSymbol)
{
memcpy(m_pSymbolTable,nSymbol,sizeof(int)*m_nTableLen);
return true;
}
bool CContextStat::Add(int nKey, int nPrevSymbol, int nCurSymbol, int nFrequency)
{//Add the context symbol to the array
PMYCONTEXT pRetItem,pNew;
int nPrevIndex,nCurIndex;
if(!GetItem(nKey,&pRetItem))//Not get it
{
pNew=new MYCONTEXT;
pNew->nKey=nKey;
pNew->nTotalFreq=0;
pNew->next=NULL;
pNew->aContextArray=new int*[m_nTableLen];
pNew->aTagFreq=new int[m_nTableLen];
memset(pNew->aTagFreq,0,sizeof(int)*m_nTableLen);
for(int i=0;i<m_nTableLen;i++)
{//new buffer for every dimension
pNew->aContextArray[i]=new int[m_nTableLen];
memset(pNew->aContextArray[i],0,sizeof(int)*m_nTableLen);//Init the frequency
}
if(pRetItem==NULL)//Empty, the new item is head
m_pContext=pNew;
else//Link the new item between pRetItem and its next item
{
pNew->next=pRetItem->next;
pRetItem->next=pNew;
}
pRetItem=pNew;
}
nPrevIndex=BinarySearch(nPrevSymbol,m_pSymbolTable,m_nTableLen);
if(nPrevSymbol>256&&nPrevIndex==-1)//Not find, just for 'nx' and other uncommon POS
nPrevIndex=BinarySearch(nPrevSymbol-nPrevSymbol%256,m_pSymbolTable,m_nTableLen);
nCurIndex=BinarySearch(nCurSymbol,m_pSymbolTable,m_nTableLen);
if(nCurSymbol>256&&nCurIndex==-1)//Not find, just for 'nx' and other uncommon POS
nCurIndex=BinarySearch(nCurSymbol-nCurSymbol%256,m_pSymbolTable,m_nTableLen);
if(nPrevIndex==-1||nCurIndex==-1)//error finding the symbol
return false;
pRetItem->aContextArray[nPrevIndex][nCurIndex]+=nFrequency;//Add the frequency
pRetItem->aTagFreq[nPrevIndex]+=nFrequency;
pRetItem->nTotalFreq+=nFrequency;
return true;
}
bool CContextStat::Save(char *sFilename)
{
FILE *fp,*fp1;
PMYCONTEXT pCur;
char sFileShow[100];
int i;
if((fp=fopen(sFilename,"wb"))==NULL)
{
return false;
}
strcpy(sFileShow,sFilename);
strcat(sFileShow,".shw");
if((fp1=fopen(sFileShow,"w+b"))==NULL)
{
return false;
}
fwrite(&m_nTableLen,sizeof(m_nTableLen),1,fp);//write the table length
fwrite(m_pSymbolTable,sizeof(int),m_nTableLen,fp);//write the symbol table
fprintf(fp1,"Table Len=%d\nSymbol:\n",m_nTableLen);
for(i=0;i<m_nTableLen;i++)
fprintf(fp1,"%d ",m_pSymbolTable[i]);
fprintf(fp1,"\n");
pCur=m_pContext;
while(pCur!=NULL)
{
fwrite(&pCur->nKey,sizeof(int),1,fp);
fwrite(&pCur->nTotalFreq,sizeof(int),1,fp);
fprintf(fp1,"nKey=%d,Total frequency=%d:\n",pCur->nKey,pCur->nTotalFreq);
fwrite(pCur->aTagFreq,sizeof(int),m_nTableLen,fp);//the every POS frequency
for(i=0;i<m_nTableLen;i++)
{
fwrite(pCur->aContextArray[i],sizeof(int),m_nTableLen,fp);
fprintf(fp1,"No.%2d=%3d: ",i,m_pSymbolTable[i]);
for(int j=0;j<m_nTableLen;j++)
fprintf(fp1,"%5d ",pCur->aContextArray[i][j]);
fprintf(fp1,"total=%d:\n",pCur->aTagFreq[i]);
}
pCur=pCur->next;
}
fclose(fp);
fclose(fp1);
return true;
}
bool CContextStat::Load(char *sFilename)
{
FILE *fp;
if((fp=fopen(sFilename,"rb"))==NULL)
{
return false;
}
PMYCONTEXT pCur=m_pContext,pTemp,pPre=NULL;
while(pCur!=NULL)
{//delete the context array
pTemp=pCur->next;
for(int i=0;i<m_nTableLen;i++)
delete [] pCur->aContextArray[i];
delete [] pCur->aContextArray;
delete [] pCur->aTagFreq;
delete pCur;
pCur=pTemp;
}
fread(&m_nTableLen,sizeof(m_nTableLen),1,fp);//write the table length
if(m_pSymbolTable!=NULL)
delete [] m_pSymbolTable;
m_pSymbolTable=new int[m_nTableLen];//new buffer for symbol
fread(m_pSymbolTable,sizeof(int),m_nTableLen,fp);//write the symbol table
//free exists items
while(!feof(fp))
{//Read the context
pCur=new MYCONTEXT;
pCur->next=NULL;
if(fread(&pCur->nKey,sizeof(int),1,fp)<1)//Read error
{
delete pCur;
break;
}
fread(&pCur->nTotalFreq,sizeof(int),1,fp);
pCur->aTagFreq=new int [m_nTableLen];
fread(pCur->aTagFreq,sizeof(int),m_nTableLen,fp);//the every POS frequency
pCur->aContextArray=new int *[m_nTableLen];
for(int i=0;i<m_nTableLen;i++)
{
pCur->aContextArray[i]=new int[m_nTableLen];
fread(pCur->aContextArray[i],sizeof(int),m_nTableLen,fp);
}
if(pPre==NULL)
m_pContext=pCur;
else
pPre->next=pCur;
pPre=pCur;
}
fclose(fp);
return true;
}
double CContextStat::GetContextPossibility(int nKey, int nPrev, int nCur)
{
PMYCONTEXT pCur;
int nCurIndex=BinarySearch(nCur,m_pSymbolTable,m_nTableLen);
int nPrevIndex=BinarySearch(nPrev,m_pSymbolTable,m_nTableLen);
if(!GetItem(nKey,&pCur)||nCurIndex==-1||nPrevIndex==-1||pCur->aTagFreq[nPrevIndex]==0||pCur->aContextArray[nPrevIndex][nCurIndex]==0)
return 0.000001;//return a lower value, not 0 to prevent data sparse
int nPrevCurConFreq=pCur->aContextArray[nPrevIndex][nCurIndex];
int nPrevFreq=pCur->aTagFreq[nPrevIndex];
return 0.9*(double)nPrevCurConFreq/(double)nPrevFreq+0.1*(double)nPrevFreq/(double)pCur->nTotalFreq;
//0.9 and 0.1 is a value based experience
}
int CContextStat::GetFrequency(int nKey, int nSymbol)
{//Get the frequency which nSymbol appears
PMYCONTEXT pFound;
int nIndex,nFrequency=0;
if(!GetItem(nKey,&pFound))//Not found such a item
return 0;
nIndex=BinarySearch(nSymbol,m_pSymbolTable,m_nTableLen);
if(nIndex==-1)//error finding the symbol
return 0;
nFrequency=pFound->aTagFreq[nIndex];//Add the frequency
return nFrequency;
}
bool CContextStat::GetItem(int nKey,PMYCONTEXT *pItemRet)
{//Get the item according the nKey
PMYCONTEXT pCur=m_pContext,pPrev=NULL;
if(nKey==0&&m_pContext)
{
*pItemRet=m_pContext;
return true;
}
while(pCur!=NULL&&pCur->nKey<nKey)
{//delete the context array
pPrev=pCur;
pCur=pCur->next;
}
if(pCur!=NULL&&pCur->nKey==nKey)
{//find it and return the current item
*pItemRet=pCur;
return true;
}
*pItemRet=pPrev;
return false;
}
bool CContextStat::SetTableLen(int nTableLen)
{
m_nTableLen=nTableLen;//Set the table len
m_pSymbolTable=new int[nTableLen];//new buffer for symbol
m_pContext=0;//init with empty
return true;
}