通过学习Lucene3.5.0的doc文档,对不同release版本号 lucene版本号的API修改做分析。最后找到了有价值的修改信息。

  • LUCENE-2302: Deprecated TermAttribute and replaced by a new CharTermAttribute. The change is backwards compatible, so mixed new/old TokenStreams all work on the same char[] buffer independent
    of which interface they use. CharTermAttribute has shorter method names and implements CharSequence and Appendable. This allows usage like Java's StringBuilder in addition to direct char[] access. Also terms can directly be used in places where CharSequence
    is allowed (e.g. regular expressions). (Uwe Schindler, Robert Muir)
  • 以上信息可以知道,原来的通过的方法已经不可以提取响应的Token了
    StringReader reader = new StringReader(s);
    TokenStream ts =analyzer.tokenStream(s, reader);
    TermAttribute ta = ts.getAttribute(TermAttribute.class);
  • 通过分析Api文档信息 可知,CharTermAttribute已经成为替换TermAttribute的接口
  • 因此我编写了一个样例来更好的从TokenStream中提取Token
  • package com.segment;
    
    import java.io.StringReader;
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.Token;
    import org.apache.lucene.analysis.TokenStream;
    import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
    import org.apache.lucene.analysis.tokenattributes.TermAttribute;
    import org.apache.lucene.util.AttributeImpl;
    import org.wltea.analyzer.lucene.IKAnalyzer; public class Segment {
    public static String show(Analyzer a, String s) throws Exception { StringReader reader = new StringReader(s);
    TokenStream ts = a.tokenStream(s, reader);
    String s1 = "", s2 = "";
    boolean hasnext= ts.incrementToken();
    //Token t = ts.next();
    while (hasnext) {
    //AttributeImpl ta = new AttributeImpl();
    CharTermAttribute ta = ts.getAttribute(CharTermAttribute.class);
    //TermAttribute ta = ts.getAttribute(TermAttribute.class); s2 = ta.toString() + " ";
    s1 += s2;
    hasnext = ts.incrementToken();
    }
    return s1;
    } public String segment(String s) throws Exception {
    Analyzer a = new IKAnalyzer();
    return show(a, s);
    }
    public static void main(String args[])
    {
    String name = "我是俊杰,我爱编程,我的測试用例";
    Segment s = new Segment();
    String test = "";
    try {
    System.out.println(test+s.segment(name));
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } }
  • 基于Lucene3.5.0怎样从TokenStream获得Token的更多相关文章

    1. solr&lucene3.6.0源码解析(二)

      上文描述了solr3.6.0怎么采用maven管理的方式在eclipse中搭建开发环境,在solr中,为了提高搜索性能,采用了缓存机制,这里描述的是LRU缓存,这里用到了 LinkedHashMap类 ...

    2. 谈谈基于OAuth 2.0的第三方认证 [下篇]

      从安全的角度来讲,<中篇>介绍的Implicit类型的Authorization Grant存在这样的两个问题:其一,授权服务器没有对客户端应用进行认证,因为获取Access Token的 ...

    3. 谈谈基于OAuth 2.0的第三方认证 [中篇]

      虽然我们在<上篇>分别讨论了4种预定义的Authorization Grant类型以及它们各自的适用场景的获取Access Token的方式,我想很多之前没有接触过OAuth 2.0的读者 ...

    4. 谈谈基于OAuth 2.0的第三方认证 [上篇]

      对于目前大部分Web应用来说,用户认证基本上都由应用自身来完成.具体来说,Web应用利用自身存储的用户凭证(基本上是用户名/密码)与用户提供的凭证进行比较进而确认其真实身份.但是这种由Web应用全权负 ...

    5. solr&lucene3.6.0源码解析(四)

      本文要描述的是solr的查询插件,该查询插件目的用于生成Lucene的查询Query,类似于查询条件表达式,与solr查询插件相关UML类图如下: 如果我们强行将上面的类图纳入某种设计模式语言的话,本 ...

    6. solr&lucene3.6.0源码解析(三)

      solr索引操作(包括新增 更新 删除 提交 合并等)相关UML图如下 从上面的类图我们可以发现,其中体现了工厂方法模式及责任链模式的运用 UpdateRequestProcessor相当于责任链模式 ...

    7. 基于Qt5.5.0的sql数据库、SDK_tts文本语音朗读的CET四六级单词背诵系统软件的编写V1.0

      作者:小波 QQ:463431476 请关注我的博客园:http://www.cnblogs.com/xiaobo-Linux/ 我的第二款软件:CET四六级单词背诵软件.基于QT5.5.0.sql数 ...

    8. 基于Qt5.5.0的sql,C++备忘录软件的编写

      我的第一个软件. 基于Qt5.5.0的 sql ,C++备忘录软件version1.0的编写 我用的Qt版本是5.5.0免配置编译器的版本,这里附上我使用的软件下载地址:http://download ...

    9. 基于Spark1.3.0的Spark sql三个核心部分

      基于Spark1.3.0的Spark sql三个核心部分: 1.可以架子啊各种结构化数据源(JSON,Hive,and Parquet) 2.可以让你通过SQL,saprk内部程序或者外部攻击,通过标 ...

    随机推荐

    1. css3学习--css3动画详解二(3d效果)

      一.设置3D场景 perspective:800       //浏览器到物体的距离(像素)perspective-origin:50% (x轴) 50% (y轴)    //视点的位置 transf ...

    2. Jquery获取元素坐标

      获取页面某一元素的绝对X,Y坐标,相对于浏览器窗体,可以用offset()方法: var X = $('#DivID').offset().top; var Y = $('#DivID').offse ...

    3. Window7 下开发php扩展

      一.首先查看phpinfo() 信息PHP Version 5.4.34Zend Extension Build     API220100525,TS,VC9 PHP Extension Build ...

    4. 【转】TypeScript中文入门教程

      目录 虽然我是转载的,但看在Copy这么多文章也是很幸苦的好吧,我罗列一个目录. 转载:<TypeScript 中文入门教程> 17.注解 (2015-12-03 11:36) 转载:&l ...

    5. 2016最新Java笔试题集锦

      更新时间:2015-08-13         来源:网络         投诉删除 [看准网(Kanzhun.com)]笔试题目频道小编搜集的范文“2016最新Java笔试题集锦”,供大家阅读参考, ...

    6. python将图片转为base64编码

      import base64 f = open("m1.jpg", "rb") res = f.read() s = base64.b64encode(res) ...

    7. iOS版 hello,world版本2

      // // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. All rig ...

    8. 用word2013写博客

      第一次使用,添加博客账户时碰到一个奇怪的问题,先输入用户名,然后密码只能输入两个字符,后来先输入密码再输入用户名才解决,很神奇~

    9. open files

      /* * * Copyright (c) International Business Machines Corp., 2001 * * This program is free software; ...

    10. oracle中有关用户、角色的一些概念。

      oracle中的每个用户对应一个单独的方案(schema),方案的名字与用户名一样,方案中包含很多数据对象,表,视图,触发器,存储过程等元素. oracle中管理数据库的角色有sys,system,数 ...