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

📄 mainfrm.cpp

📁 良好的代码实现
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "TextClassify.h"
#include "Message.h"

#include "MainFrm.h"
#include "LeftView.h"
#include "TextClassifyView.h"


#include "SegDocDlg.h"
#include "WordSegment.h"
#include "TrainParamsPage.h"
#include "TestParamsDlg.h"
#include "classifier.h"
#include "svmparamspage.h"
#include <direct.h>

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame
CString CMainFrame::m_strResultDir;

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_MESSAGE(WM_COMPUTATION_FINISH,OnComputationFinish)
	ON_COMMAND(ID_TOOLS_WORDSEG, OnToolsWordseg)
	ON_WM_CLOSE()
	ON_WM_TIMER()
	ON_COMMAND(ID_MAIN_TRAINKNN, OnMainTrainknn)
	ON_UPDATE_COMMAND_UI(ID_MAIN_TRAINKNN, OnUpdateMainTrainknn)
	ON_COMMAND(ID_MAIN_TRAINSVM, OnMainTrainsvm)
	ON_UPDATE_COMMAND_UI(ID_MAIN_TRAINSVM, OnUpdateMainTrainsvm)
	ON_COMMAND(ID_MAIN_TRAINBAYES,OnMainTrainbayes)
	ON_UPDATE_COMMAND_UI(ID_MAIN_TRAINBAYES,OnUpdateMainTrainbayes)
	ON_COMMAND(ID_MAIN_OPENMODEL, OnMainOpenmodel)
	ON_UPDATE_COMMAND_UI(ID_MAIN_OPENMODEL, OnUpdateMainOpenmodel)
	ON_COMMAND(ID_MAIN_RESULTS, OnMainResults)
	ON_UPDATE_COMMAND_UI(ID_MAIN_RESULTS, OnUpdateMainResults)
	ON_COMMAND(ID_MAIN_TEST, OnMainTest)
	ON_UPDATE_COMMAND_UI(ID_MAIN_TEST, OnUpdateMainTest)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator	
//	ID_INDICATOR_CAPS,
//	ID_INDICATOR_NUM,
//	ID_INDICATOR_SCRL,
//	ID_INDICATOR_TIME,
};


UINT CMainFrame::TrainThreadProc(LPVOID pParam)
{	
	theClassifier.m_theSVM.com_param.running=1;
	theClassifier.Train(theClassifier.m_paramClassifier.m_nClassifierType);
	theClassifier.m_theSVM.com_param.running=0;
	::PostMessage((HWND)pParam,WM_COMPUTATION_FINISH,0,0);
	return 0;
}

