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

📄 selectdocs.cpp

📁 svm算法的中文文本分类 找的很辛苦啊
💻 CPP
字号:
// SelectDocs.cpp: implementation of the CSelectDocs class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "svmcls.h"
#include "message.h"
#include "SelectDocs.h"

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

CSelectDocs theSelectDocs;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSelectDocs::CSelectDocs():m_nPercent(67)
{

}

CSelectDocs::~CSelectDocs()
{

}

long CSelectDocs::SelectDocs()
{
	if(m_strSource.IsEmpty()||m_strTarget.IsEmpty())
	{
		CMessage::PrintError("源文档或目标文档所在目录不能为空!");
		return 0;
	}
	if(!SetCurrentDirectory(m_strSource))
	{
		CMessage::PrintError("源文档目录不存在!");
		return 0;
	}
	if(m_strTarget.Find(m_strSource)>=0)
	{
		CMessage::PrintError("\"目标文档目录\"不能是\"源文档目录\"的子目录!");
		return 0;
	}
	if(m_nPercent<=0||m_nPercent>=100)
	{
		CMessage::PrintError("训练文档所占比例必须在0-100之间!");
		return 0;
	}
	if(!SetCurrentDirectory(m_strTarget))
	{
		if(!CreateDirectory(m_strTarget,NULL))
		{
			CMessage::PrintError("无法创建目标文档目录!");
			return 0;
		}
	}

	CreateDirectory(m_strTarget+"\\train",NULL);
	CreateDirectory(m_strTarget+"\\answer",NULL);
	CreateDirectory(m_strTarget+"\\result",NULL);

	long lFileNum=0;
	CString str;
	HANDLE hFinder;
	LPWIN32_FIND_DATA lpFindFileData;
	lpFindFileData  = new WIN32_FIND_DATA;
	hFinder = FindFirstFile(m_strSource+"\\*.*",lpFindFileData );

	while(FindNextFile(hFinder,lpFindFileData))
	{
		if( !strcmp(lpFindFileData->cFileName,".") || !strcmp(lpFindFileData->cFileName,"..") )
			continue;

		if(lpFindFileData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			lFileNum+=CopyDoc(lpFindFileData->cFileName);
			str="正在扫描目录";
			str+=lpFindFileData->cFileName;
			CMessage::PrintStatusInfo(str);
		}
	}
	delete lpFindFileData;
	return lFileNum;
}


DWORD CSelectDocs::GetFileNum(TCHAR *pPathName)
{
	DWORD dwFileNum=0;	
	CString str=pPathName;
	str=str+"\\*.*";

	HANDLE hFinder;
	LPWIN32_FIND_DATA lpFindFileData;
	lpFindFileData  = new WIN32_FIND_DATA;
	hFinder = FindFirstFile(str,lpFindFileData );

	while(FindNextFile(hFinder,lpFindFileData)&&(dwFileNum>=0))
	{
		if( !strcmp(lpFindFileData->cFileName,".") || !strcmp(lpFindFileData->cFileName,"..") )
			continue;

		dwFileNum++;
	}
	delete lpFindFileData;
	return dwFileNum;
}

DWORD CSelectDocs::CopyDoc(TCHAR *strPathName)
{
	CStringArray astrFiles;
	CString str;
	HANDLE hFinder;
	LPWIN32_FIND_DATA lpFindFileData;
	lpFindFileData  = new WIN32_FIND_DATA;
	str.Format("%s\\%s\\*.*",m_strSource,strPathName);
	hFinder = FindFirstFile(str,lpFindFileData );
	while(FindNextFile(hFinder,lpFindFileData))
	{
		if( !strcmp(lpFindFileData->cFileName,".") || !strcmp(lpFindFileData->cFileName,"..") )
			continue;
		if(!(lpFindFileData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
			astrFiles.Add(lpFindFileData->cFileName);
	}
	delete lpFindFileData;

	CString strSource,strTrain,strAnswer;
	strSource=m_strSource+"\\"+strPathName;
	strTrain=m_strTarget+"\\train\\"+strPathName;
	strAnswer=m_strTarget+"\\answer\\"+strPathName;

	CreateDirectory(strTrain,NULL);
	CreateDirectory(strAnswer,NULL);

	//随机选择训练文档
	CString fs,ft;
	long i,lTotalDocs,lTrainDocNum,lSelected,lReturned;
	lTotalDocs=astrFiles.GetSize();
	lReturned=lTotalDocs;
	lTrainDocNum=lTotalDocs*(m_nPercent/100.0);

	if(lReturned==0)
	{
		str.Format("目录%s下的文档总数为0!",strPathName);
		CMessage::PrintError(str);
		return 0;
	}

	for(i=0;i<lTrainDocNum;i++)
	{
		srand((unsigned)time(NULL));
		lSelected=rand()%lTotalDocs;
		fs=strSource+"\\"+astrFiles.GetAt(lSelected);
		ft=strTrain+"\\"+astrFiles.GetAt(lSelected);
		//AfxMessageBox(fs+" "+ft);
		CopyFile(fs,ft,FALSE);
		astrFiles.RemoveAt(lSelected);
		lTotalDocs--;
	}
	//降剩余部分作为测试文档
	for(i=0;i<astrFiles.GetSize();i++)
	{
		fs=strSource+"\\"+astrFiles.GetAt(i);
		ft=strAnswer+"\\"+astrFiles.GetAt(i);
		CopyFile(fs,ft,FALSE);
	}
	return lReturned;
}

⌨️ 快捷键说明

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