扩展TokenFilter,实现二次分词

3 篇文章 0 订阅
0 篇文章 0 订阅

经过paoding分词后,再对每个token进行2次分词,此处是二元切分法

 

public class MyCJKFilter extends TokenFilter {

    private TermAttribute termAtt = (TermAttribute) addAttribute(TermAttribute.class);;
    private OffsetAttribute offsetAtt = (OffsetAttribute) addAttribute(OffsetAttribute.class);
    private PositionIncrementAttribute posAtt = (PositionIncrementAttribute) addAttribute(PositionIncrementAttribute.class);

    protected MyCJKFilter(TokenStream input) {
        super(input);
    }

    private Vector<Token> bufferToken = new Vector<Token>();

    private int count = 0;

    private CJKAnalyzer analyzer = new CJKAnalyzer();

    Map<String, Token> map = new HashMap<String, Token>();

    @Override
    public final boolean incrementToken() throws IOException {
        if (this.bufferToken.size() > 0) {
            Token t = this.bufferToken.remove(0);
            this.termAtt.setTermBuffer(t.term());
            this.offsetAtt.setOffset(t.startOffset(), t.endOffset());
            this.posAtt.setPositionIncrement(t.getPositionIncrement());
            return true;
        }
        if (this.bufferToken.size() == 0 && this.count > 0) {
            // System.out.println("count is > 0!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            count = 0;
            return false;
        }

        map.clear();

        while (input.incrementToken()) {
            this.termAtt = (TermAttribute) input.getAttribute(TermAttribute.class);
            this.offsetAtt = (OffsetAttribute) input.getAttribute(OffsetAttribute.class);
            this.posAtt = (PositionIncrementAttribute) input.getAttribute(PositionIncrementAttribute.class);
            String term = this.termAtt.term();
            Token tokenOri = new Token(term, this.offsetAtt.startOffset(), this.offsetAtt.endOffset());
            this.bufferToken.add(tokenOri);
            map.put(term, tokenOri);

            // System.out.println(term + "-->" + this.offsetAtt.startOffset() + "," + this.offsetAtt.endOffset());

            TokenStream ts = this.analyzer.tokenStream("", new StringReader(term));
            while (ts.incrementToken()) {
                TermAttribute ta = (TermAttribute) ts.getAttribute(TermAttribute.class);
                if (map.containsKey(ta.term())) {
                    continue;
                }
                OffsetAttribute offa = (OffsetAttribute) ts.getAttribute(OffsetAttribute.class);
                // System.out.println(ta.term() + "-->" + offa.startOffset() + "," + offa.endOffset());
                Token token = new Token(ta.term(), offa.startOffset(), offa.endOffset());
                if (token == null) {
                    // System.out.println("ts.next() is null");
                } else {
                    this.bufferToken.add(token);
                    // System.out.println("add to vector, term=" + token.term());
                }
            }
            count++;
        }

        if (bufferToken.size() > 0) {
            return this.incrementToken();
        } else {
            return false;
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值