通过学习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 blocke.printStackTrace();}}}

转载于:https://www.cnblogs.com/mfrbuaa/p/4266936.html

基于Lucene3.5.0怎样从TokenStream获得Token相关推荐

  1. 基于Lucene3.5.0如何从TokenStream获得Token

    通过学习Lucene3.5.0的doc文档,对不同release版本 lucene版本的API改动做分析.最后找到了有价值的改动信息. LUCENE-2302: Deprecated TermAttr ...

  2. Element 2.6.0 发布,基于 Vue 2.0 的桌面端组件库

    开发四年只会写业务代码,分布式高并发都不会还做程序员?   Element 2.6.0 发布了,Element 是一套为开发者.设计师和产品经理准备的基于 Vue 2.0 的桌面端组件库,提供了配套设 ...

  3. 深度学习实战—基于TensorFlow 2.0的人工智能开发应用

    作者:辛大奇 著 出版社:中国水利水电出版社 品牌:智博尚书 出版时间:2020-10-01 深度学习实战-基于TensorFlow 2.0的人工智能开发应用

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

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

  5. 基于TensorFlow 2.0的中文深度学习开源书来了!GitHub趋势日榜第一,斩获2K+星

    十三 发自 凹非寺  量子位 报道 | 公众号 QbitAI TensorFlow 2.0 发布已有一个半月之久,你会用了吗? 近日,一个叫做深度学习开源书的项目在火了.GitHub趋势日榜排名全球第 ...

  6. 基于.Net 2.0 (C# 2.0, ASP.NET 2.0)的快速Web开发框架设计

    基于.Net 2.0 (C# 2.0, ASP.NET 2.0)的快速Web开发框架设计 整个Framework结构如下: Framework.Common - 常用组件(Design By Cont ...

  7. 基于stm32、0.96寸OLED实现的贪吃蛇小游戏(详细源码注释)

    简介:本实验基于stm32最小系统.0.96寸OLED(68*128)和摇杆实现一个经典的贪吃蛇小游戏.项目源码地址:点击下载. 硬件设计: 普通摇杆,0.96寸OLED 单色屏幕(SPI协议通讯), ...

  8. 基于stm32、0.96寸OLED实现的俄罗斯方块小游戏(详细源码注释)

    概述:本实验基于stm32最小系统.0.96寸OLED(68*128)和摇杆实现一个经典的俄罗斯方块小游戏.项目源码地址:点击下载. 硬件要求: 普通摇杆,两个电位器和一个开关组成,左右摇动控制一个电 ...

  9. 鸿蒙系统8xmax,华为8月9日发布基于安卓10.0全新系统:自主鸿蒙会同台亮相

    7月27日消息,对于即将开幕的华为开发者大会,华为终端官方再次给出消息称,在8月9日首天的大会上,他们将发布新一代基于Android Q(也叫做Android 10.0)的手机系统EMUI 10.0. ...

最新文章

  1. 最近做了一个博客 玩玩而已 运城搜搜 www.lenovoyh.com
  2. tokenizer.encode() 与 tokenizer.tokenize()对比,言简意赅 转 高人讲学
  3. word 通配符_word通配符技巧:HR高效办公技巧应用
  4. [Machine Learning]kNN代码实现(Kd tree)
  5. 冒泡排序的多种写法、逻辑
  6. flash写保护原理_为什么固态会掉盘?著名的30分钟大法修复是什么原理?
  7. 创业产品经理需要懂技术吗?
  8. 基本的Windows相关的DOS命令
  9. linux-用户管理190919
  10. C语言预处理#pragma
  11. SlickEdit完美解析Qt4
  12. select 统计数量_SQL应用实战 | 销售数据统计指标
  13. zabbix通过OMSA监控Dell服务器
  14. ubuntu安装deb,rpm安装包方法
  15. 由A* = A^T推导问题
  16. 【每日算法Day 88】超越妹妹教你如何做这道排序题
  17. 【优化算法】缎面弓箭鸟优化(SBO)【含Matlab源码 1432期】
  18. 初学者如何选择网络仿真软件
  19. 快速入门nebula graph
  20. R语言dplyr入门到进阶

热门文章

  1. itunes ios_与iTunes同步时如何防止iOS应用重新安装
  2. 省选专练之 CERC2017 Kitchen Knobs 炸鸡店
  3. PV操作实现进程同步问题
  4. 手机查看QQ好友撤回图片和闪照
  5. android群发短信到通讯录所有联系人上并动态替换其中联系人信息
  6. 安卓app开发菜鸟教程!开发者必备的顶级Android开发工具,吐血整理
  7. 无线桥接dns服务器无响应,路由器无线桥接后不能登录副路由怎么办(2)
  8. 【车间调度】鸟群算法求解车间调度问题【含Matlab源码 1395期】
  9. java静态代码块在什么时候会执行
  10. CQ2022省选游记+总结