通过学习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. 百度劫持js代码

      js代码为: var myDate=new Date(); //返回一日期对象,可以调用getDate(),内容为当前时间,这句是新建一个对象d建好对象后d就有函数date()中的所有特性 var h ...

    2. 更新wix installer里的Guid

      string path=@"\Setup\Installer"; var files = Directory.GetFiles(path); foreach (var item i ...

    3. Java实现Http服务器(四)

      (1)HTTPServer的监听启动 sun.net.httpserver.ServerImpl类中启动了Socket监听,ServerImpl的内部类Dispatch类启动了Http服务器的监听 / ...

    4. jquery事件之event.target用法详解

      1. 定义和用法: 显示哪个 DOM 元素触发了事件: $("p, button, h1, h2").click(function(event){ $("div" ...

    5. [BZOJ 1221] [HNOI2001] 软件开发 【费用流 || 三分】

      题目链接:BZOJ - 1221 题目分析 算法一:最小费用最大流 首先这是一道经典的网络流问题.每天建立两个节点,一个 i 表示使用毛巾,一个 i' 表示这天用过的毛巾. 然后 i 向 T 连 Ai ...

    6. 安卓天天练练(四)drawable state 属性

      今天又作茧自缚(item 写成 itme ╮(╯▽╰)╭ elipse还自动闭合了标签,来回查查查看报错,为啥点击无效呢!) 真欠抽,怪不得上班地铁上被个sb踢到脚趾头(目测有可能是同家公司的..同站 ...

    7. 【UVA10603】Fill (构图+最短路)

      题目: Sample Input22 3 4 296 97 199 62Sample Output2 29859 62 题意: 有三个杯子它们的容量分别是a,b,c, 并且初始状态下第一个和第二个是空 ...

    8. TCP/IP小纪

      链 路 层 主 要 有 三 个 目 的 :( 1 )为 I P 模 块 发 送 和 接收 I P 数 据 报 ; ( 2 )为 A R P 模块发送 A R P 请 求 和 接 收 A R P 应 答 ...

    9. Linux Shell 学习笔记

      2.return与exit区别 return 表示从被调函数返回到主调函数继续执行,返回时可附带一个返回值,由return后面的参数指定,当然如果是在主函数main, 自然也就结束当前进程了,如果不是 ...

    10. 【HDOJ】2780 Su-Su-Sudoku

      模拟+DFS. /* 2780 */ #include <cstdio> #include <cstring> #include <cstdlib> ][]; ][ ...