不能用Weka预测类(使用java代码)

时间:2014-04-24 17:18:10

标签: java text classification weka arff

我正在使用此代码审核我的文件test1.txttest1.model,我的班级为{Business,Friends,Spam} 当函数classify()编译时,它不会预测任何类。我是Weka的新手,所以我发现类的类型是错误的,所以我尝试了其他类型,但它导致相同的输出和classify()剂量没有以正确的方式编译。任何人都可以告诉我这是什么问题?

输出显示如下

   run:
===== Loaded text data: D:\test\test1.txt =====
 hello  this is a test 
===== Loaded model: D:\test\test1.model =====
===== Instance created with reference dataset =====
@relation 'Test relation'

@attribute class {Business,Friends,Spam}
@attribute text string

@data
?,' hello  this is a test '
Problem found when classifying the text
BUILD SUCCESSFUL (total time: 2 seconds)

使用此代码

package sentimentclassifier;
import weka.core.*;
import weka.core.FastVector;
import weka.classifiers.meta.FilteredClassifier;
import java.util.List;
import java.util.ArrayList;
import java.io.*;

public class SentimentClassifier {
/**
    String text;
    /**
     * Object that stores the instance.
     */
    Instances instances;
    /**
     * Object that stores the classifier.
     */
    FilteredClassifier classifier;
    private String text;

    /**
     * This method loads the text to be classified.
     * @param fileName The name of the file that stores the text.
     */
    public void load(String fileName) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            String line;
            text = "";
            while ((line = reader.readLine()) != null) {
                text = text + " " + line;
            }
            System.out.println("===== Loaded text data: " + fileName + " =====");
            reader.close();
            System.out.println(text);
        }
        catch (IOException e) {
            System.out.println("Problem found when reading: " + fileName);
        }
    }

    /**
     * This method loads the model to be used as classifier.
     * @param fileName The name of the file that stores the text.
     */
    public void loadModel(String fileName) {
        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
            Object tmp = in.readObject();
            classifier = (FilteredClassifier) tmp;
            in.close();
            System.out.println("===== Loaded model: " + fileName + " =====");
       } 
        catch (Exception e) {
            // Given the cast, a ClassNotFoundException must be caught along with the IOException
            System.out.println("Problem found when reading: " + fileName);
        }
    }

    /**
     * This method creates the instance to be classified, from the text that has been read.
     */
    public void makeInstance() {
        FastVector fvNominalVal = new FastVector(3);
    fvNominalVal.addElement("Business");
    fvNominalVal.addElement("Friends");
            fvNominalVal.addElement("Spam");
    Attribute attribute1 = new Attribute("class", fvNominalVal);
    Attribute attribute2 = new Attribute("text",(FastVector) null);
            //==========================================
    // Create list of instances with one element
    FastVector fvWekaAttributes = new FastVector(2);
    fvWekaAttributes.addElement(attribute1);
    fvWekaAttributes.addElement(attribute2);
    instances = new Instances("Test relation", fvWekaAttributes, 1);           
    // Set class index
    instances.setClassIndex(0);
    // Create and add the instance
    DenseInstance instance = new DenseInstance(2);
    instance.setValue(attribute2, text);
    // Another way to do it:
    // instance.setValue((Attribute)fvWekaAttributes.elementAt(1), text);
    instances.add(instance);
    System.out.println("===== Instance created with reference dataset =====");
    System.out.println(instances);
    }

    /**
     * This method performs the classification of the instance.
     * Output is done at the command-line.
     */
    public void classify() {
        try {
            double pred = classifier.classifyInstance(instances.instance(2));
            System.out.println("===== Classified instance =====");
            System.out.println("Class predicted: " + instances.classAttribute().value((int) pred));
        }
        catch (Exception e) {
            System.out.println("Problem found when classifying the text");
        }       
    }
    public static void main(String[] args) {
 SentimentClassifier classifier;

            classifier = new SentimentClassifier();
            classifier.load("D:\\test\\test1.txt");
        classifier.loadModel("D:\\test\\test1.model");
            classifier.makeInstance();
            classifier.classify();
        }
    }

0 个答案:

没有答案