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

📄 premise.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.decisiontree;

import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.Example;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.tools.Ontology;

import java.io.Serializable;

/** A premise is a condition for a rule. It contains an attribute.
 *
 *  @author Ingo
 *  @version $Id: Premise.java,v 2.3 2004/08/27 11:57:38 ingomierswa Exp $
 */
public abstract class Premise implements Serializable {

    private Attribute attribute;

    /** Creates a new premise with the given attribute.
     */
    public Premise(Attribute attribute) {
	this.attribute = attribute;
    }

    public Attribute getAttribute() {
	return attribute;
    }

    /** Returns true if the premise is satisfied and false otherwise.
     */
    public abstract boolean isTrue(Example example); 


    // ---------- C4.5 ----------

    /** Creates a premise from a line of a c4.5 rule.
     */
    public static Premise parseC45Premise(String constraintLine, ExampleSet exampleSet) {
	String argumentLine = constraintLine.trim();
	String name = argumentLine.substring(0, argumentLine.indexOf(" "));
	argumentLine = argumentLine.substring(argumentLine.indexOf(" ") + 1);
	String operator = argumentLine.substring(0, argumentLine.indexOf(" "));
	argumentLine = argumentLine.substring(argumentLine.indexOf(" ") + 1);
	return parseC45Argument(name, operator, argumentLine, exampleSet);
    }

    public static Premise parseC45Argument(String name, String operator, String argument, ExampleSet exampleSet) {
	Attribute attribute = exampleSet.getAttribute(name);
	String argumentLine = argument.trim();
	if (argumentLine.indexOf("or") != -1) {
	    Premise[] constraintPair = new Premise[2];
	    constraintPair[0] = parseC45Argument(name, operator, argumentLine.substring(0, argumentLine.indexOf("or")), exampleSet);
	    constraintPair[1] = parseC45Argument(name, operator, argumentLine.substring(argumentLine.indexOf("or") + 3), exampleSet);
	    return new OrPremise(constraintPair);
	} else if (argumentLine.indexOf("..") != -1) {
	    if (!operator.equals("=")) {
		throw new RuntimeException("Building an interval is only possible with '='!");
	    } else {
		String lowerBoundStr = argumentLine.substring(0, argumentLine.indexOf(".."));
		String upperBoundStr = argumentLine.substring(argumentLine.indexOf("..")).trim();
		double lowerBound, upperBound;
		try {
		    lowerBound = Double.parseDouble(lowerBoundStr);
		    upperBound = Double.parseDouble(upperBoundStr);
		} catch (NumberFormatException e) {
		    throw new RuntimeException("Cannot parse C4.5 output: "+argumentLine);
		}
		Premise[] constraintPair = new Premise[2];
		constraintPair[0] = new SimplePremise(attribute, ">=", lowerBound);
		constraintPair[1] = new SimplePremise(attribute, "<=", upperBound);
		return new AndPremise(constraintPair);
	    }
	} else {
	    double value;
	    if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(attribute.getValueType(), Ontology.NOMINAL)) {
		value = attribute.mapString(argumentLine);
	    } else {
		try {
		    value = Double.parseDouble(argumentLine);
		} catch (NumberFormatException e) {
		    throw new RuntimeException("Cannot parse C4.5 output: "+argumentLine);
		}
	    }
	    return new SimplePremise(attribute, operator, value);
	}
    }

    public abstract String toString();

}

⌨️ 快捷键说明

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