39

AnalyzerLucene のサブクラスを使用して を解析/トークン化する簡単な方法はありStringますか?

何かのようなもの:

String to_be_parsed = "car window seven";
Analyzer analyzer = new StandardAnalyzer(...);
List<String> tokenized_string = analyzer.analyze(to_be_parsed);
4

4 に答える 4

40

私の知る限り、ループは自分で作成する必要があります。このようなもの(ソースツリーから直接取得):

public final class LuceneUtils {

    public static List<String> parseKeywords(Analyzer analyzer, String field, String keywords) {

        List<String> result = new ArrayList<String>();
        TokenStream stream  = analyzer.tokenStream(field, new StringReader(keywords));

        try {
            while(stream.incrementToken()) {
                result.add(stream.getAttribute(TermAttribute.class).term());
            }
        }
        catch(IOException e) {
            // not thrown b/c we're using a string reader...
        }

        return result;
    }  
}
于 2011-06-13T19:11:46.797 に答える