基于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)
上一篇 2021年9月4日 下午8:00
下一篇 2021年9月4日 下午9:00


相关推荐

  • Android 学习路线

    Android 学习路线1 性能 内存调优 2 JNI 和 NDK3 插件化 热修复 组件化 4 算法和数据结构 5 js 与本地交互 6 Socket 通信 TCP IP 和 HTTP7 自定义控件 8 音 视频 9 OKHttp Retrofit RxJava Gson10 服务 线程 aidl 进程的通信机制

    2026年3月17日
    3
  • 地理空间数据云DEM数据解压失败_解决了

    地理空间数据云DEM数据解压失败_解决了准备计算土壤保持生态系统服务 需要一个分水岭矢量图 所以先从空间数据云下 Dem 发现 SRTNSLOPE90M 下载好了解压的时候会报错 搜了一下看到有人说直接解压到别的盘发现没用 后来去搜了一下 img 格式在 arcgis 中怎么打开 才知道这种格式不需要解压直接可以打开

    2026年1月15日
    2
  • pycharm快捷键、常用设置、配置管理「建议收藏」

    pycharm快捷键、常用设置、配置管理「建议收藏」http://blog.csdn.net/pipisorry/article/details/39909057本博客一直在同步更新中!内容包含:pycharm学习技巧Learningtips、PyCharm3.0默认快捷键(翻译的)、pycharm常用设置、pycharm环境和路径配置、Pycharm实用拓展功能:pycharm中清除已编译.pyc中间文件、python2转python…

    2022年6月14日
    53
  • sublime 激活码【中文破解版】「建议收藏」

    (sublime 激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~S32P…

    2022年3月26日
    49
  • IDEA代码格式化快捷键

    IDEA代码格式化快捷键Ctrl Alt LIDEA 代码格式化快捷键简直不要太好用

    2026年3月19日
    3
  • Sublime Text3 的 Markdown 实时预览全面总结

    Sublime Text3 的 Markdown 实时预览全面总结如前文《SublimeText3的插件管理工具(PackageControl)的安装及使用方法》所说,Sublime有强大的插件扩展功能,本文介绍如何在用Sublime写Markdown文档时,做到效果预览。1.插件介绍先介绍两个有关Markdown的常用插件:MarkdownEditing和MarkdownPreviewMarkdownEditing顾名思义,Ma…

    2022年7月27日
    4

发表回复

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

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