📄 test.cpp
字号:
/*==========================================================
*
* Copyright (c) 2008
* All rights reserved.
*
* 文件名称: Test.cpp
* 摘 要: 测试向量
*
* 当前版本: 1.0
* 作 者: 祝美莲
* 创建日期: 2008-5-10
*
*==========================================================
*/
// Test.cpp: implementation of the CTest class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DeTreeSVM.h"
#include "Test.h"
#include "DeTreeSVMDlg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern CDeTreeSVMDlg *p;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
/********************************************************************
// 创建日期 : 2008-5-19 20:51:51
// 作 者 : 祝美莲<zml123818@163.com>
// 函数名称 : CTest::CTest
// 函数功能 : 构造函数
// 返回类型 :
// 函数参数 : int nKNumber
// 函数参数 : int nViNumber
// 函数参数 : int nTemp[]
********************************************************************/
CTest::CTest(int nKNumber, int nViNumber, int nTemp[])
{
nKindNumber = nKNumber;
nVectorItemNumber = nViNumber;
for(int i=0; i<KINDNUMBER; i++)
{
nRight[i] = 0;
nTotal[i] = 0;
nTotalTest[i]= 0;
nKind[i] = nTemp[i];
}
}
CTest::CTest()
{
nKindNumber = KINDNUMBER;
nVectorItemNumber = VECTORITEMNUMBER;
for(int i=0; i<KINDNUMBER; i++)
{
nRight[i] = 0;
nTotal[i] = 0;
nTotalTest[i]= 0;
}
}
CTest::~CTest()
{
}
/********************************************************************
// 创建日期 : 2008-5-19 20:52:53
// 作 者 : 祝美莲<zml123818@163.com>
// 函数名称 : CTest::Transform
// 函数功能 : 转化数据,方便计算
// 返回类型 : void
// 函数参数 : CPageInfo page
// 函数参数 : double VX[VECTORITEMNUMBER]
********************************************************************/
void CTest::Transform( CPageInfo page, double VX[VECTORITEMNUMBER] )
{
int n = 0;
VectorItem vi;//特征向量的一个分量
//赋一个初值
vi = page.GetCharacterVector( n );
for(int t=0; t<VECTORITEMNUMBER; t++)
{
if(t==vi.nIndex)
{
VX[t] = vi.dPower;
n++;
if(n < page.GetSize())
vi = page.GetCharacterVector( n );
}
else
VX[t] = 0;
}
}
/********************************************************************
// 创建日期 : 2008-5-19 19:00:18
// 作 者 : 祝美莲<zml123818@163.com>
// 函数名称 : CTest::DotMetrix
// 函数功能 : 计算两个向量的点积
// 返回类型 : double
// 函数参数 : double VX1[VECTORITEMNUMBER]
// 函数参数 : double VX2[VECTORITEMNUMBER]
********************************************************************/
double CTest::DotMetrix(double VX1[VECTORITEMNUMBER],double VX2[VECTORITEMNUMBER])
{
double sum = 0;//初值
for(int i=0; i<nVectorItemNumber; i++)
sum += VX1[i] * VX2[i];
return sum;
}
/********************************************************************
// 创建日期 : 2008-5-19 19:01:13
// 作 者 : 祝美莲<zml123818@163.com>
// 函数名称 : CTest::CreateResultDirectory
// 函数功能 : 创建文件夹存放分类后的实际网页
// 返回类型 : BOOL
********************************************************************/
BOOL CTest::CreateResultDirectory()
{
CString strPath,strPathWild;
strPath = pPath;
strPath += "\\结果文件夹";
CFileFind f;
BOOL bWorking=f.FindFile(strPath);
if(bWorking)
DeleteFolder(strPath);
CreateDirectory(strPath,NULL);
for(int i=1;i<=nKindNumber;i++)
{
CString strSubPath;
CString str;
str.Format("%d",i);
strSubPath = str + "类网页";//设置网页路径
strSubPath = "\\" + strSubPath;
strSubPath = strPath + strSubPath;
CreateDirectory(strSubPath,NULL);
}
/*strSubPath.Empty();
strSubPath = strPath + "\\2类网页";
CreateDirectory(strSubPath,NULL);
strSubPath.Empty();
strSubPath = strPath + "\\3类网页";
CreateDirectory(strSubPath,NULL);
strSubPath.Empty();
strSubPath = strPath + "\\4类网页";
CreateDirectory(strSubPath,NULL);*/
return TRUE;
}
/********************************************************************
// 创建日期 : 2008-5-19 20:53:19
// 作 者 : 祝美莲<zml123818@163.com>
// 函数名称 : CTest::ReadFile
// 函数功能 : 读取向量机模型文件
// 返回类型 : BOOL
// 函数参数 : int k
********************************************************************/
BOOL CTest::ReadFile(int k)
{
CFile mFile; //文件操作
//读取k类的模型
for(int i=1; i<k; i++)
{
CString strName;//模型文件的路径
//赋值
strName.Format("%d.txt",i);
strName = "\\向量机模型\\" + strName;
strName = pPath + strName;
if(mFile.Open(strName,CFile::modeRead) == 0) //打开模型文件
{
AfxMessageBox("打开模型文件失败!", MB_ICONSTOP);
return FALSE;
}
//准备读取数据
CArchive ar(&mFile,CArchive::load);
//读出数据
for(int j = 0; j < nVectorItemNumber; j++)
{
ar>>Plane[i].dNormalVector[j];//读出w值
}
ar>>Plane[i].dThreValue;//b值
ar.Close();
mFile.Close();//关闭文件
}
return TRUE;
}
/********************************************************************
// 创建日期 : 2008-5-19 20:54:17
// 作 者 : 祝美莲<zml123818@163.com>
// 函数名称 : CTest::Func
// 函数功能 : 计算目标函数值,看是否属于该类
// 返回类型 : BOOL
// 函数参数 : double VX[VECTORITEMNUMBER]
// 函数参数 : int k
********************************************************************/
BOOL CTest::Func( double VX[VECTORITEMNUMBER], int k )
{
if((DotMetrix(Plane[k].dNormalVector,VX) - Plane[k].dThreValue) > 0 )
{
return TRUE;
}
else
{
return FALSE;
}
}
/********************************************************************
// 创建日期 : 2008-5-19 20:55:08
// 作 者 : 祝美莲<zml123818@163.com>
// 函数名称 : CTest::Testing
// 函数功能 : 测试向量机
// 返回类型 : BOOL
********************************************************************/
BOOL CTest::Testing()
{
//定义相关变量
int i = 0;//循环变量
int pos = 0;
CFileFind f;//查找文件
CStdioFile mFile;//文件操作
VectorItem vi; //特征向量的一个分量
CString strWildcard;//通配文件路径
CString strNameTest;//测试集的文件夹路径
//赋初值
strNameTest = "\\测试集\\特征向量表示\\";
strNameTest = pPath + strNameTest;
strWildcard = strNameTest+"*.*";
//判断是否查找完的标记
BOOL bWorking = f.FindFile(strWildcard);
if(!bWorking)
{
AfxMessageBox("请将测试集放在正确的位置!",MB_ICONSTOP);
return FALSE;
}
//读取向量机模型
if( !ReadFile( nKindNumber ) )
return FALSE;
//创建文件夹,以存放分类之后的文件
CreateResultDirectory();
while( bWorking )
{
bWorking = f.FindNextFile();//指向下一个文件
if(f.IsDots()) //是否为上一级的文件夹
{
continue;
}
else if(f.IsDirectory()) //时候为子文件夹
{
AfxMessageBox("请将所有待测网页放在一个文件夹中!", MB_ICONINFORMATION);
}
else//找到文件
{
CPageInfo Page; //测试集的一个网页
CString str; //字符串,存放文件的完整路径
str = strNameTest + f.GetFileName();//赋初值
CString strNameTxt;//实际测试集网页的文件名
CString strNameTxtPath;//实际测试集网页的文件路径
//赋值
strNameTxt = f.GetFileName();
strNameTxt = strNameTxt.Left( strNameTxt.GetLength() -4 );//因为特征表示的格式是.test
strNameTxt = strNameTxt + "htm";
strNameTxtPath = "\\测试集\\实际网页\\" + strNameTxt;
strNameTxtPath = pPath + strNameTxtPath;
//判断测试集是否存放在默认的位置上
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -