How to use StringToWordVector (weka) in java?

核能气质少年 提交于 2020-01-03 20:04:58

问题


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:


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.



来源:https://stackoverflow.com/questions/41877413/how-to-use-stringtowordvector-weka-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!