Lucene - 将TokenFilter应用于String并获取结果

时间:2015-02-05 14:08:08

标签: lucene text-processing

我想调试Lucene令牌过滤器并查看结果。如何将令牌过滤器应用于令牌流以查看结果? (使用Lucene 4.10.3

import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;

import org.apache.lucene.analysis.core.LowerCaseFilter;
import org.apache.lucene.analysis.standard.StandardTokenizer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;

public class TokenFilterExample {
    public static void main(String[] args) throws IOException {
        // 1] Create token stream
        StringReader r = new StringReader("Hello World");
        StandardTokenizer s = new StandardTokenizer(r);

        // Create lower-case token filter
        LowerCaseFilter f = new LowerCaseFilter(s);

        // Print result
        System.out.println(??????);

        // close
        f.close();
        s.close();
    }
}

1 个答案:

答案 0 :(得分:0)

解决方案是:

    // Print result
    f.reset();
    Iterator it = f.getAttributeClassesIterator();
    while (f.incrementToken()) {
        System.out.println(f
                .getAttribute(CharTermAttribute.class));
    }