UINT CMainFrame::TestThreadProc(LPVOID pParam)
{
	if(theClassifier.m_paramClassifier.m_nDocFormat==CClassifierParam::nDF_Directory)
	{
		if(theClassifier.Classify())
			CMainFrame::m_strResultDir = theClassifier.m_paramClassifier.m_strResultDir;
	}
	else if(theClassifier.m_paramClassifier.m_nDocFormat==CClassifierParam::nDF_Smart)
	{
		if(theClassifier.ClassifySmart())
			CMainFrame::m_strResultDir = theClassifier.m_paramClassifier.m_strResultDir;
	}
	::PostMessage((HWND)pParam,WM_COMPUTATION_FINISH,0,0);
	return 0;
}
/*
UINT CMainFrame::ConvertThreadProc(LPVOID pParam)
{
	CTime startTime;
	CTimeSpan totalTime;

	CMessage::PrintInfo(_T("开始进行文档格式转换,请稍候..."));
	startTime=CTime::GetCurrentTime();
	theSaxFileHandler.Convert();
	totalTime=CTime::GetCurrentTime()-startTime;
	CMessage::PrintInfo(_T("文档格式转换结束,耗时")+totalTime.Format("%H:%M:%S"));
	CMessage::PrintStatusInfo("");

	::PostMessage((HWND)pParam,WM_COMPUTATION_FINISH,0,0);
	ExitThread(0);
	return 0;
}

UINT CMainFrame::SeldocsThreadProc(LPVOID pParam)
{
	CTime startTime;
	CTimeSpan totalTime;
	long lDocNum;
	CString str;

	CMessage::PrintInfo(_T("开始选择训练文档和测试文档,请稍候..."));
	startTime=CTime::GetCurrentTime();
	lDocNum=theSelectDocs.SelectDocs();
	totalTime=CTime::GetCurrentTime()-startTime;
	str.Format("共处理了%d篇文档!",lDocNum);
	CMessage::PrintInfo(str);
	CMessage::PrintInfo(_T("选择文档结束,耗时")+totalTime.Format("%H:%M:%S"));
	CMessage::PrintStatusInfo("");

	::PostMessage((HWND)pParam,WM_COMPUTATION_FINISH,0,0);
	ExitThread(0);
	return 0;
}
*/
UINT CMainFrame::SegDocsThreadProc(LPVOID pParam)
{
	CTime startTime;
	CTimeSpan totalTime;
	startTime=CTime::GetCurrentTime();
	CMessage::PrintInfo(_T("分词程序初始化,请稍候..."));	
	if(!g_wordSeg.InitWorgSegment(theApp.m_strPath.GetBuffer(0)))
	{
		CMessage::PrintError(_T("分词程序初始化失败!"));
		return 0;
	}
	g_wordSeg.SetSegSetting(CWordSegment::uPlace);
	CMessage::PrintInfo(_T("正在进行分词,请稍候..."));	

	if(theSegDocsParam.m_bMultiDocs)
	{
		CString strTarget,strSource;
		HANDLE hFinder;
		LPWIN32_FIND_DATA lpFindFileData;	
		lpFindFileData  = new WIN32_FIND_DATA;
		hFinder = ::FindFirstFile(theSegDocsParam.m_strSource+"\\*.*",lpFindFileData );
		while(::FindNextFile(hFinder,lpFindFileData))
		{
			if( !strcmp(lpFindFileData->cFileName,".") || !strcmp(lpFindFileData->cFileName,"..") )
				continue;

			if(!(lpFindFileData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
			{
				strTarget=theSegDocsParam.m_strTarget+"\\";
				strTarget+=lpFindFileData->cFileName;
				strSource=theSegDocsParam.m_strSource+"\\";
				strSource+=lpFindFileData->cFileName;
				g_wordSeg.SegmentFile(strSource.GetBuffer(0),
									strTarget.GetBuffer(0),
									theSegDocsParam.m_bDelStopwords);
				CMessage::PrintStatusInfo(lpFindFileData->cFileName);
			}
		}
		delete	lpFindFileData;
	}
	else
		g_wordSeg.SegmentFile(theSegDocsParam.m_strSource.GetBuffer(0),
							theSegDocsParam.m_strTarget.GetBuffer(0),
							theSegDocsParam.m_bDelStopwords);

	g_wordSeg.FreeWordSegment();
	totalTime=CTime::GetCurrentTime()-startTime;
	CMessage::PrintInfo(_T("分词结束,耗时")+totalTime.Format("%H:%M:%S"));
	CMessage::PrintStatusInfo("");

	::PostMessage((HWND)pParam,WM_COMPUTATION_FINISH,0,0);
	ExitThread(0);
	return 0;
}




/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	m_pThread=NULL;
	m_strModel.Empty();
	m_strResultDir.Empty();
	m_nRunning=0;
	m_bPaused=false;
	m_nClassifierType=0;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;


	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
	}
	
	CMessage::SetStatusWnd(&m_wndStatusBar);

	return 0;
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
	CCreateContext* pContext)
{
	// create splitter window
	if (!m_wndSplitter.CreateStatic(this, 1, 2))
		return FALSE;

	if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftView), CSize(100, 100), pContext) ||
		!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CTextClassifyView), CSize(100, 100), pContext))
	{
		m_wndSplitter.DestroyWindow();
		return FALSE;
	}
	CMessage::SetOutputWnd((CWnd *)m_wndSplitter.GetPane(0, 1));
	return TRUE;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::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
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

/*
CSvmclsView* CMainFrame::GetRightPane()
{
	CWnd* pWnd = m_wndSplitter.GetPane(0, 1);
	CSvmclsView* pView = DYNAMIC_DOWNCAST(CSvmclsView, pWnd);
	return pView;
}

CLeftView* CMainFrame::GetLeftView()
{
	CWnd* pWnd = m_wndSplitter.GetPane(0, 0);
	CLeftView* pView = DYNAMIC_DOWNCAST(CLeftView, pWnd);
	return pView;
}

void CMainFrame::OnUpdateViewStyles(CCmdUI* pCmdUI)
{
	// TODO: customize or extend this code to handle choices on the
	// View menu.

	CSvmclsView* pView = GetRightPane(); 

	// if the right-hand pane hasn't been created or isn't a view,
	// disable commands in our range

	if (pView == NULL)
		pCmdUI->Enable(FALSE);
	else
	{
		DWORD dwStyle = pView->GetStyle() & LVS_TYPEMASK;

		// if the command is ID_VIEW_LINEUP, only enable command
		// when we're in LVS_ICON or LVS_SMALLICON mode

		if (pCmdUI->m_nID == ID_VIEW_LINEUP)
		{
			if (dwStyle == LVS_ICON || dwStyle == LVS_SMALLICON)
				pCmdUI->Enable();
			else
				pCmdUI->Enable(FALSE);
		}
		else
		{
			// otherwise, use dots to reflect the style of the view
			pCmdUI->Enable();
			BOOL bChecked = FALSE;

			switch (pCmdUI->m_nID)
			{
			case ID_VIEW_DETAILS:
				bChecked = (dwStyle == LVS_REPORT);
				break;

			case ID_VIEW_SMALLICON:
				bChecked = (dwStyle == LVS_SMALLICON);
				break;

			case ID_VIEW_LARGEICON:
				bChecked = (dwStyle == LVS_ICON);
				break;

			case ID_VIEW_LIST:
				bChecked = (dwStyle == LVS_LIST);
				break;

			default:
				bChecked = FALSE;
				break;
			}

			pCmdUI->SetRadio(bChecked ? 1 : 0);
		}
	}
}

*/
void CMainFrame::OnComputationFinish(WPARAM wParam,LPARAM lParam)
{
	CeaseComputation();
}


