lucene 查看分词器分出的词

import java.io.StringReader;

import net.paoding.analysis.analyzer.PaodingAnalyzer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;

public class TestPaoDing {
    public static void main(String[] args) throws Exception {
        // StandardAnalyzer: 一元分词
        // Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_29)  ;
        // PaodingAnalyzer: 二元分词
          Analyzer analyzer = new PaodingAnalyzer();
        String indexStr = "人之所以痛苦,在于追求错误的东西";
        StringReader reader = new StringReader(indexStr);
        TokenStream ts = analyzer.tokenStream(null, reader);
        TermAttribute termAtt = (TermAttribute) ts
                .getAttribute(TermAttribute.class);
        TypeAttribute typeAtt = (TypeAttribute) ts
                .getAttribute(TypeAttribute.class);

        while (ts.incrementToken()) {
            System.out.print(termAtt.term());
            System.out.print(' ');
            System.out.println(typeAtt.type());
        }

    }
}

 

 

=======================================================

import java.io.StringReader;

import net.paoding.analysis.analyzer.PaodingAnalyzer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;


public class TestPaoDing {
    public static void main(String[] args) throws Exception {
        //StandardAnalyzer: 一元分词
        //Analyzer analyzer = new StandardAnalyzer();
        //PaodingAnalyzer: 二元分词
        Analyzer analyzer = new PaodingAnalyzer();
        String indexStr = "围城,我们都是好孩子";
        StringReader reader = new StringReader(indexStr);
        TokenStream ts = analyzer.tokenStream(null, reader);
        Token t = ts.next();
        while (t != null) {
            System.out.print(t.termText()+"   ");
            t = ts.next();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值