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

📄 filemodel.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
字号:
/*
 *  YALE - Yet Another Learning Environment
 *  Copyright (C) 2001-2004
 *      Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, 
 *          Katharina Morik, Oliver Ritthoff
 *      Artificial Intelligence Unit
 *      Computer Science Department
 *      University of Dortmund
 *      44221 Dortmund,  Germany
 *  email: yale-team@lists.sourceforge.net
 *  web:   http://yale.cs.uni-dortmund.de/
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as 
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version. 
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA.
 */
package edu.udo.cs.yale.operator.learner;

import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.tools.TempFileService;
import edu.udo.cs.yale.tools.Tools;

import java.io.*;

/** FileModels are models that are generated by external algorithms and are not interpreted by Yale.
 *  Yale keeps the contents of the file in memory and provides a Reader or InputStream
 *  for the data.
 *
 *  @author Ingo
 *  @version $Id: FileModel.java,v 2.10 2004/08/27 11:57:38 ingomierswa Exp $
 */
public abstract class FileModel extends IOModel {

    /** The data contained in the file as literally generated by the learner. */
    private byte[] data;

    /** Creates an empty file model. */
    public FileModel(Attribute label) {
	super(label);
	data = new byte[0];
    }

    /** This constructor is for the test case only! */
    public FileModel(Attribute label, byte[] data) {
	super(label);
	this.data = data;
    }

    /** Constructs a new model reading the data from the given file. */
    public FileModel(Attribute label, File modelFile) throws IOException {
	super(label);
	if (!modelFile.exists()) {
	    throw new FileNotFoundException("Model file does not exist: " + modelFile);
	}

	FileInputStream in = new FileInputStream(modelFile);
	data = new byte[(int)modelFile.length()];
	in.read(data);
	in.close();
    }

    public void writeData(ObjectOutputStream out) throws IOException {
	writeYaleData(out);
	out.flush();
	writeFileData(out);
	out.flush();
    }

    public final void readData(ObjectInputStream in) throws IOException {
	readYaleData(in);
	readFileData(in);
    }

    /** Reads additional data written by {@link #readYaleData(ObjectInputStream)}. */
    public abstract void writeYaleData(ObjectOutputStream out) throws IOException;

    /** Writes additional data, e.g. training parameters used for applying. */
    public abstract void readYaleData(ObjectInputStream in) throws IOException;

    /** Reads the data from the stream. */
    private void readFileData(ObjectInputStream in) throws IOException {
	data = new byte[in.readInt()];
	in.readFully(data);
	LogService.logMessage("Model has "+data.length+" bytes.", LogService.MINIMUM);
    }

    /** Writes the file data to the stream. */
    private void writeFileData(ObjectOutputStream out) throws IOException {
	out.writeInt(data.length);
	out.write(data);
	out.flush();
    }

    /** Creates a temp file and writes the model to it as it was read when created by the learner. */
    public File writeModelToTmp() throws IOException {
	File file = TempFileService.createTempFile("model_");
	OutputStream out = new FileOutputStream(file);
	out.write(data);
	out.close();
	return file;
    }


    public InputStream getInputStream() {
	return new ByteArrayInputStream(data);
    }

    public Reader getReader() {
	return new InputStreamReader(getInputStream());
    }

    public boolean equals(Object o) {
	if (!super.equals(o)) return false;
	FileModel other = (FileModel)o;
	if (this.data.length != other.data.length) return false;
	for (int i = 0; i < this.data.length; i++) {
	    if (this.data[i] != other.data[i]) return false;
	}
	return true;
    }

}

⌨️ 快捷键说明

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