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

📄 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"
#include "GetWordInfo.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)
	ON_COMMAND(ID_Add_NewWord, OnAddNewWord)
	ON_COMMAND(ID_RUN, OnRun)
	//}}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);
}


void CMainFrame::OnAddNewWord() 
{
	// TODO: Add your command handler code here
	if (pDict.myDatabaseName.IsEmpty()) {
		AfxMessageBox("您没有打开词库,无法添加新的词条");
		return;
	}

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

	GetWordInfo nwDlg;
	if(nwDlg.DoModal() != IDOK)
		return;

	CString newword, newpos;
	long newfreq;

	newword = nwDlg.m_NewWord;
	newpos  = nwDlg.m_NewPos;
	newfreq = nwDlg.m_NewFreq;

	newword.TrimLeft();
	newword.TrimRight();

	newpos.TrimLeft();
	newpos.TrimRight();

	if(newword.IsEmpty() || newpos.IsEmpty() || (newfreq<1 || newfreq>65536))
	{
		AfxMessageBox("词语、词性标记不能为空,频次取值应在1到65536之间");
		return;
	}
	

	int checktag = 0, i=coMatrix.pTags->GetSize();
	for(int k=0; k<i; k++)
	{
		if (newpos == coMatrix.pTags->GetAt(k)){
			checktag = 1;
			break;
		}
	}

	if (checktag == 0)
	{
		AfxMessageBox("新增词条的词性标记不在预定义标记集范围内,当前操作将被取消");
		return;
	}

	pDict.Insert(newword, newpos, newfreq);
}



void convert(CString FileName)
{
	FILE *in,*out;
	in=fopen((const char *)FileName,"rt");
	if(in==NULL)
	{
		AfxMessageBox("无法打开文件");
		return;
	}
	out=fopen("D:\\train.data","a");
	CStdioFile inFile(in);
	char s[2048];
	CString word,w1,w2,pos,line,st="";
//	CString ;
	while(inFile.ReadString(s,2048))
	{// 循环读入文件中的每一行
		int sp,sp1;
		line = s;
		sp=line.Find("/")+1;
		while(sp!=0)
		{
			sp1=line.Find("  ")+2;
			word=line.Left(sp-1);
			if(line.GetAt(sp)=='w')
			{
				fprintf(out,"%s  w  B\n",word);//标点符号格式转换
				if(word=="。"||word=="!"||word=="?")
					fprintf(out,"\n");
			}
			else 
			{
				pos=line.Mid(sp,sp1-sp-2);
				w1=word.Left(2);
				fprintf(out,"%s  %s  B\n",w1,pos);//文字格式转换
				if(word.GetLength()>2)
					w2=word.Mid(2);
				while(w2.GetLength()>=2)
				{
					w1=w2.Left(2);
					fprintf(out,"%s  %s  I\n",w1,pos);//文字格式转换
					w2=w2.Mid(2);
				}
			}
			line=line.Mid(sp1);	
			sp=line.Find("/")+1;
		}
	}
	fprintf(out,"\n\n");
	inFile.Close();
	fclose(in);
	fclose(out);
}


void CMainFrame::OnRun() 
{
	// TODO: Add your command handler code here
    CFileDialog dlg(TRUE, "", "", OFN_ALLOWMULTISELECT,"已标注文件(*.pos)|*.pos|文本文件 (*.txt)|*.txt|所有文件|*.*||");
	CString strFileNames; // 分配一片空间存放文件名,可以选取多个文件

	dlg.m_ofn.lpstrFile = strFileNames.GetBuffer(65536);
	dlg.m_ofn.nMaxFile =65536;
	if(dlg.DoModal()!=IDOK) {
		AfxMessageBox("您没有选取任何文件!");
		return;
	}
//	OPENFILENAME filename     ;
//	filename.nMaxFile
//	int fileCount = 0;
	CString FileName;
	POSITION pos = dlg.GetStartPosition(); // 获取第一个文件名的起点位置
	while(pos!=NULL) {  // 如果有文件可以获取
		FileName = dlg.GetNextPathName(pos); // 获取文件名,并移到下一个文件名的起始位置
	convert(FileName); // 调用处理单个文件的函数
	}
	AfxMessageBox("格式转换成功,结果存放在D:\\train.data中");
	
}

⌨️ 快捷键说明

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