使用 Lucene 提取英语单词

2022-01-15 00:00:00 lucene stemming porter-stemmer java

我正在一个 Java 应用程序中处理一些英文文本,我需要对它们进行词干处理.例如,从文本amenities/amenity"我需要得到amenit".

I'm processing some English texts in a Java application, and I need to stem them. For example, from the text "amenities/amenity" I need to get "amenit".

函数如下:

String stemTerm(String term){
   ...
}

我找到了 Lucene Analyzer,但它看起来太复杂了,无法满足我的需要.http://lucene.apache.org/java/2_2_0/api/org/apache/lucene/analysis/PorterStemFilter.html

I've found the Lucene Analyzer, but it looks way too complicated for what I need. http://lucene.apache.org/java/2_2_0/api/org/apache/lucene/analysis/PorterStemFilter.html

有没有办法在不构建分析器的情况下使用它来词干?我不了解所有 Analyzer 业务...

Is there a way to use it to stem words without building an Analyzer? I don't understand all the Analyzer business...

编辑:我实际上需要词干提取+词形还原.Lucene 可以做到这一点吗?

EDIT: I actually need a stemming + lemmatization. Can Lucene do this?

推荐答案

import org.apache.lucene.analysis.PorterStemmer;
...
String stemTerm (String term) {
    PorterStemmer stemmer = new PorterStemmer();
    return stemmer.stem(term);
}

参见这里 了解更多详情.如果您只想使用词干提取,那么您应该使用 this 而不是 Lucene.

See here for more details. If stemming is all you want to do, then you should use this instead of Lucene.

您应该在将 term 传递给 stem() 之前将其小写.

You should lowercase term before passing it to stem().

相关文章