-1
私はwekaを初めて利用しています。現在私はwekaとjavaを使用してテキスト分類に取り組んでいます。私のトレーニングデータセットには、1つのString属性と1つのクラス属性があります。wekaを使用してJavaのドキュメント分類で動的テストインスタンスを渡す方法
@RELATION test
@ATTRIBUTE tweet string
@ATTRIBUTE class {positive,negative}
テストインスタントを動的に作成し、Naive-Bayesクラシファイアを使用して分類する必要があります。
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);
}
私は前の質問How to test a single test case in Weka, entered by a User?に従っています。
しかし、私は値がインスタンスをテストするために設定しようとしたとき、まだ私は取得していますjava.lang.NullPointerExceptionが、
testcase.setValue(つぶやき、「こんにちは、私の世界」);
'インスタンステストケース。 testcase = null; 'ここではnullを代入します。代わりに、新しいオブジェクトを作成する必要があります。' 'インスタンスtestcase =新しいインスタンス(1);'このようなものを作成する必要があります。 –