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");