⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wordsegdlg.cpp

📁 这是一个中文分词程序。用户将中文文件(.txt)打开
💻 CPP
字号:
// WordSegDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WordSeg.h"
#include "WordSegDlg.h"
#include "wordsegment.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWordSegDlg dialog

extern CWordSegApp theApp;
extern CWordSegment g_WordSeg;

CWordSegDlg::CWordSegDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWordSegDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWordSegDlg)
	m_strFileName = _T("");
	m_strSource = _T("");
	m_strTarget = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWordSegDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWordSegDlg)
	DDX_Text(pDX, IDC_EDIT_FILENAME, m_strFileName);
	DDX_Text(pDX, IDC_EDIT_SOURCE, m_strSource);
	DDX_Text(pDX, IDC_EDIT_TARGET, m_strTarget);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWordSegDlg, CDialog)
	//{{AFX_MSG_MAP(CWordSegDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_BROWSE, OnBtnBrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWordSegDlg message handlers

BOOL CWordSegDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CWordSegDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWordSegDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWordSegDlg::OnBtnBrowse() 
{
	CFileDialog dlg(TRUE,NULL,NULL,NULL,
		"Text Files (*.txt)|*.txt|All Files (*.*)|*.*||");

	if(dlg.DoModal()==IDOK)
	{
		m_strFileName=dlg.GetPathName();
		
		CFile fIn;
		if(!fIn.Open(m_strFileName,CFile::modeRead))
			return;
		
		fIn.Read(m_strSource.GetBuffer(fIn.GetLength()),fIn.GetLength());
		m_strSource.ReleaseBuffer();
		UpdateData(FALSE);
	}
}

void CWordSegDlg::OnOK() 
{
	UpdateData(TRUE);
	CFile fIn;
	if(!fIn.Open(m_strFileName,CFile::modeRead))
		return;

	DWORD dwSize=fIn.GetLength();
	char *buffer=new char[dwSize+1];
	fIn.Read(buffer,dwSize);
	buffer[dwSize]=0;

	int wordcnt;
	char *c;
	char *a=new char[50];
	char *b=new char[5];

	//创建分词的类的对象
	//这是个样例程序,所以在这里创建了分词类的对象,但是每个分词类的对象都会读取一次词表,占用内存空间
	//所以最好创建一个全局分词对象,如果某个文件中要用,就使用extern CWordSegment g_WordSeg说明一下

	if(!g_WordSeg.InitFCDll(theApp.m_strPath))
	{
		AfxMessageBox("分词组件初始化失败!");
		return;
	}
	//设置分词的参数,CWordSegment::cwsCPlaceName代表分词时识别地名,CWordSegment::cwsCName代表分词时识别人名
	//CWordSegment::cwsIdiom代表识别成语,CWordSegment::cwsOrganiseName代表识别组织机构名
	//g_WordSeg.cwsSetSegSetting(CWordSegment::cwsTag|CWordSegment::cwsCPlaceName|
	//		CWordSegment::cwsCName|CWordSegment::cwsIdiom|CWordSegment::cwsOrganiseName);
	//g_WordSeg.cwsSetSegSetting();

	//开始对buffer中的字符串进行分词,返回分词结果数目
	wordcnt=g_WordSeg.cwsSegment(buffer);

	m_strTarget.Empty();
	for(int i=0;i<wordcnt;i++)
	{
		c=g_WordSeg.fcwsGetWord(i,&a,&b);
		m_strTarget=m_strTarget+c+" ";
	}
	delete[] a;
	delete[] b;
	delete[] buffer;
	
	UpdateData(FALSE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -