基于Lucene3.5.0怎样从TokenStream获得Token

基于Lucene3.5.0怎样从TokenStream获得Token

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

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109513.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 黑马ssm项目_黑马程序员社区

    黑马ssm项目_黑马程序员社区上传到https://github.com/ahn6666/ssm2021.6.13

    2025年6月18日
    3
  • idea控制台中文乱码怎么解决_idea控制台打印中文乱码

    idea控制台中文乱码怎么解决_idea控制台打印中文乱码IDEA 控制台中文乱码处理

    2022年4月20日
    174
  • xshell连接虚拟机使用的是什么连接模式_虚拟机安装ssh服务

    xshell连接虚拟机使用的是什么连接模式_虚拟机安装ssh服务XShell使用前提:1.对应的需要连接的虚拟机在vm中开机着2.下载并安装好XShell3.虚拟机网络连通(具体可看(5条消息)Hadoop(1)——Hadoop集群构建(4)——Linux系统网络配置_连胜是我偶像的博客-CSDN博客使用教程:1.点击新建,输入名称(该名称为xshell中使用的名称),输入主机(对应虚拟机的ip地址)2.右键新建的会话,点击打开3.输入账号密码进行登录4.成功标志…

    2025年11月19日
    7
  • python pycharm教程_Pycharm简单使用教程(入门小结)

    python pycharm教程_Pycharm简单使用教程(入门小结)1、下载pycharmpycharm是一种PythonIDE,能够帮助我们在编写代码时提高效率。网上提供的有专业版和教育版之分。专业版是收费的,功能更全面点。教育版或社区版是阉割版本,但它是免费的。2、pycharm的安装比较简单,官网下载备注:刚下载好的pycharm无法运行程序“Cannotstartprocess,theworkingdirectory…”,两种解决方法1.选择…

    2022年8月29日
    4
  • linux改sudo密码_linux怎么给用户权限

    linux改sudo密码_linux怎么给用户权限linux设置sudo不要密码

    2022年4月22日
    51
  • Mysql数据库课程设计

    Mysql数据库课程设计Hello小伙伴们,大家好,我是楠橘星!!今天给大家分享一下使用javafx编写的前端的Mysql数据库课程设计题库与试卷生成系统!废话不多说了,直接上截图,希望对大家有所帮助!(建议拿来参考不建议直接CV哦!)1.系统需求分析1-1、功能分析通过深入细致的调查,多方面搜集资料,以及实地考察等方法,经过总结研究,总结出了试卷生成系统的的基本的业务功能,详细如下:学生信息维护:主要完成学生的学号、班级、考试信息等操作。教师信息维护:主要是教师信息的添加、修改和删除等操作。题库信息维护

    2022年5月19日
    58

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号