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

📄 mainfrm.cpp

📁 中文信息处理方面的一个源码。此为一个词性标注软件
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "POSTagger.h"

#include "MainFrm.h"
#include "MyDictionary.h"
#include "MyFileApp.h"
#include "POSTagging.h"

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

# if !pDict
extern CMyDictionary pDict;
# endif

#if !coMatrix
extern CCoMatrix coMatrix;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_GetTagSet, OnGetTagSet)
	ON_COMMAND(ID_Trainning, OnTrainning)
	ON_COMMAND(ID_Tagging, OnTagging)
	ON_COMMAND(ID_OpenLexicon, OnOpenLexicon)
	//}}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 CMainFrame::OnOpenLexicon() 
{//打开词库,为训练语料和词性标注做准备
	// TODO: Add your command handler code here
	if (pDict.myDatabaseName.IsEmpty()) 
		pDict.OpenMDB();
	else
		AfxMessageBox("词库已经打开");	
}

void CMainFrame::OnGetTagSet() 
{// 从文本文件中获取词性标记集,包括开放类的词性标记,如名词,动词等
	// TODO: Add your command handler code here
	CFileDialog dlg(TRUE);
	
	if(dlg.DoModal()!=IDOK)
		return;
	
	coMatrix.FileName=dlg.GetPathName(); // 获取数据文件的文件名

	if(coMatrix.FileName.Right(3)=="@#$") {
		CFile tf;
		char buf[512];

		if(tf.Open((const char *) coMatrix.FileName,CFile::modeRead)) {
			CArchive ar(&tf,CArchive::load,512,buf);
			coMatrix.Serialize(ar); // 读入原有的词性标记集及模型参数
			coMatrix.possetModified=TRUE; // 表示已经读入一个新的词性标记集
		}
/*	输出词性转移矩阵数据到文本文件	
		FILE *out; // 输出词性转移矩阵数据到文本文件
		out=fopen("posmatrix.txt","wt");
		if(!out) {
			AfxMessageBox("无法创建词性转移矩阵数据文件");
			return;
		}
		CStdioFile outFile(out);
		coMatrix.WriteTo(out);
		outFile.Close();
*/	}
	else {
		FILE *in;
		in=fopen((const char *)dlg.GetPathName(),"rt");
		if(!in) {
			AfxMessageBox("无法打开词性标记集文件");
			return;
		}
		CStdioFile inFile(in);
		coMatrix.Create(inFile);
		coMatrix.FileName=ChangeExt(coMatrix.FileName,"@#$");
	}
}

void CMainFrame::OnTrainning() 
{// 对已经标注了词性标记的语料进行训练
	// TODO: Add your command handler code here
	if(!coMatrix.possetModified) // 判断是否读入了词性标记集
	{
		AfxMessageBox("您没有读入词性标记集");
		return;
	}
	
	if (pDict.myDatabaseName.IsEmpty()) {
		AfxMessageBox("您没有打开词库,无法进行分词处理");
		return;
	}
	ProcessFiles("","*.*",TrainFile); // 对所有选定的已标注语料文件进行处理

///////////////////////////////////////////////// 2002-12-13
// 输出词性转移矩阵数据到文本文件
//
    FILE *out; 
	out=fopen("posmatrix.txt","wt");
	if(!out) {
		AfxMessageBox("无法创建词性转移矩阵数据文件");
		return;
	}
	CStdioFile outFile(out);
	coMatrix.WriteTo(out);
	outFile.Close();
////
///////////////////////////////////////////////// 2002-12-13
}

void CMainFrame::OnTagging() 
{ // 对语料进行词性标注
	// TODO: Add your command handler code here

	if (pDict.myDatabaseName.IsEmpty()) {
		AfxMessageBox("您没有打开词库,无法进行处理");
		return;
	}

	if(!coMatrix.possetModified)
	{
		AfxMessageBox("您没有读入词性标记集");
		return;
	}

	if(!coMatrix.Ready()) // 判断是否读入了词性标记集及进行过语料训练
	{
		AfxMessageBox("您没有通过训练语料获得模型参数");
		return;
	}

	ProcessFiles("","*.*",TaggingFile);
}

⌨️ 快捷键说明

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