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

📄 arffstream.cpp

📁 一个简单灵活的数据挖掘实验平台
💻 CPP
字号:
/************************************************************************/
/*		AUTHOR:   Leiming Hong                                          */
/*		INST.:    South China Univ. of Tech.							*/
/*      Date:     2005-10-25                                            */
/*      FUNC:     Attribute used for defining data column type          */   
/************************************************************************/
#pragma warning(disable: 4786)

#include "ArffStream.h"
#include "..\\..\\Core\\Exception.h"
#include "..\\..\\Utils\\xs.h"
//: for test
#include <iostream>
#include <ctime>
using namespace std;

ArffStream::ArffStream(string file_name)
	: InputStream(0),
	  _file_name(file_name)
{
}
ArffStream::~ArffStream()
{
}
void ArffStream::open()
{
	_stream.open(_file_name.c_str());
	if(!_stream.is_open())
		throw new Exception("ArffStream::open "+_file_name + " can not open!");
	this->recognize();
}
void ArffStream::close()
{
	_stream.close();
}
void ArffStream::reset()
{
	_stream.close();
	this->open();
}
bool ArffStream::next()
{
	while(true)
	{
		if(!getline(_stream,_line)) return false;
		_line = xs::trim(_line);
		if(_line.length() == 0) continue;
		if(_line[0] == '%') continue;
		
		vector<string> blocks = xs::split(_line,", \t");
		int size = blocks.size();
		if(size != _metaData->size())
			throw new Exception("data instance format error!");
		for(int i=0;i<size;i++)
		{
			
			Attribute attr = (*_metaData)[i];
			if(attr.is_nominal())
				(*_data)[i] = attr.nominal_value(blocks[i]);
			else //: numeric
			{	
				(*_data)[i] = atof(blocks[i].c_str());
			}
		}
		break;
	}
	return true;
}
void ArffStream::recognize()
{
	if(!_recognized && _metaData == 0)
		_metaData = new MetaData();

	while(true)
	{
		if(!getline(_stream,_line)) break;
		_line = xs::trim(_line);
		if(_line[0] == '%') continue; //: omit the remarkable line
	
		if(xs::start_with(_line,"@relation",false)) //: relation name encountered
		{
			string name = xs::trim(_line.substr(strlen("@relation")),"' \t");
			_metaData->set_name(name);
		}
		if(xs::start_with(_line,"@attribute",false)) 
		{
			//: handling the attribute section
			vector<string> blocks = xs::split(_line,"{,} \t");
			int size = blocks.size();
			if(size < 3) 
				throw new Exception("@attribute section error encountered!");
			Attribute* attr = 0;
			//: 1) nominal attribute
			if(_line[_line.length()-1] == '}')
			{
				attr = new Attribute(blocks[1],Attribute::_At::nominal);
				for(int i=2;i<size;i++)
					attr->add_nominal(blocks[i]);
			}
			//: 2) numeric attribute
			else
			{
				attr = new Attribute(blocks[1],Attribute::_At::numeric);
			}
			_metaData->append(attr);
			if(blocks[1] == "class")
				_metaData->add_target(attr);
		}
		if(xs::start_with(_line,"@data",false)) break;
	}
	_recognized = true;
	//: initialize the data 
	_data = new Data(_metaData->size());
}
/************************************************************************
void main()
{
	clock_t start = clock();
	try
	{
		
		ArffStream arff("arff//soybeanTrain.arff");
		arff.open();
		while(arff.next())
		{
		}
	}
	catch(Exception* e)
	{
		cout<<e->message()<<endl;
		delete e;
	}
	clock_t end = clock();
	cout<<"Time eclipsed[s]:"<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
}
/************************************************************************/

⌨️ 快捷键说明

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