📄 mainfrm.cpp
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "svmcls.h"
#include "svmclsDoc.h"
#include "Message.h"
#include "MainFrm.h"
#include "LeftView.h"
#include "svmclsView.h"
#include "ConvertDlg.h"
#include "SAXFileHandlers.h"
#include "svmparamspage.h"
#include "TrainParamsPage.h"
#include "TestParamsDlg.h"
#include "DocselDlg.h"
#include "classifier.h"
#include "SelectDocs.h"
#include "SegDocDlg.h"
#include "WordSegment.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_WM_CLOSE()
ON_WM_TIMER()
ON_UPDATE_COMMAND_UI(ID_MAIN_CLASSIFY, OnUpdateMainClassify)
ON_UPDATE_COMMAND_UI(ID_MAIN_OPENMODEL, OnUpdateMainOpenmodel)
ON_COMMAND(ID_MAIN_OPENMODEL, OnMainOpenmodel)
ON_UPDATE_COMMAND_UI(ID_MAIN_STOPRUNNING, OnUpdateMainStoprunning)
ON_COMMAND(ID_MAIN_STOPRUNNING, OnMainStoprunning)
ON_UPDATE_COMMAND_UI(ID_MAIN_PAUSE, OnUpdateMainPause)
ON_COMMAND(ID_MAIN_PAUSE, OnMainPause)
ON_UPDATE_COMMAND_UI(ID_MAIN_RESUME, OnUpdateMainResume)
ON_COMMAND(ID_MAIN_RESUME, OnMainResume)
ON_UPDATE_COMMAND_UI(ID_MAIN_RESULTS, OnUpdateMainResults)
ON_COMMAND(ID_MAIN_RESULTS, OnMainResults)
ON_COMMAND(ID_MAIN_TEST, OnMainTest)
ON_UPDATE_COMMAND_UI(ID_MAIN_TEST, OnUpdateMainTest)
ON_COMMAND(ID_TOOLS_RESULTS, OnToolsResults)
ON_UPDATE_COMMAND_UI(ID_TOOLS_RESULTS, OnUpdateToolsResults)
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_CLASSIFY_SINGLEFILE, OnMainClassifySinglefile)
ON_UPDATE_COMMAND_UI(ID_MAIN_CLASSIFY_SINGLEFILE, OnUpdateMainClassifySinglefile)
ON_COMMAND(ID_TOOLS_CONVERT, OnToolsConvert)
ON_COMMAND(ID_TOOLS_SELDOC, OnToolsSeldoc)
ON_COMMAND(ID_TOOLS_WORDSEG, OnToolsWordseg)
ON_COMMAND(ID_MAIN_READ_FEATURES, OnMainReadFeatures)
ON_UPDATE_COMMAND_UI(ID_MAIN_READ_FEATURES, OnUpdateMainReadFeatures)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
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()
{
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_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
}
CMessage::SetStatusWnd(&m_wndStatusBar);
// 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::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(150, 150), pContext) ||
!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CSvmclsView), CSize(150, 150), pContext))
{
m_wndSplitter.DestroyWindow();
return FALSE;
}
CMessage::SetOutputWnd((CWnd *)m_wndSplitter.GetPane(0, 1));
return TRUE;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style&=~(LONG)FWS_ADDTOTITLE;
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -