十二、Attribute

Attribute


            TokenStream stream = a.tokenStream("content",new StringReader(str));


            //位置增量的属性,存储语汇单元之间的距离
            PositionIncrementAttribute pia = stream.addAttribute(PositionIncrementAttribute.class);


            //每个语汇单元的位置偏移量
            OffsetAttribute oa = stream.addAttribute(OffsetAttribute.class);


            //存储每一个语汇单元的信息(分词单元信息)
            CharTermAttribute cta = stream.addAttribute(CharTermAttribute.class);


            //使用的分词器的类型信息
            TypeAttribute ta =  stream.addAttribute(TypeAttribute.class);


            for(;stream.incrementToken();) {
                System.out.print(pia.getPositionIncrement()+":");
                System.out.print(cta+"["+oa.startOffset()+"-"+oa.endOffset()+"]-->"+ta.type()+"\n");
            }

 

 

            Analyzer a1 = new StandardAnalyzer(Version.LUCENE_35);
        Analyzer a2 = new StopAnalyzer(Version.LUCENE_35);
        Analyzer a3 = new SimpleAnalyzer(Version.LUCENE_35);
        Analyzer a4 = new WhitespaceAnalyzer(Version.LUCENE_35);
        String txt = "how are you thank you";
       
        AnalyzerUtils.displayAllTokenInfo(txt, a1);
        System.out.println("------------------------------");
        AnalyzerUtils.displayAllTokenInfo(txt, a2);
        System.out.println("------------------------------");
        AnalyzerUtils.displayAllTokenInfo(txt, a3);
        System.out.println("------------------------------");
        AnalyzerUtils.displayAllTokenInfo(txt, a4);


得到的输出结果是:

1:how[0-3]--><ALPHANUM>
2:you[8-11]--><ALPHANUM>
1:thank[12-17]--><ALPHANUM>
1:you[18-21]--><ALPHANUM>
------------------------------
1:how[0-3]-->word
2:you[8-11]-->word
1:thank[12-17]-->word
1:you[18-21]-->word
------------------------------
1:how[0-3]-->word
1:are[4-7]-->word
1:you[8-11]-->word
1:thank[12-17]-->word
1:you[18-21]-->word
------------------------------
1:how[0-3]-->word
1:are[4-7]-->word
1:you[8-11]-->word
1:thank[12-17]-->word
1:you[18-21]-->word

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值