MainFrm.cpp
上传用户:sanxfzhen
上传日期:2014-12-28
资源大小:2324k
文件大小:29k
源码类别:

多国语言处理

开发平台:

Visual C++

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3. #include "stdafx.h"
  4. #include "TextClassify.h"
  5. #include "Message.h"
  6. #include "MainFrm.h"
  7. #include "LeftView.h"
  8. #include "TextClassifyView.h"
  9. #include "SegDocDlg.h"
  10. #include "WordSegment.h"
  11. #include "TrainParamsPage.h"
  12. #include "TestParamsDlg.h"
  13. #include "classifier.h"
  14. #include "svmparamspage.h"
  15. #include <direct.h>
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CMainFrame
  23. CString CMainFrame::m_strResultDir;
  24. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  25. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  26. //{{AFX_MSG_MAP(CMainFrame)
  27. ON_WM_CREATE()
  28. ON_MESSAGE(WM_COMPUTATION_FINISH,OnComputationFinish)
  29. ON_COMMAND(ID_TOOLS_WORDSEG, OnToolsWordseg)
  30. ON_WM_CLOSE()
  31. ON_WM_TIMER()
  32. ON_COMMAND(ID_MAIN_TRAINKNN, OnMainTrainknn)
  33. ON_UPDATE_COMMAND_UI(ID_MAIN_TRAINKNN, OnUpdateMainTrainknn)
  34. ON_COMMAND(ID_MAIN_TRAINSVM, OnMainTrainsvm)
  35. ON_UPDATE_COMMAND_UI(ID_MAIN_TRAINSVM, OnUpdateMainTrainsvm)
  36. ON_COMMAND(ID_MAIN_TRAINBAYES,OnMainTrainbayes)
  37. ON_UPDATE_COMMAND_UI(ID_MAIN_TRAINBAYES,OnUpdateMainTrainbayes)
  38. ON_COMMAND(ID_MAIN_OPENMODEL, OnMainOpenmodel)
  39. ON_UPDATE_COMMAND_UI(ID_MAIN_OPENMODEL, OnUpdateMainOpenmodel)
  40. ON_COMMAND(ID_MAIN_RESULTS, OnMainResults)
  41. ON_UPDATE_COMMAND_UI(ID_MAIN_RESULTS, OnUpdateMainResults)
  42. ON_COMMAND(ID_MAIN_TEST, OnMainTest)
  43. ON_UPDATE_COMMAND_UI(ID_MAIN_TEST, OnUpdateMainTest)
  44. //}}AFX_MSG_MAP
  45. END_MESSAGE_MAP()
  46. static UINT indicators[] =
  47. {
  48. ID_SEPARATOR,           // status line indicator
  49. // ID_INDICATOR_CAPS,
  50. // ID_INDICATOR_NUM,
  51. // ID_INDICATOR_SCRL,
  52. // ID_INDICATOR_TIME,
  53. };
  54. UINT CMainFrame::TrainThreadProc(LPVOID pParam)
  55. {
  56. theClassifier.m_theSVM.com_param.running=1;
  57. theClassifier.Train(theClassifier.m_paramClassifier.m_nClassifierType);
  58. theClassifier.m_theSVM.com_param.running=0;
  59. ::PostMessage((HWND)pParam,WM_COMPUTATION_FINISH,0,0);
  60. return 0;
  61. }
  62. UINT CMainFrame::TestThreadProc(LPVOID pParam)
  63. {
  64. if(theClassifier.m_paramClassifier.m_nDocFormat==CClassifierParam::nDF_Directory)
  65. {
  66. if(theClassifier.Classify())
  67. CMainFrame::m_strResultDir = theClassifier.m_paramClassifier.m_strResultDir;
  68. }
  69. else if(theClassifier.m_paramClassifier.m_nDocFormat==CClassifierParam::nDF_Smart)
  70. {
  71. if(theClassifier.ClassifySmart())
  72. CMainFrame::m_strResultDir = theClassifier.m_paramClassifier.m_strResultDir;
  73. }
  74. ::PostMessage((HWND)pParam,WM_COMPUTATION_FINISH,0,0);
  75. return 0;
  76. }
  77. /*
  78. UINT CMainFrame::ConvertThreadProc(LPVOID pParam)
  79. {
  80. CTime startTime;
  81. CTimeSpan totalTime;
  82. CMessage::PrintInfo(_T("开始进行文档格式转换,请稍候..."));
  83. startTime=CTime::GetCurrentTime();
  84. theSaxFileHandler.Convert();
  85. totalTime=CTime::GetCurrentTime()-startTime;
  86. CMessage::PrintInfo(_T("文档格式转换结束,耗时")+totalTime.Format("%H:%M:%S"));
  87. CMessage::PrintStatusInfo("");
  88. ::PostMessage((HWND)pParam,WM_COMPUTATION_FINISH,0,0);
  89. ExitThread(0);
  90. return 0;
  91. }
  92. UINT CMainFrame::SeldocsThreadProc(LPVOID pParam)
  93. {
  94. CTime startTime;
  95. CTimeSpan totalTime;
  96. long lDocNum;
  97. CString str;
  98. CMessage::PrintInfo(_T("开始选择训练文档和测试文档,请稍候..."));
  99. startTime=CTime::GetCurrentTime();
  100. lDocNum=theSelectDocs.SelectDocs();
  101. totalTime=CTime::GetCurrentTime()-startTime;
  102. str.Format("共处理了%d篇文档!",lDocNum);
  103. CMessage::PrintInfo(str);
  104. CMessage::PrintInfo(_T("选择文档结束,耗时")+totalTime.Format("%H:%M:%S"));
  105. CMessage::PrintStatusInfo("");
  106. ::PostMessage((HWND)pParam,WM_COMPUTATION_FINISH,0,0);
  107. ExitThread(0);
  108. return 0;
  109. }
  110. */
  111. UINT CMainFrame::SegDocsThreadProc(LPVOID pParam)
  112. {
  113. CTime startTime;
  114. CTimeSpan totalTime;
  115. startTime=CTime::GetCurrentTime();
  116. CMessage::PrintInfo(_T("分词程序初始化,请稍候..."));
  117. if(!g_wordSeg.InitWorgSegment(theApp.m_strPath.GetBuffer(0)))
  118. {
  119. CMessage::PrintError(_T("分词程序初始化失败!"));
  120. return 0;
  121. }
  122. g_wordSeg.SetSegSetting(CWordSegment::uPlace);
  123. CMessage::PrintInfo(_T("正在进行分词,请稍候..."));
  124. if(theSegDocsParam.m_bMultiDocs)
  125. {
  126. CString strTarget,strSource;
  127. HANDLE hFinder;
  128. LPWIN32_FIND_DATA lpFindFileData;
  129. lpFindFileData  = new WIN32_FIND_DATA;
  130. hFinder = ::FindFirstFile(theSegDocsParam.m_strSource+"\*.*",lpFindFileData );
  131. while(::FindNextFile(hFinder,lpFindFileData))
  132. {
  133. if( !strcmp(lpFindFileData->cFileName,".") || !strcmp(lpFindFileData->cFileName,"..") )
  134. continue;
  135. if(!(lpFindFileData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  136. {
  137. strTarget=theSegDocsParam.m_strTarget+"\";
  138. strTarget+=lpFindFileData->cFileName;
  139. strSource=theSegDocsParam.m_strSource+"\";
  140. strSource+=lpFindFileData->cFileName;
  141. g_wordSeg.SegmentFile(strSource.GetBuffer(0),
  142. strTarget.GetBuffer(0),
  143. theSegDocsParam.m_bDelStopwords);
  144. CMessage::PrintStatusInfo(lpFindFileData->cFileName);
  145. }
  146. }
  147. delete lpFindFileData;
  148. }
  149. else
  150. g_wordSeg.SegmentFile(theSegDocsParam.m_strSource.GetBuffer(0),
  151. theSegDocsParam.m_strTarget.GetBuffer(0),
  152. theSegDocsParam.m_bDelStopwords);
  153. g_wordSeg.FreeWordSegment();
  154. totalTime=CTime::GetCurrentTime()-startTime;
  155. CMessage::PrintInfo(_T("分词结束,耗时")+totalTime.Format("%H:%M:%S"));
  156. CMessage::PrintStatusInfo("");
  157. ::PostMessage((HWND)pParam,WM_COMPUTATION_FINISH,0,0);
  158. ExitThread(0);
  159. return 0;
  160. }
  161. /////////////////////////////////////////////////////////////////////////////
  162. // CMainFrame construction/destruction
  163. CMainFrame::CMainFrame()
  164. {
  165. // TODO: add member initialization code here
  166. m_pThread=NULL;
  167. m_strModel.Empty();
  168. m_strResultDir.Empty();
  169. m_nRunning=0;
  170. m_bPaused=false;
  171. m_nClassifierType=0;
  172. }
  173. CMainFrame::~CMainFrame()
  174. {
  175. }
  176. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  177. {
  178. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  179. return -1;
  180. if (!m_wndStatusBar.Create(this) ||
  181. !m_wndStatusBar.SetIndicators(indicators,
  182.   sizeof(indicators)/sizeof(UINT)))
  183. {
  184. TRACE0("Failed to create status barn");
  185. return -1;      // fail to create
  186. }
  187. CMessage::SetStatusWnd(&m_wndStatusBar);
  188. return 0;
  189. }
  190. BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
  191. CCreateContext* pContext)
  192. {
  193. // create splitter window
  194. if (!m_wndSplitter.CreateStatic(this, 1, 2))
  195. return FALSE;
  196. if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftView), CSize(100, 100), pContext) ||
  197. !m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CTextClassifyView), CSize(100, 100), pContext))
  198. {
  199. m_wndSplitter.DestroyWindow();
  200. return FALSE;
  201. }
  202. CMessage::SetOutputWnd((CWnd *)m_wndSplitter.GetPane(0, 1));
  203. return TRUE;
  204. }
  205. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  206. {
  207. if( !CFrameWnd::PreCreateWindow(cs) )
  208. return FALSE;
  209. // TODO: Modify the Window class or styles here by modifying
  210. //  the CREATESTRUCT cs
  211. return TRUE;
  212. }
  213. /////////////////////////////////////////////////////////////////////////////
  214. // CMainFrame diagnostics
  215. #ifdef _DEBUG
  216. void CMainFrame::AssertValid() const
  217. {
  218. CFrameWnd::AssertValid();
  219. }
  220. void CMainFrame::Dump(CDumpContext& dc) const
  221. {
  222. CFrameWnd::Dump(dc);
  223. }
  224. #endif //_DEBUG
  225. /////////////////////////////////////////////////////////////////////////////
  226. // CMainFrame message handlers
  227. /*
  228. CSvmclsView* CMainFrame::GetRightPane()
  229. {
  230. CWnd* pWnd = m_wndSplitter.GetPane(0, 1);
  231. CSvmclsView* pView = DYNAMIC_DOWNCAST(CSvmclsView, pWnd);
  232. return pView;
  233. }
  234. CLeftView* CMainFrame::GetLeftView()
  235. {
  236. CWnd* pWnd = m_wndSplitter.GetPane(0, 0);
  237. CLeftView* pView = DYNAMIC_DOWNCAST(CLeftView, pWnd);
  238. return pView;
  239. }
  240. void CMainFrame::OnUpdateViewStyles(CCmdUI* pCmdUI)
  241. {
  242. // TODO: customize or extend this code to handle choices on the
  243. // View menu.
  244. CSvmclsView* pView = GetRightPane(); 
  245. // if the right-hand pane hasn't been created or isn't a view,
  246. // disable commands in our range
  247. if (pView == NULL)
  248. pCmdUI->Enable(FALSE);
  249. else
  250. {
  251. DWORD dwStyle = pView->GetStyle() & LVS_TYPEMASK;
  252. // if the command is ID_VIEW_LINEUP, only enable command
  253. // when we're in LVS_ICON or LVS_SMALLICON mode
  254. if (pCmdUI->m_nID == ID_VIEW_LINEUP)
  255. {
  256. if (dwStyle == LVS_ICON || dwStyle == LVS_SMALLICON)
  257. pCmdUI->Enable();
  258. else
  259. pCmdUI->Enable(FALSE);
  260. }
  261. else
  262. {
  263. // otherwise, use dots to reflect the style of the view
  264. pCmdUI->Enable();
  265. BOOL bChecked = FALSE;
  266. switch (pCmdUI->m_nID)
  267. {
  268. case ID_VIEW_DETAILS:
  269. bChecked = (dwStyle == LVS_REPORT);
  270. break;
  271. case ID_VIEW_SMALLICON:
  272. bChecked = (dwStyle == LVS_SMALLICON);
  273. break;
  274. case ID_VIEW_LARGEICON:
  275. bChecked = (dwStyle == LVS_ICON);
  276. break;
  277. case ID_VIEW_LIST:
  278. bChecked = (dwStyle == LVS_LIST);
  279. break;
  280. default:
  281. bChecked = FALSE;
  282. break;
  283. }
  284. pCmdUI->SetRadio(bChecked ? 1 : 0);
  285. }
  286. }
  287. }
  288. */
  289. void CMainFrame::OnComputationFinish(WPARAM wParam,LPARAM lParam)
  290. {
  291. CeaseComputation();
  292. }
  293. void CMainFrame::CeaseComputation()
  294. {
  295. m_wndStatusBar.SetPaneText(0,"运算全部结束!");
  296. m_nRunning=0;
  297. m_pThread=NULL;
  298. m_bPaused=false;
  299. KillTimer(m_nTimer);
  300. }
  301. void CMainFrame::BeginComputation()
  302. {
  303. m_nRunning=1;
  304. m_nTimer=SetTimer(1001,500,NULL);
  305. m_tmBegin=CTime::GetCurrentTime();
  306. }
  307. /*
  308. void CMainFrame::OnTimer(UINT nIDEvent) 
  309. {
  310. CTimeSpan t=CTime::GetCurrentTime()-m_tmBegin;
  311. m_wndStatusBar.SetPaneText(1,t.Format("耗时%H:%M:%S"),TRUE);
  312. CFrameWnd::OnTimer(nIDEvent);
  313. }
  314. void CMainFrame::OnClose() 
  315. {
  316. GetActiveDocument()->SetModifiedFlag(false);
  317. if(m_nRunning>0)
  318. {
  319. if(AfxMessageBox("正在进行计算,真的要停止吗?",MB_YESNO)==IDYES)
  320. {
  321. StopComputation();
  322. CFrameWnd::OnClose();
  323. }
  324. }
  325. else
  326. CFrameWnd::OnClose();
  327. }
  328. */
  329. void CMainFrame::Train()
  330. {
  331. CMessage::ClearInfo();
  332. BeginComputation();
  333. m_pThread=AfxBeginThread(TrainThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
  334. }
  335. void CMainFrame::Test() 
  336. {
  337. CMessage::ClearInfo();
  338. BeginComputation();
  339. m_pThread=AfxBeginThread(TestThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
  340. }
  341. /*
  342. void CMainFrame::OnMainClassifySinglefile() 
  343. {
  344. static char BASED_CODE szTextFilter[] = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*||";
  345. CFileDialog cfd(TRUE,szTextFilter,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szTextFilter,NULL);
  346. if (cfd.DoModal()==IDOK) 
  347. {
  348. CString strFile=cfd.GetPathName();
  349. short id;
  350. theClassifier.m_paramClassifier.m_dThreshold     =   60;
  351. theClassifier.m_paramClassifier.m_nKNN           =   35;
  352. theClassifier.m_paramClassifier.m_nClassifyType  =   CClassifierParam::nFT_Single;
  353. theClassifier.m_paramClassifier.m_bEvaluation    =   FALSE;
  354. theClassifier.m_paramClassifier.m_bCopyFiles     =   FALSE;
  355. if(theClassifier.m_paramClassifier.m_nClassifierType==CClassifierParam::nCT_KNN)
  356. id=theClassifier.KNNCategory(strFile.GetBuffer(0));
  357. else if(theClassifier.m_paramClassifier.m_nClassifierType==CClassifierParam::nCT_SVM)
  358. id=theClassifier.SVMCategory(strFile.GetBuffer(0));
  359. else
  360. {
  361. CMessage::PrintError("分类器类型不正确!");
  362. return;
  363. }
  364. CMessage::ClearInfo();
  365. if(id>=0) theClassifier.m_lstTrainCatalogList.GetCataName(id,strFile);
  366. else strFile="无法识别";
  367. CMessage::PrintInfo("文档的类别为: "+strFile);
  368. }
  369. }
  370. */
  371. void CMainFrame::OnMainTest() 
  372. {
  373. CTestParamsDlg testParamsDlg;
  374. if(theClassifier.m_paramClassifier.m_strTestDir.IsEmpty())
  375. theClassifier.m_paramClassifier.m_strTestDir=theApp.m_strPath+"\sample\test";
  376. if(theClassifier.m_paramClassifier.m_strResultDir.IsEmpty())
  377. theClassifier.m_paramClassifier.m_strResultDir=theClassifier.m_paramClassifier.m_txtResultDir;
  378. if((theClassifier.m_paramClassifier.m_nClassifierType==CClassifierParam::nCT_SVM)||
  379. (theClassifier.m_paramClassifier.m_dThreshold>0))
  380. {
  381. theClassifier.m_paramClassifier.m_nKNN=35;
  382. theClassifier.m_paramClassifier.m_dThreshold=0;
  383. }
  384. testParamsDlg.m_strTestDir    = theClassifier.m_paramClassifier.m_strTestDir;
  385. testParamsDlg.m_strResultDir  = theClassifier.m_paramClassifier.m_strResultDir;
  386. testParamsDlg.m_nDocFormat    = theClassifier.m_paramClassifier.m_nDocFormat;
  387. testParamsDlg.m_dThreshold    = theClassifier.m_paramClassifier.m_dThreshold;
  388. testParamsDlg.m_nKNN          = theClassifier.m_paramClassifier.m_nKNN;
  389. testParamsDlg.m_nClassifyType = theClassifier.m_paramClassifier.m_nClassifyType;
  390. testParamsDlg.m_bEvaluation   = theClassifier.m_paramClassifier.m_bEvaluation;
  391. testParamsDlg.m_bCopyFiles    = theClassifier.m_paramClassifier.m_bCopyFiles;
  392. testParamsDlg.SetClassifierType(theClassifier.m_paramClassifier.m_nClassifierType);
  393. if(testParamsDlg.DoModal()==IDOK)
  394. {
  395. if((testParamsDlg.m_nDocFormat==CClassifierParam::nDF_Directory)&&
  396. !CheckPath(testParamsDlg.m_strTestDir)) return;
  397. if(!CheckPath(testParamsDlg.m_strResultDir,true)) return;
  398. theClassifier.m_paramClassifier.m_strTestDir     =   testParamsDlg.m_strTestDir;
  399. theClassifier.m_paramClassifier.m_strResultDir   =   testParamsDlg.m_strResultDir;
  400. theClassifier.m_paramClassifier.m_nDocFormat     =   testParamsDlg.m_nDocFormat; 
  401. theClassifier.m_paramClassifier.m_dThreshold     =   testParamsDlg.m_dThreshold;
  402. theClassifier.m_paramClassifier.m_nKNN           =   testParamsDlg.m_nKNN ;
  403. theClassifier.m_paramClassifier.m_nClassifyType  =   testParamsDlg.m_nClassifyType;
  404. theClassifier.m_paramClassifier.m_bEvaluation    =   testParamsDlg.m_bEvaluation;
  405. theClassifier.m_paramClassifier.m_bCopyFiles     =   testParamsDlg.m_bCopyFiles;
  406. Test();
  407. }
  408. }
  409. /*
  410. void CMainFrame::StopComputation()
  411. {
  412. if((m_nRunning>=1)&&(m_pThread!=NULL))
  413. {
  414. if(TerminateThread(m_pThread->m_hThread,1))
  415. {
  416. CeaseComputation();
  417. }
  418. else
  419. AfxMessageBox("无法停止计算过程!");
  420. }
  421. }
  422. void CMainFrame::OnUpdateMainReadFeatures(CCmdUI* pCmdUI) 
  423. {
  424. pCmdUI->Enable(m_nRunning<=0);
  425. }
  426. */
  427. void CMainFrame::OnUpdateMainTrainknn(CCmdUI* pCmdUI) 
  428. {
  429. pCmdUI->Enable(m_nRunning<=0);
  430. }
  431. void CMainFrame::OnUpdateMainTrainsvm(CCmdUI* pCmdUI) 
  432. {
  433. pCmdUI->Enable(m_nRunning<=0);
  434. }
  435. void CMainFrame::OnUpdateMainTrainbayes(CCmdUI* pCmdUI)
  436. {
  437. pCmdUI->Enable(m_nRunning<=0);
  438. }
  439. void CMainFrame::OnUpdateMainTest(CCmdUI* pCmdUI) 
  440. {
  441. pCmdUI->Enable(m_nRunning<=0&&!m_strModel.IsEmpty());
  442. }
  443. /*
  444. void CMainFrame::OnUpdateMainClassify(CCmdUI* pCmdUI) 
  445. {
  446. pCmdUI->Enable(m_nRunning<=0&&!m_strModel.IsEmpty());
  447. }
  448. void CMainFrame::OnUpdateMainClassifySinglefile(CCmdUI* pCmdUI) 
  449. {
  450. pCmdUI->Enable(m_nRunning<=0&&!m_strModel.IsEmpty());
  451. }
  452. */
  453. void CMainFrame::OnUpdateMainResults(CCmdUI* pCmdUI) 
  454. {
  455. pCmdUI->Enable(m_nRunning<=0&&!m_strResultDir.IsEmpty());
  456. }
  457.   
  458. void CMainFrame::OnUpdateMainOpenmodel(CCmdUI* pCmdUI) 
  459. {
  460. pCmdUI->Enable(m_nRunning<=0);
  461. }
  462. /*
  463. void CMainFrame::OnUpdateMainStoprunning(CCmdUI* pCmdUI) 
  464. {
  465. pCmdUI->Enable(m_nRunning);
  466. }
  467. void CMainFrame::OnUpdateMainPause(CCmdUI* pCmdUI) 
  468. {
  469. pCmdUI->Enable(m_nRunning&&!m_bPaused);
  470. }
  471. void CMainFrame::OnUpdateMainResume(CCmdUI* pCmdUI) 
  472. {
  473. pCmdUI->Enable(m_nRunning&&m_bPaused);
  474. }
  475. void CMainFrame::OnUpdateToolsConvert(CCmdUI* pCmdUI) 
  476. {
  477. pCmdUI->Enable(m_nRunning<=0);
  478. }
  479. void CMainFrame::OnUpdateToolsResults(CCmdUI* pCmdUI) 
  480. {
  481. pCmdUI->Enable(m_nRunning<=0);
  482. }
  483. void CMainFrame::OnMainStoprunning() 
  484. {
  485. if(AfxMessageBox("正在进行计算,真的要停止吗?",MB_YESNO)==IDYES)
  486. StopComputation();
  487. }
  488. */
  489. void CMainFrame::OnMainOpenmodel() 
  490. {
  491. static char BASED_CODE szModelFilter[] = "Model Files (*.prj)|*.prj|All Files (*.*)|*.*||";
  492. CFileDialog cfd(TRUE,szModelFilter,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szModelFilter,NULL);
  493. if (cfd.DoModal()==IDOK) 
  494. {
  495. m_strModel=cfd.GetPathName();
  496. CMessage::ClearInfo();
  497. if(!theClassifier.OpenModel(m_strModel)) m_strModel.Empty();
  498. }
  499. }
  500. /*
  501. void CMainFrame::OnMainPause() 
  502. {
  503. if(!m_bPaused)
  504. {
  505. m_pThread->SuspendThread();
  506. m_wndStatusBar.SetPaneText(0,"运算被用户暂停了!");
  507. m_bPaused=true;
  508. }
  509. }
  510. void CMainFrame::OnMainResume() 
  511. {
  512. if(m_bPaused)
  513. {
  514. m_pThread->ResumeThread();
  515. m_wndStatusBar.SetPaneText(0,"恢复正常运算了!");
  516. m_bPaused=false;
  517. }
  518. }
  519. */
  520. void CMainFrame::OnMainResults() 
  521. {
  522. if(theClassifier.m_paramClassifier.m_bEvaluation)
  523. theClassifier.Evaluate(theApp.m_strPath);
  524. else
  525. {
  526. CMessage::ClearInfo();
  527. CFile fin;
  528. CString fname=theClassifier.m_paramClassifier.m_strResultDir+"\results.txt";
  529. if(!fin.Open(fname,CFile::modeRead))
  530. {
  531. CMessage::PrintError(_T("分类结果文件")+fname+"不存在!");
  532. return;
  533. }
  534. DWORD flen=fin.GetLength();
  535. char *buffer=new char[flen+1];
  536. flen=fin.ReadHuge(buffer,flen);
  537. buffer[flen]='';
  538. fin.Close();
  539. CMessage::PrintInfo("----------------------分类结果----------------------rn");
  540. CMessage::PrintInfo("文档IDt名称tt所属类别rn");
  541. CMessage::PrintInfo(buffer);
  542. delete[] buffer;
  543. }
  544. }
  545. /*
  546. void CMainFrame::OnToolsResults() 
  547. {
  548. CString strFileName=theApp.m_strPath;
  549. strFileName+="\multieval.exe";
  550. if(WinExec(strFileName,SW_SHOWNORMAL)<32)
  551. AfxMessageBox("分类结果评测程序不存在!");
  552. }
  553. void CMainFrame::OnMainReadFeatures() 
  554. {
  555. static char BASED_CODE szModelFilter[] = "Features Files (*.txt)|*.txt|All Files (*.*)|*.*||";
  556. CFileDialog cfd(TRUE,szModelFilter,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szModelFilter,NULL);
  557. if (cfd.DoModal()==IDOK) 
  558. {
  559. CString strFileName;
  560. strFileName=cfd.GetPathName();
  561. CMessage::ClearInfo();
  562. theClassifier.m_lstTrainWordList.GetListFromFile(strFileName);
  563. }
  564. }
  565. */
  566. void CMainFrame::OnMainTrainknn() 
  567. {
  568. CPropertySheet paramsSheet;
  569. CTrainParamsPage trainParamsPage;
  570. if(theClassifier.m_paramClassifier.m_txtTrainDir.IsEmpty())
  571. theClassifier.m_paramClassifier.m_txtTrainDir=theApp.m_strPath+"\sample\train";
  572. if(theClassifier.m_paramClassifier.m_txtResultDir.IsEmpty())
  573. theClassifier.m_paramClassifier.m_txtResultDir=theApp.m_strPath+"\sample\knn";
  574. trainParamsPage.m_txtTrainDir  = theClassifier.m_paramClassifier.m_txtTrainDir;
  575. trainParamsPage.m_txtResultDir = theClassifier.m_paramClassifier.m_txtResultDir;
  576. trainParamsPage.m_nFSMode      = theClassifier.m_paramClassifier.m_nFSMode;
  577. trainParamsPage.m_nWordSize    = theClassifier.m_paramClassifier.m_nWordSize;
  578. trainParamsPage.m_nSelMode     = theClassifier.m_paramClassifier.m_nSelMode;
  579. trainParamsPage.m_nOpMode      = theClassifier.m_paramClassifier.m_nOpMode;
  580. trainParamsPage.m_nLanguageType= theClassifier.m_paramClassifier.m_nLanguageType;
  581. trainParamsPage.m_bStem        = theClassifier.m_paramClassifier.m_bStem;
  582. trainParamsPage.m_nWeightMode  = theClassifier.m_paramClassifier.m_nWeightMode;
  583. //paramsSheet.SetWizardMode();
  584. paramsSheet.SetTitle("参数设置");
  585. paramsSheet.AddPage(&trainParamsPage);
  586. if(paramsSheet.DoModal()==IDOK)
  587. {
  588. if(!CheckPath(trainParamsPage.m_txtTrainDir)||
  589. !CheckPath(trainParamsPage.m_txtResultDir,true))
  590. return;
  591. theClassifier.m_paramClassifier.m_txtTrainDir    =   trainParamsPage.m_txtTrainDir;
  592. theClassifier.m_paramClassifier.m_txtResultDir   =   trainParamsPage.m_txtResultDir;
  593. theClassifier.m_paramClassifier.m_nFSMode        =   trainParamsPage.m_nFSMode;
  594. theClassifier.m_paramClassifier.m_nWordSize      =   trainParamsPage.m_nWordSize;
  595. theClassifier.m_paramClassifier.m_nSelMode       =   trainParamsPage.m_nSelMode;
  596. theClassifier.m_paramClassifier.m_nOpMode        =   trainParamsPage.m_nOpMode;
  597. theClassifier.m_paramClassifier.m_nLanguageType  =   trainParamsPage.m_nLanguageType;
  598. theClassifier.m_paramClassifier.m_bStem          =   trainParamsPage.m_bStem;
  599. theClassifier.m_paramClassifier.m_nWeightMode    =   trainParamsPage.m_nWeightMode;
  600. theClassifier.m_paramClassifier.m_nClassifierType=   CClassifierParam::nCT_KNN;
  601. Train();
  602. m_strModel=theClassifier.m_paramClassifier.m_txtResultDir+"\model.prj";
  603. theClassifier.m_paramClassifier.m_strModelFile="model";
  604. }
  605. }
  606. void CMainFrame::OnMainTrainsvm() 
  607. {
  608. // TODO: Add your command handler code here
  609. CPropertySheet paramsSheet;
  610. CSVMParamsPage   svmParamsPage;
  611. CTrainParamsPage trainParamsPage;
  612. if(theClassifier.m_paramClassifier.m_txtTrainDir.IsEmpty())
  613. theClassifier.m_paramClassifier.m_txtTrainDir=theApp.m_strPath+"\sample\train";
  614. if(theClassifier.m_paramClassifier.m_txtResultDir.IsEmpty())
  615. theClassifier.m_paramClassifier.m_txtResultDir=theApp.m_strPath+"\sample\svm";
  616. trainParamsPage.m_txtTrainDir  = theClassifier.m_paramClassifier.m_txtTrainDir;
  617. trainParamsPage.m_txtResultDir = theClassifier.m_paramClassifier.m_txtResultDir;
  618. trainParamsPage.m_nFSMode      = theClassifier.m_paramClassifier.m_nFSMode;
  619. trainParamsPage.m_nWordSize    = theClassifier.m_paramClassifier.m_nWordSize;
  620. trainParamsPage.m_nSelMode     = theClassifier.m_paramClassifier.m_nSelMode;
  621. trainParamsPage.m_nOpMode      = theClassifier.m_paramClassifier.m_nOpMode;
  622. trainParamsPage.m_nLanguageType= theClassifier.m_paramClassifier.m_nLanguageType;
  623. trainParamsPage.m_bStem        = theClassifier.m_paramClassifier.m_bStem;
  624. trainParamsPage.m_nWeightMode  = theClassifier.m_paramClassifier.m_nWeightMode;
  625. svmParamsPage.m_bB=theClassifier.m_theSVM.com_param.biased_Hyperplane;
  626. svmParamsPage.m_bI=theClassifier.m_theSVM.com_param.remove_inconsitant;
  627. svmParamsPage.m_dC=theClassifier.m_theSVM.com_param.C;
  628. svmParamsPage.m_dJ=theClassifier.m_theSVM.com_param.cost_factor;
  629. svmParamsPage.m_bX=theClassifier.m_theSVM.com_param.loo;
  630. svmParamsPage.m_fK=theClassifier.m_theSVM.com_param.search_depth;
  631. svmParamsPage.m_fO=theClassifier.m_theSVM.com_param.rho;
  632. svmParamsPage.m_fP=theClassifier.m_theSVM.com_param.fraction;
  633. svmParamsPage.m_fG=theClassifier.m_theSVM.com_param.rbf_gamma;
  634. svmParamsPage.m_fC=theClassifier.m_theSVM.com_param.poly_c;
  635. svmParamsPage.m_fS=theClassifier.m_theSVM.com_param.poly_s;
  636. svmParamsPage.m_iD=theClassifier.m_theSVM.com_param.poly_degree;
  637. svmParamsPage.m_iKernelType=theClassifier.m_theSVM.com_param.kernel_type;
  638. svmParamsPage.m_fE=theClassifier.m_theSVM.com_param.epsion;
  639. svmParamsPage.m_lgIterationTime=theClassifier.m_theSVM.com_param.iteration_time;
  640. svmParamsPage.m_fM=theClassifier.m_theSVM.com_param.cache_size;
  641. svmParamsPage.m_lgNewVariable=theClassifier.m_theSVM.com_param.new_variable;
  642. svmParamsPage.m_lgMaxQPSize=theClassifier.m_theSVM.com_param.maximum_size;
  643. svmParamsPage.m_bFinal=theClassifier.m_theSVM.com_param.final_test;
  644. //paramsSheet.SetWizardMode();
  645. paramsSheet.SetTitle("参数设置");
  646. paramsSheet.AddPage(&trainParamsPage);
  647. paramsSheet.AddPage(&svmParamsPage);
  648. if(paramsSheet.DoModal()==IDOK)
  649. {
  650. if(!CheckPath(trainParamsPage.m_txtTrainDir)||
  651. !CheckPath(trainParamsPage.m_txtResultDir,true))
  652. return;
  653. theClassifier.m_theSVM.com_param.biased_Hyperplane=svmParamsPage.m_bB ;
  654. theClassifier.m_theSVM.com_param.remove_inconsitant=svmParamsPage.m_bI;
  655. theClassifier.m_theSVM.com_param.C                =  svmParamsPage.m_dC;
  656. theClassifier.m_theSVM.com_param.cost_factor      =  svmParamsPage.m_dJ;
  657. theClassifier.m_theSVM.com_param.loo              =  svmParamsPage.m_bX;
  658. theClassifier.m_theSVM.com_param.search_depth     =  svmParamsPage.m_fK;
  659. theClassifier.m_theSVM.com_param.rho              =  svmParamsPage.m_fO;
  660. theClassifier.m_theSVM.com_param.fraction         =  svmParamsPage.m_fP;
  661. theClassifier.m_theSVM.com_param.rbf_gamma        =  svmParamsPage.m_fG  ;
  662. theClassifier.m_theSVM.com_param.poly_c           =  svmParamsPage.m_fC;
  663. theClassifier.m_theSVM.com_param.poly_s           =  svmParamsPage.m_fS;
  664. theClassifier.m_theSVM.com_param.poly_degree      =  svmParamsPage.m_iD  ;
  665. theClassifier.m_theSVM.com_param.kernel_type      =  svmParamsPage.m_iKernelType ;
  666. theClassifier.m_theSVM.com_param.epsion           =  svmParamsPage.m_fE;
  667. theClassifier.m_theSVM.com_param.iteration_time   =  svmParamsPage.m_lgIterationTime;
  668. theClassifier.m_theSVM.com_param.cache_size       =  svmParamsPage.m_fM  ;
  669. theClassifier.m_theSVM.com_param.new_variable     =  svmParamsPage.m_lgNewVariable;
  670. theClassifier.m_theSVM.com_param.maximum_size     =  svmParamsPage.m_lgMaxQPSize;
  671. theClassifier.m_theSVM.com_param.final_test       =  svmParamsPage.m_bFinal  ;
  672. theClassifier.m_paramClassifier.m_txtTrainDir    =   trainParamsPage.m_txtTrainDir;
  673. theClassifier.m_paramClassifier.m_txtResultDir   =   trainParamsPage.m_txtResultDir;
  674. theClassifier.m_paramClassifier.m_nFSMode        =   trainParamsPage.m_nFSMode;
  675. theClassifier.m_paramClassifier.m_nWordSize      =   trainParamsPage.m_nWordSize;
  676. theClassifier.m_paramClassifier.m_nSelMode       =   trainParamsPage.m_nSelMode;
  677. theClassifier.m_paramClassifier.m_nOpMode        =   trainParamsPage.m_nOpMode;
  678. theClassifier.m_paramClassifier.m_nLanguageType  =   trainParamsPage.m_nLanguageType;
  679. theClassifier.m_paramClassifier.m_bStem          =   trainParamsPage.m_bStem;
  680. theClassifier.m_paramClassifier.m_nWeightMode    =   trainParamsPage.m_nWeightMode;
  681. theClassifier.m_paramClassifier.m_nClassifierType=   CClassifierParam::nCT_SVM;
  682. Train();
  683. m_strModel=theClassifier.m_paramClassifier.m_txtResultDir+"\model.prj";
  684. theClassifier.m_paramClassifier.m_strModelFile="model";
  685. }
  686. }
  687. void CMainFrame::OnMainTrainbayes()
  688. {
  689. CPropertySheet paramsSheet;
  690. CTrainParamsPage trainParamsPage;
  691. if(theClassifier.m_paramClassifier.m_txtTrainDir.IsEmpty())
  692. theClassifier.m_paramClassifier.m_txtTrainDir=theApp.m_strPath+"\sample\train";
  693. if(theClassifier.m_paramClassifier.m_txtResultDir.IsEmpty())
  694. theClassifier.m_paramClassifier.m_txtResultDir=theApp.m_strPath+"\sample\bayes";
  695. trainParamsPage.m_txtTrainDir  = theClassifier.m_paramClassifier.m_txtTrainDir;
  696. trainParamsPage.m_txtResultDir = theClassifier.m_paramClassifier.m_txtResultDir;
  697. trainParamsPage.m_nFSMode      = theClassifier.m_paramClassifier.m_nFSMode;
  698. trainParamsPage.m_nWordSize    = theClassifier.m_paramClassifier.m_nWordSize;
  699. trainParamsPage.m_nSelMode     = theClassifier.m_paramClassifier.m_nSelMode;
  700. trainParamsPage.m_nOpMode      = theClassifier.m_paramClassifier.m_nOpMode;
  701. trainParamsPage.m_nLanguageType= theClassifier.m_paramClassifier.m_nLanguageType;
  702. trainParamsPage.m_bStem        = theClassifier.m_paramClassifier.m_bStem;
  703. trainParamsPage.m_nWeightMode  = theClassifier.m_paramClassifier.m_nWeightMode;
  704. //paramsSheet.SetWizardMode();
  705. paramsSheet.SetTitle("参数设置");
  706. paramsSheet.AddPage(&trainParamsPage);
  707. if(paramsSheet.DoModal()==IDOK)
  708. {
  709. if(!CheckPath(trainParamsPage.m_txtTrainDir)||
  710. !CheckPath(trainParamsPage.m_txtResultDir,true))
  711. return;
  712. theClassifier.m_paramClassifier.m_txtTrainDir    =   trainParamsPage.m_txtTrainDir;
  713. theClassifier.m_paramClassifier.m_txtResultDir   =   trainParamsPage.m_txtResultDir;
  714. theClassifier.m_paramClassifier.m_nFSMode        =   trainParamsPage.m_nFSMode;
  715. theClassifier.m_paramClassifier.m_nWordSize      =   trainParamsPage.m_nWordSize;
  716. theClassifier.m_paramClassifier.m_nSelMode       =   trainParamsPage.m_nSelMode;
  717. theClassifier.m_paramClassifier.m_nOpMode        =   trainParamsPage.m_nOpMode;
  718. theClassifier.m_paramClassifier.m_nLanguageType  =   trainParamsPage.m_nLanguageType;
  719. theClassifier.m_paramClassifier.m_bStem          =   trainParamsPage.m_bStem;
  720. theClassifier.m_paramClassifier.m_nWeightMode    =   trainParamsPage.m_nWeightMode;
  721. theClassifier.m_paramClassifier.m_nClassifierType=   CClassifierParam::nCT_BAYES;
  722. Train();
  723. m_strModel=theClassifier.m_paramClassifier.m_txtResultDir+"\model.prj";
  724. theClassifier.m_paramClassifier.m_strModelFile="model";
  725. }
  726. }
  727. bool CMainFrame::CheckPath(CString &strPath, bool bCreateDir)
  728. {
  729. if(_chdir(strPath)<0)
  730. {
  731. if(bCreateDir)
  732. {
  733. if(_mkdir(strPath)<0)
  734. {
  735. CMessage::PrintError("无法创建目录"+strPath+"!");
  736. return false;
  737. }
  738. }
  739. else
  740. {
  741. CMessage::PrintError("目录"+strPath+"不存在!");
  742. return false;
  743. }
  744. }
  745. if(strPath[strPath.GetLength()-1]=='\')
  746. strPath=strPath.Left(strPath.GetLength()-1);
  747. return true;
  748. }
  749. /*
  750. void CMainFrame::OnToolsConvert() 
  751. {
  752. CConvertDlg dlg;
  753. if(dlg.DoModal()==IDOK)
  754. {
  755. if(dlg.m_nSourceFormat==0&&!CheckPath(dlg.m_strSource)) return;
  756. if(dlg.m_nTargetFormat==0&&!CheckPath(dlg.m_strTarget,true)) return;
  757. strcpy(theSaxFileHandler.m_pTarget,dlg.m_strTarget);
  758. strcpy(theSaxFileHandler.m_pSource,dlg.m_strSource);
  759. theSaxFileHandler.m_nTargetFormat=dlg.m_nTargetFormat;
  760. theSaxFileHandler.m_nSourceFormat=dlg.m_nSourceFormat;
  761. theSaxFileHandler.m_bShowErrors=dlg.m_bShowErrors;
  762. theSaxFileHandler.m_nSplitType=dlg.m_nSplitType;
  763. theSaxFileHandler.m_nDocsSetType=dlg.m_nDocsSetType;
  764. theSaxFileHandler.m_nCategoryNum=dlg.m_nCategoryNum;
  765. CMessage::ClearInfo();
  766. BeginComputation();
  767. m_pThread=AfxBeginThread(ConvertThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
  768. }
  769. }
  770. void CMainFrame::OnToolsSeldoc() 
  771. {
  772. // TODO: Add your command handler code here
  773. CDocSelDlg dlg;
  774. dlg.m_strSource=theSelectDocs.m_strSource;
  775. dlg.m_strTarget=theSelectDocs.m_strTarget;
  776. dlg.m_nPercent=theSelectDocs.m_nPercent;
  777. if(dlg.DoModal()==IDOK)
  778. {
  779. theSelectDocs.m_strSource=dlg.m_strSource;
  780. theSelectDocs.m_strTarget=dlg.m_strTarget;
  781. theSelectDocs.m_nPercent=dlg.m_nPercent;
  782. CMessage::ClearInfo();
  783. BeginComputation();
  784. m_pThread=AfxBeginThread(SeldocsThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
  785. }
  786. }
  787. */
  788. void CMainFrame::OnToolsWordseg() 
  789. {
  790. CSegDocDlg dlg;
  791. if(dlg.DoModal()==IDOK)
  792. {
  793. CMessage::ClearInfo();
  794. BeginComputation();
  795. m_pThread=AfxBeginThread(SegDocsThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
  796. }
  797. }