?? oncorpusnew.cpp
字號:
//創(chuàng)建語料庫
void CMainFrame::OnCorpusNew()
{
CFileDialog dlg(FALSE); //獲取語料存盤文件名
if(dl.DoModel()!=IDOK) return;
if(1 corpusName.IsEmpty()) OnCorpusClose(); //關(guān)閉當前語料庫
corpusName = dlg.GetPathName();
OnCorpusAdd(); //添加語料
}
//打開語料庫
void CMainFrame::OnCorpusOpen()
{
CFileDialog dlg(TRUE,"ylk","*.ylk",
OFN_HIDEREADONLY| OFN_OVERWRITEPROMPT,
"語料庫文件|*.ylk|所有文件||"); //語料庫存盤文件
if(dl.DoModel()!=IDOK) return;
if(1 corpusName.IsEmpty()) OnCorpusClose();
CFile cf;char buf[512];
if(cf.Open(const char *)(dlg.GetPathName()),CFile::modeRead){
CArchive ar(&cf, CArchive::load,512,buf);
texts.Serialize(ar);// 讀入所有語料文件名
}
else {AfxMessageBox("無法打開語料庫!");return;}
CFile tf;
CString ylkName = ChangeExt(dlg.GetPathName(), "@@@");
if(tf.Open(const char *)ylkName,CFile::modeRead)){
CArchive ar(&tf, CArchive::load,512,buf);
for(int i=0;i<6768;i++)
hzInfo[i].Serialize(ar); //讀入每個漢字的信息(出現(xiàn)次數(shù)和地址串)
}
else{AfxMessageBox("無法讀入漢字信息!");return;}
corpusName = dlg.GetPathName(); //保存語料庫文件名
CorpusModified=FALSE; //語料庫修改標記
}
//關(guān)閉語料庫
void CMainFrame::OnCorpusClose()
{
if(corpusName.IsEmpty()) return;
if(CorpusModified) { //如果語料庫已經(jīng)修改
CFile cf;char buf[512];
if(cf.Open((const char *)corpusName,
CFile::modeCreate|CFile::modeWrite)){
CArchive ar(&cf, CArchive::store,512,buf);
texts.Serialize(ar);
}
else {AfxMessageBox("語料庫無法存盤!");return;}
texts.RemoveAll();
CFile tf;
CString ylkName = ChangeExt(corpusName, "@@@");
if(tf.Open(const char *)ylkName,
CFile::modeCreate|CFile::modeWrite)){
CArchive ar(&tf, CArchive::store,512,buf);
for(int i=0;i<6768;i++)
hzInfo[i].Serialize(ar);
}
else{AfxMessageBox("漢字信息無法存盤!");return;}
corpusName = ""; //表示現(xiàn)在沒有打開的語料庫
CorpusModified=FALSE; //語料庫修改標記
}
//增加語料
void CMainFrame::OnCorpusAdd()
{
if(corpusName.IsEmpty()||texts.GetSize() == 65535) return;
ProcessFiles ("txt","*.txt",AddAFile);
}
void AddAFile(CString FileName)
{
int n= texts.GetSize();
if( n == 65535) return;
FILE *in; in=fopen((const char *)FileName, "rb");
if(!in) {AfxMessageBox("打不開文件!"+ FileName);return;}
while (! feof(in)) {
unsigned char c1,c2;
c1=(unsigned cahr)fgetc(in);
if(c1<128) continue; //不處理西文字符
c1=(unsigned cahr)fgetc(in);
if(c1>=176) { //如果是漢字
int id =HZ_ID(c1,c2); if(id<0||id>=6768) continue;
hzInfo[id].AddTextID(n); //將語料文件序號加入地址串
}
}
fclose(in);
FileName.MakeLower(); //將文件名中的字母轉(zhuǎn)為小寫
texts.Add(FileName); //將文件名添加到字符串數(shù)組
CorpusModified = TRUE; //設(shè)置語料庫修改標記
}
//檢索漢字串
//需要獲取用戶輸入待查找的漢字串,并檢查其合法性
void CMainFrame::OnCorpusHanzi()
{
CString key;
if(!GetData("查找漢字串:",key)) return;
key.TrimLeft(); key.TrimRight(); //壓縮前后空格
int id,id2, n=key.GetLength();
if(! GoodHzStr(key)) { //檢查輸入的合法性
AfxMessageBox("輸入的漢字串有錯誤!"+ FileName);
return;
}
CWordArray *txtID, *tmp;
id=HZ_ID((unsigned char)key[0],(unsigned char)key[1]);
if(n==2){ //如果輸入的是單個漢字
txtID=& (hzInfo[id].TextID);
if(txtID->GetSize()==0) {
AfxMessageBox("找不到這個漢字串");
return;
}
Retrive(txtxID,key); //在語料文件中檢索漢字key
return;
}
//以下處理檢索多個漢字
id2 =HZ_ID((unsigned char)hey[2],(unsigned char)hey[3]);
//第二個漢字
txtID = Intersection(hzInfo[id].TextID,hzInfo[id2].TextID);
//求前兩個漢字的地址串的交集
int i=4; //從第三個漢字開始
while(txtID->GetSize()>0 && i<key.GetLength()-1){
id2 =HZ_ID((unsigned char)hey[i],(unsigned char)hey[i+1]);
tmp= Intersection(*textID,hzInfo[id2].TextID);
delete txtID;txtID=tmp;i+=2;
}
if(txtID->GetSize() == 0) { //如果交集為空
AfxMessageBox("找不到這個漢字串");
return;
}
Retrive(txtxID,key); //在語料文件中檢索漢字key
return;
}
//合法性輸入函數(shù)
BOOL GoodHzStr(CString s)
{
int n=s.GetLength();
if(n==0||n%2!=0) return FALSE;
for(int i=0;i<n-1:i+=2)
if((unsigned char)s[i]<176 || (unsigned char)s[i]<161) return FALSE;
return TRUE;
}
//求地址串的交集
CWordArray *Intersection(CWordArray &wi, CWordArray &wj)
{
CWordArray *pw= new CWordArray; //交集指針
for(int i=0,j=0; i<wi.GetSize() && j<wj.GetSize();) {
if(wi[i] == wj[j]) {
pw->Add(wi[i]); i++;j++;} //加入相同地址
else if(wi[i]<wj[j]) i++;
else j++;
}
return pw;
}
//由地址查找漢字串
void Retrive(CWordArray *txtID, CString key)
{
if(txtID->GetSize() == 0) return;
FILE *in, *out;
int Examples=0;
out = fopen("f:\\work\\4.8\\現(xiàn)代漢語自動分析\\output.txt","wt");
if(! out) { AfxMessageBox("無法創(chuàng)建檢索輸出文件!");return; }
CStdioFile outFile(out);
outFile.WriteString("查找字符串:“");
outFile.WriteString(key+"”\n");
for(int i=0;i<txtID->GetSize();i++){
int id = txtID->GetAt(i); //取出一個地址
CStdio fname = texts[id]; //取出相應(yīng)的語料文件名
in=fopen((const char *)frame, "rt");
if(! in) { AfxMessageBox("找不到語料文件"+frame);continue; }
CStdioFile inFile(in);
char s[3000];CString ss="",dd;
while (inFile.ReadString(s,3000)) //讀語料文件的每一行
if(key.GetLength()>2 && strstr(s,(const char *))key)
|| key.GetLength()== 2 && FindOneHZ(s,(const char *)key)>= 0) { //如果含有該漢字或者漢字串
Examples s++; //例子個數(shù)
dd.Format("例%40d:",Examples);
ss+ = dd + s ;ss+ = '\n'; //加到輸出串中
}
if(ss.GetLength()>0) {
outFile.WriteString(fname+":\n"); //寫文件名
outFile.WriteString(ss+"”\n"); //寫輸出串
}
inFile.Close();
}
outFile.Close();
if(Examples >0) AfxGetApp()-> OpenDocumentFile("output.txt");
//在應(yīng)用程序中打開輸出文件
else AfxMessageBox("找不到這個漢字串");
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -