Commit 8d923205 authored by Markus Krug's avatar Markus Krug
Browse files

First Upload

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>de.uniwue.mk.kall.mlf.classifier.application</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Classifier
Bundle-SymbolicName: de.uniwue.mk.kall.mlf.classifier.application
Bundle-Version: 1.0.0.qualifier
Bundle-ClassPath: .
Require-Bundle: de.uniwue.mk.kall.mlf.features;bundle-version="1.0.0",
de.uniwue.mk.kall.mlf.libraries;bundle-version="1.0.0"
Export-Package: de.uniwue.mk.kall.mlf.classifier.application,
de.uniwue.mk.kall.mlf.classifier.trainer
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
package de.uniwue.mk.kall.mlf.classifier.application;
public abstract class AKallimachosClassifierApplication {
public abstract void classify();
}
package de.uniwue.mk.kall.mlf.classifier.application;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import cc.mallet.classify.C45;
import cc.mallet.classify.Classification;
import de.uniwue.mk.kall.mlf.featureRepresentation.util.MalletIO;
public class C45ClassifierApplication {
public static String[] apply(File testFile, File modelFile) throws Exception {
MalletIO ioHandler = new MalletIO();
// read the Model from modelFile
C45 model = (C45) ioHandler
.deserialize(modelFile);
List<String> resultList = new ArrayList<String>();
List<String> lines = Files.readAllLines(testFile.toPath(), Charset.defaultCharset());
int index = 0;
for (String line : lines) {
if (line.isEmpty())
continue;
String[] split = line.split(" ");
StringBuilder featureBuilder = new StringBuilder();
for (int i = 0; i < split.length - 1; i++) {
featureBuilder.append(split[i]).append(" ");
}
// classify
String instance = featureBuilder.toString().replaceAll("=", "");
Classification classify = model.classify(instance);
resultList.add(classify.getLabeling().getBestLabel().toString());
}
return resultList.toArray(new String[0]);
}
}
package de.uniwue.mk.kall.mlf.classifier.application;
import java.io.File;
public class CRFApplication {
public static String[] apply(File testFile, File modelFile) throws Exception {
SimpleTagger.main(new String[] { "--train", "false", "--model-file",
modelFile.getAbsolutePath(), testFile.getAbsolutePath() });
return SimpleTagger.output.toString().split(" ");
}
}
package de.uniwue.mk.kall.mlf.classifier.application;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import cc.mallet.classify.Classification;
import de.uniwue.mk.kall.mlf.featureRepresentation.util.MalletIO;
public class DecisionTreeClassifierApplication {
public static String[] apply(File testFile, File modelFile) throws Exception {
MalletIO ioHandler = new MalletIO();
// read the Model from modelFile
cc.mallet.classify.DecisionTree model = (cc.mallet.classify.DecisionTree) ioHandler
.deserialize(modelFile);
List<String> resultList = new ArrayList<String>();
List<String> lines = Files.readAllLines(testFile.toPath(), Charset.defaultCharset());
int index = 0;
for (String line : lines) {
if (line.isEmpty())
continue;
String[] split = line.split(" ");
StringBuilder featureBuilder = new StringBuilder();
for (int i = 0; i < split.length - 1; i++) {
featureBuilder.append(split[i]).append(" ");
}
// classify
String instance = featureBuilder.toString().replaceAll("=", "");
Classification classify = model.classify(instance);
resultList.add(classify.getLabeling().getBestLabel().toString());
}
return resultList.toArray(new String[0]);
}
}
package de.uniwue.mk.kall.mlf.classifier.application;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import cc.mallet.classify.Classification;
import cc.mallet.classify.MaxEnt;
import de.uniwue.mk.kall.mlf.featureRepresentation.util.MalletIO;
public class MaxEntClassifierApplication {
public static String[] apply(File testFile, File modelFile) throws Exception {
MalletIO ioHandler = new MalletIO();
// read the Model from modelFile
MaxEnt model = (MaxEnt) ioHandler.deserialize(modelFile);
List<String> resultList = new ArrayList<String>();
List<String> lines = Files.readAllLines(testFile.toPath(), Charset.defaultCharset());
for (String line : lines) {
if (line.isEmpty())
continue;
String[] split = line.split(" ");
StringBuilder featureBuilder = new StringBuilder();
for (int i = 0; i < split.length - 1; i++) {
featureBuilder.append(split[i]).append(" ");
}
// classify
String instance = featureBuilder.toString().replaceAll("=", "");
Classification classify = model.classify(instance);
resultList.add(classify.getLabeling().getBestLabel().toString());
}
return resultList.toArray(new String[0]);
}
public static String[] apply(File testFile, MaxEnt model) throws Exception {
List<String> resultList = new ArrayList<String>();
List<String> lines = Files.readAllLines(testFile.toPath(), Charset.defaultCharset());
for (String line : lines) {
if (line.isEmpty())
continue;
String[] split = line.split(" ");
StringBuilder featureBuilder = new StringBuilder();
for (int i = 0; i < split.length - 1; i++) {
featureBuilder.append(split[i]).append(" ");
}
// classify
String instance = featureBuilder.toString().replaceAll("=", "");
Classification classify = model.classify(instance);
resultList.add(classify.getLabeling().getBestLabel().toString());
}
return resultList.toArray(new String[0]);
}
public static Double[] applyForConfidence(File testFile, File modelFile) throws Exception {
MalletIO ioHandler = new MalletIO();
// read the Model from modelFile
cc.mallet.classify.MaxEnt model = (cc.mallet.classify.MaxEnt) ioHandler.deserialize(modelFile);
List<Double> resultList = new ArrayList<Double>();
List<String> lines = Files.readAllLines(testFile.toPath(), Charset.defaultCharset());
for (String line : lines) {
if (line.isEmpty())
continue;
String[] split = line.split(" ");
StringBuilder featureBuilder = new StringBuilder();
for (int i = 0; i < split.length - 1; i++) {
featureBuilder.append(split[i]).append(" ");
}
// classify
String instance = featureBuilder.toString().replaceAll("=", "");
Classification classify = model.classify(instance);
resultList.add(classify.getLabeling().getBestValue());
}
return resultList.toArray(new Double[0]);
}
public static Double[] applyForConfidence(File testFile, MaxEnt model) throws Exception {
List<Double> resultList = new ArrayList<Double>();
List<String> lines = Files.readAllLines(testFile.toPath(), Charset.defaultCharset());
for (String line : lines) {
if (line.isEmpty())
continue;
String[] split = line.split(" ");
StringBuilder featureBuilder = new StringBuilder();
for (int i = 0; i < split.length - 1; i++) {
featureBuilder.append(split[i]).append(" ");
}
// classify
String instance = featureBuilder.toString().replaceAll("=", "");
Classification classify = model.classify(instance);
resultList.add(classify.getLabeling().getBestValue());
}
return resultList.toArray(new Double[0]);
}
}
package de.uniwue.mk.kall.mlf.classifier.application;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import cc.mallet.classify.Classification;
import cc.mallet.classify.NaiveBayes;
import de.uniwue.mk.kall.mlf.featureRepresentation.util.MalletIO;
public class NaiveBayesClassifierApplication {
public static String[] apply(File testFile, File modelFile) throws Exception {
MalletIO ioHandler = new MalletIO();
// read the Model from modelFile
NaiveBayes model = (NaiveBayes) ioHandler.deserialize(modelFile);
List<String> resultList = new ArrayList<String>();
List<String> lines = Files.readAllLines(testFile.toPath(), Charset.defaultCharset());
for (String line : lines) {
if (line.isEmpty())
continue;
String[] split = line.split(" ");
StringBuilder featureBuilder = new StringBuilder();
for (int i = 0; i < split.length - 1; i++) {
featureBuilder.append(split[i]).append(" ");
}
// classify
String instance = featureBuilder.toString().replaceAll("=", "");
Classification classify = model.classify(instance);
resultList.add(classify.getLabeling().getBestLabel().toString());
}
return resultList.toArray(new String[0]);
}
}
package de.uniwue.mk.kall.mlf.classifier.application;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.ObjectInputStream;
import weka.classifiers.Classifier;
import weka.core.Instance;
import weka.core.Instances;
public class NaiveBayesWekaClassifierApplication {
public static String[] apply(File testFile, File modelFile) throws Exception {
// deserialize model
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(modelFile));
Classifier cls = (Classifier) ois.readObject();
ois.close();
// get the instances
Instances inst = new Instances(new BufferedReader(new FileReader(testFile)));
inst.setClassIndex(inst.numAttributes() - 1);
String[] results = new String[inst.numInstances()];
for (int i = 0; i < inst.numInstances(); i++) {
Instance instance = inst.instance(i);
results[i] = String.valueOf(cls.classifyInstance(instance));
}
// returns doubles not labels !! i hate this but i canot change it ...
return results;
}
}
package de.uniwue.mk.kall.mlf.classifier.application;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.ObjectInputStream;
import weka.classifiers.Classifier;
import weka.core.Instance;
import weka.core.Instances;
public class RandomforestClassifierApplication {
public static String[] apply(File testFile, File modelFile) throws Exception {
// deserialize model
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(modelFile));
Classifier cls = (Classifier) ois.readObject();
ois.close();
System.out.println(testFile.getName());
// get the instances
Instances inst = new Instances(new BufferedReader(new FileReader(testFile)));
inst.setClassIndex(inst.numAttributes() - 1);
String[] results = new String[inst.numInstances()];
for (int i = 0; i < inst.numInstances(); i++) {
Instance instance = inst.instance(i);
results[i] = String.valueOf(cls.classifyInstance(instance));
}
// returns doubles not labels !! i hate this but i canot change it ...
return results;
}
public static String[] apply(Instances test, File modelFile) throws Exception {
// deserialize model
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(modelFile));
Classifier cls = (Classifier) ois.readObject();
ois.close();
// get the instances
Instances inst = test;
// add instances from list to instances
inst.setClassIndex(inst.numAttributes() - 1);
String[] results = new String[inst.numInstances()];
for (int i = 0; i < inst.numInstances(); i++) {
Instance instance = inst.instance(i);
results[i] = String.valueOf(cls.classifyInstance(instance));
}
// returns doubles not labels !! i hate this but i canot change it ...
return results;
}
public static String[] apply(Instances test, Classifier classifier) throws Exception {
// get the instances
Instances inst = test;
// add instances from list to instances
inst.setClassIndex(inst.numAttributes() - 1);
String[] results = new String[inst.numInstances()];
for (int i = 0; i < inst.numInstances(); i++) {
Instance instance = inst.instance(i);
results[i] = String.valueOf(classifier.classifyInstance(instance));
}
// returns doubles not labels !! i hate this but i canot change it ...
return results;
}
}
package de.uniwue.mk.kall.mlf.classifier.application;
import java.io.Serializable;
import libsvm.svm;
import libsvm.svm_model;
import libsvm.svm_node;
import cc.mallet.classify.Classification;
import cc.mallet.classify.Classifier;
import cc.mallet.pipe.Pipe;
import cc.mallet.types.FeatureVector;
import cc.mallet.types.Instance;
import cc.mallet.types.LabelVector;
public class SVM extends Classifier implements Serializable
{
private static final long serialVersionUID = -3273222430839071709L;
public static svm_node[] getSvmNodes(Instance instance)
{
FeatureVector fv = (FeatureVector) instance.getData();
svm_node[] ret = null;
if (fv.numLocations() > 0)
{
ret = new svm_node[fv.numLocations()];
for (int loc = 0; loc < fv.numLocations(); loc++)
{
ret[loc] = new svm_node();
ret[loc].index = fv.indexAtLocation(loc) + 1;
ret[loc].value = fv.valueAtLocation(loc);
}
}
return ret;
}
private