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


相关推荐

  • python关于缩进_python缩进符号

    python关于缩进_python缩进符号在写作文的时候,老师会告诉我们每段要空两格,这两个空格标志着一个新的段落开始了。在编写程序的时候,我们也要采用类似的方式,通过缩进来表示代码块的开始和结束。认识缩进在之前学过的的例子中,我们所编写的都是简单的表达式语句,没有缩进。但是,要创建复合语句,就需要用到缩进这个重要的概念。我们可以把许多代码行组织到一个代码块中,其中的每一行代码的开始,都保持相同的空格数,通过查看代码行前面的空格数,就可以…

    2022年10月13日
    0
  • 【教程】Spring+Mybatis环境配置多数据源

    一、简要概述在做项目的时候遇到需要从两个数据源获取数据,项目使用的Spring + Mybatis环境,看到网上有一些关于多数据源的配置,自己也整理学习一下,然后自动切换实现从不同的数据源获取数据功能。二、代码详解2.1 DataSourceConstants 数据源常量类/** * 数据源名称常量类 * 对应 application.xml 中 bean multipleDataSo…

    2022年2月27日
    29
  • nginx系列之一:nginx入门

    nginx系列之一:nginx入门一、nginx功能介绍Nginx因为它的稳定性、丰富的模块库、灵活的配置和低系统资源的消耗而闻名.业界一致认为它是Apache2.2+mod_proxy_balancer的轻量级代替者,不仅是因为响应静态页面的速度非常快,而且它的模块数量达到Apache的近2/3。对proxy和rewrite模块的支持很彻底,还支持mod_fcgi、ssl、vhosts,适合用来做mongrelclust…

    2022年6月2日
    29
  • iptables命令

    iptables命令

    2022年2月8日
    45
  • python 网络编程

    python 网络编程

    2021年7月8日
    80
  • 漂亮的表格样式(使用CSS样式表控制表格样式)

    漂亮的表格样式(使用CSS样式表控制表格样式)

    2021年9月7日
    47

发表回复

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

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