I am using the Java Weka API to build a classification model. I can use the builtin stopwords filter. However, I need to use a custom filter for my problem. I do not know how to use a custom stopwords filter in the Java Weka API.


You can try the following code.

import weka.core.converters.ConverterUtils.DataSource;
import weka.filters.unsupervised.attribute.StringToWordVector;
import weka.core.Instances;

Instances data = DataSource.read(".../document.txt"); //Your document .
StringToWordVector filter = new StringToWordVector();
filter.setStopwords(new File(".../stopwords.txt")); //stop words file.
Instances data = Filter.useFilter(data,filter);

You can also read the following document for better understanding of the Weka API for Java. http://weka.sourceforge.net/doc.stable/

  • $\begingroup$ I appreciate your support. Thanks a lot. Your code and the link of the document really help me lot. $\endgroup$ – Radia Karim Nov 6 '18 at 2:52

First of all you have to prepare a text file for your custom stopwords. Then you can use the following code:

import weka.filters.unsupervised.attribute.StringToWordVector;

StringToWordVector filter = new StringToWordVector(10000);
filter.setStopwords(new File(".../stopwords.txt"));

Hope it will help you.

  • $\begingroup$ I appreciate your support. $\endgroup$ – Radia Karim Nov 6 '18 at 2:22
  • $\begingroup$ I am very new in Java weka-api programming. I do not understand clearly. Anyone please provide me little more elaborate answer. Thanks in advance for your support. $\endgroup$ – Radia Karim Nov 6 '18 at 2:34
  • $\begingroup$ Please let us know which part do not you understand? $\endgroup$ – Rejaul Karim Dec 4 '18 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.