-1

I am new to weka. Currently I am working on text classification using weka and java. My training data-set has one String attribute and one class attribute.

@RELATION test

@ATTRIBUTE tweet string
@ATTRIBUTE class {positive,negative}

I want to create a test instant dynamically and get it classified using Naive-Bayes classifier.

   public static void main(String[] args) throws FileNotFoundException, IOException, Exception {

    StringToWordVector filter = new StringToWordVector();

    //training set
    BufferedReader reader = null;
    reader = new BufferedReader(new FileReader("D:/suicideTest.arff"));

    Instances train = new Instances(reader);
    train.setClassIndex(train.numAttributes() -1);
    filter.setInputFormat(train);
    train = Filter.useFilter(train, filter);

    reader.close();



    Attribute tweet = new Attribute("tweet");
    FastVector classVal = new FastVector(2);
    classVal.addElement("positive");
    classVal.addElement("negative");


    FastVector testAttributes = new FastVector(2);
    testAttributes.addElement(tweet);
    testAttributes.addElement(classVal);

    Instance testcase;
    testcase = null;

    testcase.setValue(tweet,"Hello my world");
    testcase.setValue((Attribute)testAttributes.elementAt(1),"?");

    Instances test = null;

    test.add(testcase);

    test = Filter.useFilter(test, filter);

    NaiveBayes nb = new NaiveBayes();
    nb.buildClassifier(train);

    Evaluation eval = new Evaluation(train);
    eval.crossValidateModel(nb, train, 10,new Random(1));


    double pred = nb.classifyInstance(test.instance(0));

    System.out.println("the result is   "+ pred);

}

I have followed this previous question How to test a single test case in Weka, entered by a User?.

But still I am getting and java.lang.NullPointerException when I tried to set values to test instance,

testcase.setValue(tweet,"Hello my world");

dennypanther
  • 53
  • 1
  • 10
  • 1
    `Instance testcase; testcase = null; ` Here you are assigning null, instead you need to create new object for that.`Instance testcase = new Instance(1);` you need to create something like this. – Sudha Velan Jul 10 '17 at 04:37

1 Answers1

-1

This code is working fine. It is possible to create Instances,

Instances testSet = new Instances("", allAtt, 1);
double pred = nb.classifyInstance(testSet.instance(0));

and pass one instance to the classifier,

public static void main(String[] args) throws Exception{
         StringToWordVector filter = new StringToWordVector();

        //training set
        BufferedReader reader;
        reader = new BufferedReader(new FileReader("D:/test.arff"));

        Instances train = new Instances(reader);
        train.setClassIndex(train.numAttributes() -1);
        filter.setInputFormat(train);
        train = Filter.useFilter(train, filter);


        reader.close();

        NaiveBayes nb = new NaiveBayes();
        nb.buildClassifier(train);


        ArrayList cls = new ArrayList(2);

        cls.add("negative"); 
        cls.add("positive");


        Attribute clsAtt = new Attribute("class", cls);

        //ArrayList<String> tweet = new ArrayList(1);
        //String tweet = "";
        //Attribute tweetAtt = new Attribute("tweet", tweet);

        ArrayList allAtt = new ArrayList(2); 
        //allAtt.add(tweetAtt);
        allAtt.add(new Attribute("tweet", (FastVector) null));
        allAtt.add(clsAtt);


        // Create an empty test set
         Instances testSet = new Instances("", allAtt, 1);
        // Set class index
        testSet.setClassIndex(testSet.numAttributes() - 1);

        String names=  "I want to suiceide";
        Instance inst = new DenseInstance(2); 
        inst.setValue((Attribute)allAtt.get(0), names.toString());

        testSet.add(inst);
        System.out.println(testSet.instance(0).toString());
        double pred = nb.classifyInstance(testSet.instance(0));

        filter.setInputFormat(testSet);
        testSet = Filter.useFilter(testSet, filter);

        String predictString = testSet.classAttribute().value((int) pred);




    }
dennypanther
  • 53
  • 1
  • 10