2017-07-05 2 views
-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(つぶやき、「こんにちは、私の世界」);

+1

'インスタンステストケース。 testcase = null; 'ここではnullを代入します。代わりに、新しいオブジェクトを作成する必要があります。' 'インスタンスtestcase =新しいインスタンス(1);'このようなものを作成する必要があります。 –

答えて

-1

このコードは正常に動作しています。分類器1つのインスタンスを渡す

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

及びインスタンスを作成することが可能である 、

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




    } 
関連する問題