📄 mainfrm.cpp
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "HanZiFreq.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
# define HZ_NUM 6768
# define HZ_ID(c1,c2) ((c1)-176) *94 +((c2)-161)
int HZFreq[HZ_NUM];
BOOL MakeResult=TRUE;
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(ID_StartFreq, OnStartFreq)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CMDIFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CMDIFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CMDIFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void hzInFile(CString FileName) // 统计文件中单字出现次数
{
FILE * in;
unsigned char c1,c2;
int id;
in=fopen((const char *) FileName, "rb");
if(in==NULL) {
AfxMessageBox("can't open the file");
return;
}
while (!feof(in)) {
c1=(unsigned char) fgetc(in);
if(c1==255) {
c2=fgetc(in);
if (c2==255)
break;
}
if(c1<128)
continue;
if(c1<176 || ((c1>247) && (c1<255))) { // 可能是中文标点或者是247之后的空白区域
c2 =fgetc(in);
if (c2>=161 && c2<=254)
continue;
else
{
CString msg;
char cc[3];
cc[0]=c1;
cc[1]=c2;
cc[2]='\0';
msg=cc;
AfxMessageBox("文件中存在非国标码字符:"+msg);
MakeResult=FALSE;
return;
}
}
c2=(unsigned char) fgetc(in);
id =HZ_ID(c1,c2);
if (c2<161 || id>HZ_NUM)
{
CString msg;
char cc[3];
cc[0]=c1;
cc[1]=c2;
cc[2]='\0';
msg=cc;
AfxMessageBox("文件中存在非国标码字符:"+msg);
MakeResult=FALSE;
return;
}
HZFreq[id]++;
}
fclose(in);
return;
}
void CMainFrame::OnStartFreq()
{// 对多个文件统计字频函数
// TODO: Add your command handler code here
int hzTypeCounter=0;
int hzTokenCounter=0;
for(int i=0; i<HZ_NUM; i++)
HZFreq[i]=0; // 初始化:将所有汉字出现次数置为0
int n=ProcessFiles("","*.*",hzInFile); // 调用字频统计函数hzInFile统计语料中汉字出现的次数
if (n==0)
return;
if(MakeResult==FALSE)
{
AfxMessageBox("因为文件中存在非国标码字符或选取文件有错误,没有产生统计结果");
return;
}
// 将统计结果按字频大小排序输出
struct { // 构造一个结构存放汉字和它的频率
char cc[2];
int freq;
} zipin[7000];
// 把文件中出现过的汉字及其出现次数值写进zipin结构中存放
for (int id=0; id<HZ_NUM; id++)
if(HZFreq[id]>0) {
zipin[hzTypeCounter].cc[0]=(unsigned char) (id/94+176);
zipin[hzTypeCounter].cc[1]=(unsigned char) (id%94+161);
zipin[hzTypeCounter].freq = HZFreq[id];
hzTypeCounter++;
hzTokenCounter=hzTokenCounter+HZFreq[id];
}
// 把zipin结构中的汉字按字频从大到小排序
int temp_freq;
char temp_cc[2];
for (int j=0;j<hzTypeCounter-1;j++) {
for (int k=1;k<hzTypeCounter;k++){
if (zipin[k].freq>zipin[k-1].freq)
{
temp_freq=zipin[k].freq;
temp_cc[0]=zipin[k].cc[0];
temp_cc[1]=zipin[k].cc[1];
zipin[k].freq=zipin[k-1].freq;
zipin[k].cc[0]=zipin[k-1].cc[0];
zipin[k].cc[1]=zipin[k-1].cc[1];
zipin[k-1].freq=temp_freq;
zipin[k-1].cc[0]=temp_cc[0];
zipin[k-1].cc[1]=temp_cc[1];
}
}
}
// 统计结果输出到文件,每行五个汉字及其出现次数值
// 确定输出文件名
FILE * outFile2;
/*
// 通过文件对话框确定输出文件名
CFileDialog dlg(FALSE, "", "*.*", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT);
if(dlg.DoModal()!=IDOK) {
AfxMessageBox("您没有选取任何文件!");
return;
}
CString outputFileName=dlg.GetPathName();
outFile2=fopen(outputFileName,"wt");
*/
outFile2=fopen("HanZiFreq_Result.txt","wt"); // 内定文件名
if(outFile2==NULL) {
AfxMessageBox("can't write the file");
return;
}
int dd=0,kk=0;
while (kk<hzTypeCounter){
if (dd<5) {
fprintf(outFile2,"%c%c:%d\t",zipin[kk].cc[0],zipin[kk].cc[1],zipin[kk].freq);
dd++;
}
else {
fprintf(outFile2,"\n");
fprintf(outFile2,"%c%c:%d\t",zipin[kk].cc[0],zipin[kk].cc[1],zipin[kk].freq);
dd=1;
}
kk++;
}
fprintf(outFile2,"\n\n语料总字数:\t%d\n语料用字字种总数:\t%d",hzTokenCounter,hzTypeCounter);
fclose(outFile2);
AfxMessageBox("全部文件处理完毕!\n请打开HanZiFreq_Result.txt文件\n查看统计结果");
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -