我正在寻找在java中阻止字符串的可能性。 首先,我想用lucene,但所有的例子 我发现在网上被弃用了。 (SnowballAnalyzer, PorterStemmer,...) 我只想干完整句话。
public static String stemSentence(String sentence) {
...
return stemmedSentence;
}
我该怎么做?
答案 0 :(得分:4)
制作本:
public static String stem(String string) throws IOException {
TokenStream tokenizer = new StandardTokenizer(Version.LUCENE_47, new StringReader(string));
tokenizer = new StandardFilter(Version.LUCENE_47, tokenizer);
tokenizer = new LowerCaseFilter(Version.LUCENE_47, tokenizer);
tokenizer = new PorterStemFilter(tokenizer);
CharTermAttribute token = tokenizer.getAttribute(CharTermAttribute.class);
tokenizer.reset();
StringBuilder stringBuilder = new StringBuilder();
while(tokenizer.incrementToken()) {
if(stringBuilder.length() > 0 ) {
stringBuilder.append(" ");
}
stringBuilder.append(token.toString());
}
tokenizer.end();
tokenizer.close();
return stringBuilder.toString();
}