我正在使用 lucene 删除英语停用词,但我的要求是删除英语停用词和自定义停用词。下面是我使用 lucene 删除英语停用词的代码。
我的示例代码:
public class Stopwords_remove {
public String removeStopWords(String string) throws IOException
{
StandardAnalyzer ana = new StandardAnalyzer(Version.LUCENE_30);
TokenStream tokenStream = new StandardTokenizer(Version.LUCENE_36,newStringReader(string));
StringBuilder sb = new StringBuilder();
tokenStream = new StopFilter(Version.LUCENE_36, tokenStream, ana.STOP_WORDS_SET);
CharTermAttribute token = tokenStream.getAttribute(CharTermAttribute.class);
while (tokenStream.incrementToken())
{
if (sb.length() > 0)
{
sb.append(" ");
}
sb.append(token.toString());
}
return sb.toString();
}
public static void main(String args[]) throws IOException
{
String text = "this is a java project written by james.";
Stopwords_remove stopwords = new Stopwords_remove();
stopwords.removeStopWords(text);
}
}
输出:java project written james.
所需输出:java project james.
我怎样才能做到这一点?