有没有一种简单的方法可以使用 Lucene 的 Analyzer
的任何子类来解析/标记 String
?
类似:
String to_be_parsed = "car window seven";
Analyzer analyzer = new StandardAnalyzer(...);
List<String> tokenized_string = analyzer.analyze(to_be_parsed);
最佳答案
根据上面的答案,这稍作修改以与 Lucene 4.0 一起使用。
public final class LuceneUtil {
private LuceneUtil() {}
public static List<String> tokenizeString(Analyzer analyzer, String string) {
List<String> result = new ArrayList<String>();
try {
TokenStream stream = analyzer.tokenStream(null, new StringReader(string));
stream.reset();
while (stream.incrementToken()) {
result.add(stream.getAttribute(CharTermAttribute.class).toString());
}
} catch (IOException e) {
// not thrown b/c we're using a string reader...
throw new RuntimeException(e);
}
return result;
}
}
关于java - 如何使用 Lucene Analyzer 标记字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6334692/