📄 vsmclassifydlg.cpp
字号:
// VSMClassifyDlg.cpp : implementation file
//
#include "stdafx.h"
#include "VSMClassify.h"
#include "VSMClassifyDlg.h"
#include "Shellapi.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVSMClassifyDlg dialog
CVSMClassifyDlg::CVSMClassifyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CVSMClassifyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CVSMClassifyDlg)
m_fltWeight = 0.0f;
m_nWordFrea = 1;
m_fltCentral = 0.0f;
m_strClassifyPath = _T("D:\\workshop\\VSMClassify\\resource\\testfile\\1.txt");
m_strUpdatePath = _T("D:\\workshop\\VSMClassify\\resource\\Tree\\VSM.tree");
m_strTrainPath = _T("D:\\workshop\\VSMClassify\\resource\\data");
m_strTreePath = _T("D:\\workshop\\VSMClassify\\resource\\Tree\\VSM.tree");
m_strResultPath = _T("D:\\workshop\\VSMClassify\\resource\\result\\result.txt");
m_strIsTrainComplete = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
IsTrainOrUpdate=0;
}
void CVSMClassifyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CVSMClassifyDlg)
DDX_Text(pDX, IDC_WEIGHT, m_fltWeight);
DDX_Text(pDX, IDC_WORD_FREQ, m_nWordFrea);
DDX_Text(pDX, IDC_CENTRAL, m_fltCentral);
DDX_Text(pDX, IDC_CLASSIFYPATH, m_strClassifyPath);
DDX_Text(pDX, IDC_UPDATEPATH, m_strUpdatePath);
DDX_Text(pDX, IDC_TRAINPATH, m_strTrainPath);
DDX_Text(pDX, IDC_SAVETREEPATH, m_strTreePath);
DDX_Text(pDX, IDC_RESULTPATH, m_strResultPath);
DDX_Text(pDX, IDC_ISCOMPLETETRAIN, m_strIsTrainComplete);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CVSMClassifyDlg, CDialog)
//{{AFX_MSG_MAP(CVSMClassifyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CLASSIFY, OnClassify)
ON_BN_CLICKED(IDC_TRAIN, OnTrain)
ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
ON_BN_CLICKED(IDC_OPENCLASSIFY, OnOpenclassify)
ON_BN_CLICKED(IDC_OPENUPDATE, OnOpenupdate)
ON_BN_CLICKED(IDC_AUTOSLECTPARA, OnAutoslectpara)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVSMClassifyDlg message handlers
BOOL CVSMClassifyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
IntialTree();
return TRUE; // return TRUE unless you set the focus to a control
}
void CVSMClassifyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CVSMClassifyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CVSMClassifyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CVSMClassifyDlg::FindDirectoryFile(CString strDirectory,CString &strResult)
{
CFileFind finder;
CString strTemp;
BOOL bWorking=finder.FindFile(strDirectory.GetBuffer(0));
if(!bWorking)
{
AfxMessageBox("The file is not existed.");
return;
}
else
{
bWorking=finder.FindNextFile();
strTemp=strDirectory;
strTemp.MakeLower();
if(strTemp.Find(_T(".txt"))!=-1)
{
txtClassify->Classify(strDirectory.GetBuffer(0),strTemp);
strTemp=strDirectory+"====="+strTemp;
strResult=strResult+strTemp+"\n";
return;
}
}
//if not ,is a directory
strTemp=strDirectory+"\\*";
bWorking=finder.FindFile(strTemp.GetBuffer(0));
if(!bWorking)
{
AfxMessageBox("The file is not existed.");
return;
}
while(bWorking)
{
bWorking = finder.FindNextFile();
if(finder.IsDots())
continue;
strTemp=finder.GetFilePath();
FindDirectoryFile(strTemp,strResult);
}
}
double CVSMClassifyDlg::OnClassify()
{
UpdateData(TRUE);
if(!IsTrainOrUpdate)
{
AfxMessageBox("Train or update first!");
return 0;
}
//if m_strClassifyPath is a folder,classify all files in this folder
CString strResult;
FindDirectoryFile(m_strClassifyPath,strResult);
// AfxMessageBox(strResult);
//save the result to file
FILE *fp;
if((fp=fopen(m_strResultPath.GetBuffer(0),"w+b"))==NULL)
{
AfxMessageBox("the result file can not be opened .");
return 0;
}
int nLen=strResult.GetLength();
fwrite(strResult.GetBufferSetLength(nLen),1,nLen,fp);
fclose(fp);
//display the reslut;
/*
SHELLEXECUTEINFO sei;
ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.hwnd = m_hWnd;
sei.lpFile = m_strResultPath.GetBuffer(0);
sei.lpVerb = "open";
sei.nShow = SW_SHOW;
ShellExecuteEx(&sei);
*/
double resultScore;
txtClassify->veracity(&resultScore,m_strResultPath);
CString str;
str.Format("right rate:%f",resultScore);
AfxMessageBox(str);
return resultScore;
}
void CVSMClassifyDlg::OnTrain()
{
UpdateData(TRUE);
m_strIsTrainComplete="please wait";
UpdateData(FALSE);
txtClassify->TrainTree(m_strTrainPath,m_nWordFrea,m_fltWeight,m_fltCentral);
txtClassify->SaveTree(m_strTreePath.GetBuffer(0));
IsTrainOrUpdate=true;
m_strIsTrainComplete="complete training";
UpdateData(FALSE);
AfxMessageBox("complete training");
}
void CVSMClassifyDlg::OnUpdate()
{
UpdateData(TRUE);
if(!txtClassify->UpdateTree(m_strUpdatePath.GetBuffer(0)))
{
AfxMessageBox("Update tree error");
return;
}
IsTrainOrUpdate=true;
AfxMessageBox("updated");
}
void CVSMClassifyDlg::OnOpenclassify()
{
// TODO: Add your control notification handler code here
// TODO: Add your control notification handler code here
CFileDialog fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "TXT File(*.txt)|*.txt||", NULL );
fileDlg.DoModal();
m_strClassifyPath=fileDlg.GetPathName();
UpdateData(FALSE);
}
void CVSMClassifyDlg::OnOpenupdate()
{
// TODO: Add your control notification handler code here
CFileDialog fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "TreeFile(*.tree)|*.tree||", NULL );
fileDlg.DoModal();
m_strUpdatePath=fileDlg.GetPathName();
UpdateData(FALSE);
}
BOOL CVSMClassifyDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
//free the txtClassify
delete txtClassify;
return CDialog::DestroyWindow();
}
void CVSMClassifyDlg::IntialTree()
{
//create TextClassify
char *strWordList="resource//wordlist_30k.txt";
char *strStopWordList="resource//stopusing.txt";
txtClassify=new TextClassify(strWordList,strStopWordList);//delete when destroy window
if(!txtClassify->InitialTree())
AfxMessageBox("Initial tree error");
}
void CVSMClassifyDlg::OnAutoslectpara()
{
double weight,central,frequence,final_weight,final_central,final_frequence;
weight=0.0;
central=0;
frequence=1;
double centralmax=1;
double veracity=0;
m_strClassifyPath=m_strTrainPath;
//we just use training data as test data
//加循环
for(weight=0;weight<1;weight+=0.01)
{
for(frequence=0;frequence<2;frequence++)
{
for(central=0;central<centralmax;central+=0.01)
{
txtClassify->clear(m_strResultPath);
txtClassify->test(weight,central,frequence);
UpdateData(FALSE);
OnTrain();
double temp=OnClassify();
// AfxMessageBox("ok");
if(temp>veracity)
{
final_frequence=frequence;
final_central=central;
final_weight=weight;
veracity=temp;
}
}
}
}
m_nWordFrea=final_frequence;
m_fltWeight=final_weight;
m_fltCentral=final_central;
UpdateData(TRUE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -