How to use StringToWordVector (weka) in java?

2019-06-26 07:07发布

This is my arff file

@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

What i want to do is to classify it with weka clasiffier in my java program, but i don't know how to use StringToWordVector and then classify it.

this my code:

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

StringToWordVector filter = new StringToWordVector(); 

What next?, i don't know what to do..

1条回答
迷人小祖宗
2楼-- · 2019-06-26 07:51
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);
   }
}

This code uses J48 decision tree to build a classifier trained with spamham.arff. Hope that helps.

查看更多
登录 后发表回答