package com.telenav.onboard.commons.analyzers.filters;
import java.io.IOException;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
public final class NonAlphaNumberFilter extends TokenFilter {
private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
public NonAlphaNumberFilter(TokenStream in) {
super(in);
}
@Override
public final boolean incrementToken() throws IOException {
if (input.incrementToken()) {
char[] buffer = termAtt.buffer();
int len = termAtt.length();
termAtt.setEmpty();
for (int i = 0; i < len; i++) {
if (accept(buffer[i])) {
termAtt.append(buffer[i]);
}
}
return true;
} else
return false;
}
private boolean accept(char ch) {
return ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'));
}
}
Lucene Filter的例子
最新推荐文章于 2021-06-21 21:43:24 发布