4
我正在使用lucene刪除英語停用詞,但我的要求是刪除英語停用詞和自定義停用詞。以下是我使用lucene刪除英文停用詞的代碼。如何在java中使用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.
我怎樣才能做到這一點?
其工作表示感謝。 – user2609542