在没有索引的情况下使用 Lucene Analyzer - 我的方法合理吗?

Using Lucene Analyzer Without Indexing - Is My Approach Reasonable?

我的 objective 是利用 Lucene 的许多分词器和过滤器中的一些来转换输入文本,但不创建任何索引。

例如,给定这个(人为的)输入字符串...

" Someone’s - [texté] goes here, foo . "

...还有像这样的 Lucene 分析器...

Analyzer analyzer = CustomAnalyzer.builder()
        .withTokenizer("icu")
        .addTokenFilter("lowercase")
        .addTokenFilter("icuFolding")
        .build();

我想获得以下输出:

someone's texte goes here foo

下面的 Java 方法可以满足我的要求。

但是有没有更好的(即更典型的 and/or 简洁)的方式我应该这样做?

我特别在想我使用TokenStreamCharTermAttribute的方式,因为我以前从未这样使用过它们。感觉笨重。

代码如下:

Lucene 8.3.0 导入:

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.custom.CustomAnalyzer;

我的方法:

private String transform(String input) throws IOException {

    Analyzer analyzer = CustomAnalyzer.builder()
            .withTokenizer("icu")
            .addTokenFilter("lowercase")
            .addTokenFilter("icuFolding")
            .build();

    TokenStream ts = analyzer.tokenStream("myField", new StringReader(input));
    CharTermAttribute charTermAtt = ts.addAttribute(CharTermAttribute.class);

    StringBuilder sb = new StringBuilder();
    try {
        ts.reset();
        while (ts.incrementToken()) {
            sb.append(charTermAtt.toString()).append(" ");
        }
        ts.end();
    } finally {
        ts.close();
    }
    return sb.toString().trim();
}

我已经使用这个设置几个星期了,没有任何问题。我还没有找到更简洁的方法。我觉得题中的代码没问题。