view src/de/mpiwg/anteater/ml/impl/WekaMLComponent.java @ 6:50aeb96a8ee9

bugfix: person ml
author jdamerow
date Thu, 01 Nov 2012 13:23:25 -0700
parents 036535fcd179
children
line wrap: on
line source

package de.mpiwg.anteater.ml.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import weka.classifiers.Classifier;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import de.mpiwg.anteater.ml.IMLComponent;

public class WekaMLComponent implements IMLComponent {
	private Classifier classifier;
	
	public WekaMLComponent(String model) {
		classifier = null;
		try {
			InputStream stream = getClass().getResourceAsStream(model);
			classifier = (Classifier) weka.core.SerializationHelper.read(stream);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	@Override
	public List<Double> run(String testFile) {
		Instances testData = null;
		try {
			DataSource source = new DataSource(testFile);
			testData = source.getDataSet(); //DataSource.read(testFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		testData.setClassIndex(0);
		
		List<Double> predictions = new ArrayList<Double>();
		
		for (int i = 0; i<testData.numInstances(); i++) {
			double prediction;
			try {
				prediction = classifier.classifyInstance(testData.instance(i));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				predictions.add(-1.0);
				continue;
			}
			predictions.add(prediction);
		}
		
		return predictions;
	}

}