void CMainFrame::CeaseComputation()
{	
	m_wndStatusBar.SetPaneText(0,"运算全部结束!");
	m_nRunning=0;
	m_pThread=NULL;
	m_bPaused=false;
	KillTimer(m_nTimer);
}

void CMainFrame::BeginComputation()
{
	m_nRunning=1;
	m_nTimer=SetTimer(1001,500,NULL);
	m_tmBegin=CTime::GetCurrentTime();
}
/*
void CMainFrame::OnTimer(UINT nIDEvent) 
{
	CTimeSpan t=CTime::GetCurrentTime()-m_tmBegin;
	m_wndStatusBar.SetPaneText(1,t.Format("耗时%H:%M:%S"),TRUE);
	CFrameWnd::OnTimer(nIDEvent);
}

void CMainFrame::OnClose() 
{
	GetActiveDocument()->SetModifiedFlag(false);
	if(m_nRunning>0)
	{
		if(AfxMessageBox("正在进行计算,真的要停止吗?",MB_YESNO)==IDYES)
		{
			StopComputation();
			CFrameWnd::OnClose();
		}
	}
	else
		CFrameWnd::OnClose();
}
*/
void CMainFrame::Train()
{
	CMessage::ClearInfo();
	BeginComputation();
	m_pThread=AfxBeginThread(TrainThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
}

void CMainFrame::Test() 
{
	CMessage::ClearInfo();
	BeginComputation();
	m_pThread=AfxBeginThread(TestThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
}

/*
void CMainFrame::OnMainClassifySinglefile() 
{
	static char BASED_CODE szTextFilter[] = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*||";
	CFileDialog cfd(TRUE,szTextFilter,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szTextFilter,NULL);
	if (cfd.DoModal()==IDOK) 
	{
		CString strFile=cfd.GetPathName();
		short id;
		theClassifier.m_paramClassifier.m_dThreshold     =   60;
		theClassifier.m_paramClassifier.m_nKNN           =   35;
		theClassifier.m_paramClassifier.m_nClassifyType  =   CClassifierParam::nFT_Single;
		theClassifier.m_paramClassifier.m_bEvaluation    =   FALSE;
		theClassifier.m_paramClassifier.m_bCopyFiles     =   FALSE;
		if(theClassifier.m_paramClassifier.m_nClassifierType==CClassifierParam::nCT_KNN)
			id=theClassifier.KNNCategory(strFile.GetBuffer(0));
		else if(theClassifier.m_paramClassifier.m_nClassifierType==CClassifierParam::nCT_SVM)
			id=theClassifier.SVMCategory(strFile.GetBuffer(0));
		else
		{
			CMessage::PrintError("分类器类型不正确!");
			return;
		}
		CMessage::ClearInfo();
		if(id>=0) theClassifier.m_lstTrainCatalogList.GetCataName(id,strFile);
		else strFile="无法识别";
		CMessage::PrintInfo("文档的类别为: "+strFile);
	}	
}
*/

void CMainFrame::OnMainTest() 
{
	CTestParamsDlg testParamsDlg;
	
	if(theClassifier.m_paramClassifier.m_strTestDir.IsEmpty())
		theClassifier.m_paramClassifier.m_strTestDir=theApp.m_strPath+"\\sample\\test";
	if(theClassifier.m_paramClassifier.m_strResultDir.IsEmpty())
		theClassifier.m_paramClassifier.m_strResultDir=theClassifier.m_paramClassifier.m_txtResultDir;
	if((theClassifier.m_paramClassifier.m_nClassifierType==CClassifierParam::nCT_SVM)||
		(theClassifier.m_paramClassifier.m_dThreshold>0))
	{
		theClassifier.m_paramClassifier.m_nKNN=35;
		theClassifier.m_paramClassifier.m_dThreshold=0;
	}

	testParamsDlg.m_strTestDir    = theClassifier.m_paramClassifier.m_strTestDir;
	testParamsDlg.m_strResultDir  = theClassifier.m_paramClassifier.m_strResultDir;
	testParamsDlg.m_nDocFormat    = theClassifier.m_paramClassifier.m_nDocFormat;
	testParamsDlg.m_dThreshold    = theClassifier.m_paramClassifier.m_dThreshold;
	testParamsDlg.m_nKNN          = theClassifier.m_paramClassifier.m_nKNN;
	testParamsDlg.m_nClassifyType = theClassifier.m_paramClassifier.m_nClassifyType;
	testParamsDlg.m_bEvaluation   = theClassifier.m_paramClassifier.m_bEvaluation;
	testParamsDlg.m_bCopyFiles    = theClassifier.m_paramClassifier.m_bCopyFiles;

	testParamsDlg.SetClassifierType(theClassifier.m_paramClassifier.m_nClassifierType);

⌨️ 快捷键说明

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