基于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/118919.html原文链接:https://javaforall.net

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


    相关推荐

    • potplayer使用madvr后没有了_potplayer配置

      potplayer使用madvr后没有了_potplayer配置 所需资源下载 Resource Version Download Notes Potplayer   Download 根据系统选择32位或64位安装文件 LAVFilters 0.68.1 Download 32位或64位版本应对应于Potplayer版本 madVR 0.90.24 Download …

      2022年9月14日
      0
    • MBUS协议_协议解析

      MBUS协议_协议解析来自于我的163博客,一篇好好的技术文章,在163上居然提示不能显示,我2014年6月25日就翻译出来了,当时很多其他博客都是直接拷贝我的文章都能在网上发布。看了许多关于MBus协议的资料,感觉说的不具体、不完整、也不系统,本人准备结合一个具体的产品实现,从理论和实现上对MBus协议做一个详细的论述,如有不当之处,欢迎讨论。1介绍MBus(MeterB…

      2022年10月16日
      0
    • [029] 微信公众帐号开发教程第5篇-各种消息的接收与响应[通俗易懂]

      [029] 微信公众帐号开发教程第5篇-各种消息的接收与响应[通俗易懂]前一篇文章里我们已经把微信公众平台接口中消息及相关操作都进行了封装,本章节将主要介绍如何接收微信服务器发送的消息并做出响应。明确在哪接收消息从微信公众平台接口消息指南中可以了解到,当用户向公众帐号发消息时,微信服务器会将消息通过POST方式提交给我们在接口配置信息中填写的URL,而我们就需要在URL所指向的请求处理类CoreServlet的doPost方法中接收消息、处理消息和响应

      2022年9月28日
      0
    • 关于属性描述符PropertyDescriptor[通俗易懂]

      关于属性描述符PropertyDescriptor[通俗易懂]本文首发于本博客猫叔的博客,转载请申明出处前言感谢GY丶L粉丝的提问:属性描述器PropertyDescriptor是干嘛用的?本来我也没有仔细了解过描述符这一块的知识,不过粉丝问了,我就抽周末的时间看看,顺便学习一下,粉丝问的刚好是PropertyDescriptor这个属性描述符,我看了下源码。/***AProper…

      2022年10月1日
      0
    • micropython教程(Python集成开发环境)

      本文旨在通过一个简单的demo,介绍基于Python3、PyQT5的环境下开发桌面应用程序的一种方案,当然开发Python的桌面应用程序不止是PyQT这一种方案,还可以使用Python自带的Tkinter来实现。本文目录:1.安装依赖环境2.安装Eric63.配置Eric4.创建窗口应用4.1创建窗体UI4.2实现代码逻辑参考资料:1.安装依赖环境Eric6官网:htt…

      2022年4月17日
      104
    • PHP array_multisort()函数超详细理解

      PHP array_multisort()函数超详细理解

      2021年10月25日
      41

    发表回复

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

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