List of usage examples for weka.classifiers Sourcable toSource
String toSource(String className) throws Exception;
From source file:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * Wraps a static classifier in enough source to test using the weka class * libraries./*from w w w .j av a 2s. com*/ * * @param classifier a Sourcable Classifier * @param className the name to give to the source code class * @return the source for a static classifier that can be tested with weka * libraries. * @throws Exception if code-generation fails */ public static String wekaStaticWrapper(Sourcable classifier, String className) throws Exception { StringBuffer result = new StringBuffer(); String staticClassifier = classifier.toSource(className); result.append("// Generated with Weka " + Version.VERSION + "\n"); result.append("//\n"); result.append("// This code is public domain and comes with no warranty.\n"); result.append("//\n"); result.append("// Timestamp: " + new Date() + "\n"); result.append("\n"); result.append("package weka.classifiers;\n"); result.append("\n"); result.append("import weka.core.Attribute;\n"); result.append("import weka.core.Capabilities;\n"); result.append("import weka.core.Capabilities.Capability;\n"); result.append("import weka.core.Instance;\n"); result.append("import weka.core.Instances;\n"); result.append("import weka.core.RevisionUtils;\n"); result.append("import weka.classifiers.Classifier;\n"); result.append("\n"); result.append("public class WekaWrapper\n"); result.append(" extends Classifier {\n"); // globalInfo result.append("\n"); result.append(" /**\n"); result.append(" * Returns only the toString() method.\n"); result.append(" *\n"); result.append(" * @return a string describing the classifier\n"); result.append(" */\n"); result.append(" public String globalInfo() {\n"); result.append(" return toString();\n"); result.append(" }\n"); // getCapabilities result.append("\n"); result.append(" /**\n"); result.append(" * Returns the capabilities of this classifier.\n"); result.append(" *\n"); result.append(" * @return the capabilities\n"); result.append(" */\n"); result.append(" public Capabilities getCapabilities() {\n"); result.append(((Classifier) classifier).getCapabilities().toSource("result", 4)); result.append(" return result;\n"); result.append(" }\n"); // buildClassifier result.append("\n"); result.append(" /**\n"); result.append(" * only checks the data against its capabilities.\n"); result.append(" *\n"); result.append(" * @param i the training data\n"); result.append(" */\n"); result.append(" public void buildClassifier(Instances i) throws Exception {\n"); result.append(" // can classifier handle the data?\n"); result.append(" getCapabilities().testWithFail(i);\n"); result.append(" }\n"); // classifyInstance result.append("\n"); result.append(" /**\n"); result.append(" * Classifies the given instance.\n"); result.append(" *\n"); result.append(" * @param i the instance to classify\n"); result.append(" * @return the classification result\n"); result.append(" */\n"); result.append(" public double classifyInstance(Instance i) throws Exception {\n"); result.append(" Object[] s = new Object[i.numAttributes()];\n"); result.append(" \n"); result.append(" for (int j = 0; j < s.length; j++) {\n"); result.append(" if (!i.isMissing(j)) {\n"); result.append(" if (i.attribute(j).isNominal())\n"); result.append(" s[j] = new String(i.stringValue(j));\n"); result.append(" else if (i.attribute(j).isNumeric())\n"); result.append(" s[j] = new Double(i.value(j));\n"); result.append(" }\n"); result.append(" }\n"); result.append(" \n"); result.append(" // set class value to missing\n"); result.append(" s[i.classIndex()] = null;\n"); result.append(" \n"); result.append(" return " + className + ".classify(s);\n"); result.append(" }\n"); // getRevision result.append("\n"); result.append(" /**\n"); result.append(" * Returns the revision string.\n"); result.append(" * \n"); result.append(" * @return the revision\n"); result.append(" */\n"); result.append(" public String getRevision() {\n"); result.append(" return RevisionUtils.extract(\"1.0\");\n"); result.append(" }\n"); // toString result.append("\n"); result.append(" /**\n"); result.append(" * Returns only the classnames and what classifier it is based on.\n"); result.append(" *\n"); result.append(" * @return a short description\n"); result.append(" */\n"); result.append(" public String toString() {\n"); result.append(" return \"Auto-generated classifier wrapper, based on " + classifier.getClass().getName() + " (generated with Weka " + Version.VERSION + ").\\n" + "\" + this.getClass().getName() + \"/" + className + "\";\n"); result.append(" }\n"); // main result.append("\n"); result.append(" /**\n"); result.append(" * Runs the classfier from commandline.\n"); result.append(" *\n"); result.append(" * @param args the commandline arguments\n"); result.append(" */\n"); result.append(" public static void main(String args[]) {\n"); result.append(" runClassifier(new WekaWrapper(), args);\n"); result.append(" }\n"); result.append("}\n"); // actual classifier code result.append("\n"); result.append(staticClassifier); return result.toString(); }
From source file:GClass.EvaluationInternal.java
License:Open Source License
/** * Wraps a static classifier in enough source to test using the weka * class libraries./*from www . jav a 2s.c o m*/ * * @param classifier a Sourcable Classifier * @param className the name to give to the source code class * @return the source for a static classifier that can be tested with * weka libraries. */ protected static String wekaStaticWrapper(Sourcable classifier, String className) throws Exception { //String className = "StaticClassifier"; String staticClassifier = classifier.toSource(className); return "package weka.classifiers;\n" + "import weka.core.Attribute;\n" + "import weka.core.Instance;\n" + "import weka.core.Instances;\n" + "import weka.classifiers.Classifier;\n\n" + "public class WekaWrapper extends Classifier {\n\n" + " public void buildClassifier(Instances i) throws Exception {\n" + " }\n\n" + " public double classifyInstance(Instance i) throws Exception {\n\n" + " Object [] s = new Object [i.numAttributes()];\n" + " for (int j = 0; j < s.length; j++) {\n" + " if (!i.isMissing(j)) {\n" + " if (i.attribute(j).type() == Attribute.NOMINAL) {\n" + " s[j] = i.attribute(j).value((int) i.value(j));\n" + " } else if (i.attribute(j).type() == Attribute.NUMERIC) {\n" + " s[j] = new Double(i.value(j));\n" + " }\n" + " }\n" + " }\n" + " return " + className + ".classify(s);\n" + " }\n\n" + "}\n\n" + staticClassifier; // The static classifer class }
From source file:milk.classifiers.MIEvaluation.java
License:Open Source License
/** * Wraps a static classifier in enough source to test using the weka * class libraries./* w w w. j a v a2 s. c o m*/ * * @param classifier a Sourcable Classifier * @param className the name to give to the source code class * @return the source for a static classifier that can be tested with * weka libraries. */ protected static String wekaStaticWrapper(Sourcable classifier, String className) throws Exception { String staticClassifier = classifier.toSource(className); return "package weka.classifiers;\n" + "import weka.core.Attribute;\n" + "import weka.core.Instance;\n" + "import weka.core.Instances;\n" + "import MI.*;\n" + "import weka.classifiers.Classifier;\n\n" + "public class WekaWrapper extends Classifier {\n\n" + " public void buildClassifier(Instances i) throws Exception {\n" + " }\n\n" + " public double classifyExemplar(Instance i) throws Exception {\n\n" + " Object [] s = new Object [i.numAttributes()];\n" + " for (int j = 0; j < s.length; j++) {\n" + " if (!i.isMissing(j)) {\n" + " if (i.attribute(j).type() == Attribute.NOMINAL) {\n" + " s[j] = i.attribute(j).value((int) i.value(j));\n" + " } else if (i.attribute(j).type() == Attribute.NUMERIC) {\n" + " s[j] = new Double(i.value(j));\n" + " }\n" + " }\n" + " }\n" + " return " + className + ".classify(s);\n" + " }\n\n" + "}\n\n" + staticClassifier; // The static classifer class }