基于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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 从零和使用mxnet实现线性回归

    1.线性回归从零实现(1000,)epoch:1,loss:5.7996epoch:2,loss:2.1903epoch:3,loss:0.9078epoch:4,loss:0.3178e

    2021年12月30日
    72
  • 孙鑫Java视频教程下载

    孙鑫Java视频教程下载刚刚学习Java。还没有入门。 孙鑫老师的这套教程还没有看完。把下载地址发上来,有兴趣的可以下来看看。 一共12集,每集大约2个半小时。讲得很详细,适合入门。 请用迅雷下载。 thunder://QUFodHRwOi8vdmlwLmlib29rOC5jb20vdmlkZW8vy+/2zkpBVkHO3sTRysIxMkNELzAxLnJhclpa|http://ww

    2022年5月13日
    43
  • 数据库设计工具的使用(实用)

    数据库设计工具的使用(实用)使用数据库设计工具,以下sql语句全部可以自动生成:/*==============================================================*//*DBMSname:MySQL5.0*//*Createdon:2017/5/270:57:18

    2022年7月11日
    15
  • Oracle数据库性能优化(Hbase是什么数据库)

    所有数据库包括Oracle的sql优化都是针对程序员的,而不是针对dba的,第一,尽量防止模糊,明确指出,即用列名代替*,第二,在where语句上下工夫。第三多表查询和子查询,第四尽量使用绑定。根据计算机硬件的基本性能指标及其在数据库中主要操作内容,可以整理出如下图所示的性能基本优化法则:这个优化法则归纳为5个层次:1、减少数据访问(减少磁盘…

    2022年4月17日
    54
  • Linux history命令

    Linux history命令1、在脚本中由于是在另外一个shell中进行语句的执行,所以history显示的是脚本运行的shell的history语句,而不会显示你执行该脚本的终端中的history2、我们可以在家目录下的.bash_history文件中查看自己的历史命令,而history查看的是内存中的历史命令,如果需要将内存中的历史命令加入其中,那么就需要使用history-w将当前终端的历史命令覆盖.bash_history的内容或是history-a在.bash_history文件的尾部添加当前shell的历史命令

    2022年7月13日
    18
  • request.getParameterNames_request和urllib区别

    request.getParameterNames_request和urllib区别Servlet中request.getParameter和getParameterValues getParameterNames三者区别1.request.getParameter:获取前台表单单个元素name对应的value值2.request.getParameterValues:获取前台表单多个标签同名name对应的所有value值3.request.getParameterN…

    2022年9月4日
    2

发表回复

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

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