编程语言
首页 > 编程语言> > 如何在java中使用StringToWordVector(weka)?

如何在java中使用StringToWordVector(weka)?

作者:互联网

这是我的arff文件

@relation hamspam

@attribute text string
@attribute class {ham,spam}

@data
'good',ham
'very good',ham
'bad',spam
'very bad',spam
'very bad, very bad',spam

我想要做的是在我的java程序中使用weka clasiffier对其进行分类,但我不知道如何使用StringToWordVector然后对其进行分类.

我的代码:

Classifier j48tree = new J48();    
Instances train = new Instances(new BufferedReader(new FileReader("data.arff")));

StringToWordVector filter = new StringToWordVector(); 

下一步是什么?,我不知道该怎么做..

解决方法:

import weka.core.Instance;
//import required classes
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import weka.core.stemmers.LovinsStemmer;
import weka.classifiers.meta.FilteredClassifier;
import weka.classifiers.trees.J48;
import weka.filters.unsupervised.attribute.Remove;
import weka.filters.unsupervised.attribute.StringToWordVector;

public class ClassifierWithFilter{

    public static void main(String args[]) throws Exception{
    //load dataset
    DataSource source = new DataSource("/Users/amaryadav/Desktop/spamham.arff");
    Instances dataset = source.getDataSet();
    //set class index to the last attribute
    dataset.setClassIndex(dataset.numAttributes()-1);

    //the base classifier
    J48 tree = new J48();

    //the filter
    StringToWordVector filter = new StringToWordVector();
    filter.setInputFormat(dataset);
    filter.setIDFTransform(true);
    filter.setUseStoplist(true);
    LovinsStemmer stemmer = new LovinsStemmer();
    filter.setStemmer(stemmer);
    filter.setLowerCaseTokens(true);

    //Create the FilteredClassifier object
    FilteredClassifier fc = new FilteredClassifier();
    //specify filter
    fc.setFilter(filter);
    //specify base classifier
    fc.setClassifier(tree);
    //Build the meta-classifier
    fc.buildClassifier(dataset);

    System.out.println(tree.graph());
    System.out.println(tree);
   }
}

此代码使用J48决策树来构建使用spamham.arff训练的分类器.希望有所帮助.

标签:java,classification,weka,arff
来源: https://codeday.me/bug/20190623/1266229.html