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


    相关推荐

    • mycat实现读写分离_mybatis读写分离实现

      mycat实现读写分离_mybatis读写分离实现环境:四台主机,两台mysql做主从:master和slave一台mysql做测试,ip:10.30.162.142一台mycat做读写分离,ip:192.168.122.230客户端访问mycat端的虚拟数据库mycat端虚拟数据库设定如下:user:adminpassword:redhatmycat通过一个真实数据库授过权的用户来对数据库端进行数据的操作,在这个实验中这个用户为m…

      2022年10月13日
      2
    • 浅谈IOC–说清楚IOC是什么

      浅谈IOC–说清楚IOC是什么转载自:http://www.cnblogs.com/DebugLZQ/archive/2013/06/05/3107957.html1.IOC的理论背景2.什么是IOC3.IOC也叫依赖注入(DI)4.IOC的优缺点5.IOC容器的技术剖析6.IOC容器的一些产品7.参考博文本文旨在用语言(非代码)说清楚IOC到底是什么,没有什么高深的技术,园中的老牛、大虾们看到这里可以绕行了,以免浪费您宝贵的…

      2022年6月4日
      28
    • 数据结构中的elemtype、elem是什么

      数据结构中的elemtype、elem是什么ElemType是数据结构的书上为了说明问题而用的一个词。它是elementtype(“元素的类型”)的简化体。 因为数据结构是讨论抽象的数据结构和算法的,一种结构中元素的类型不一定是整型、字符型、浮点型或者用户自定义类型,为了不重复说明,使用过程中用“elemtype”代表所有可能的数据类型,简单明了的概括了整体。在算法中,除特别说明外,规定ElemType的默认是int型。elem是单词…

      2022年5月19日
      60
    • 海康威视rtsp取流地址(海康威视设置教程)

      RTSP视频流显示(海康威视)VLCSDK(C++)ffmpeg+Nginx本文目的主要是想要在html上实时显示海康威视的摄像头数据,笔者尝试了如下三种方式:VLCSDK(C++)ffmpeg+Nginx下面分别说说通过这几种方式如何实现在web页面上显示。VLC填的地址就是摄像头RTSP视频流地址,然后点串流:左上角是这样的说明就已经在转换了:网页显示注意…

      2022年4月17日
      2.6K
    • Windows下dos中 copy命令的实现

      Windows下dos中 copy命令的实现实现的的功能:复制文件功能一:功能分析1.1windows系统下的dos命令中指令copy能实现文件的复制。比如:copylog.txtlog1.txt就是将log.txt文件复制一份,复制后的文件名称为log1.txt图例:1.2copy命令实现要求:自己创造一个命令,比如:test.exelog.txttest.bak有三个参数,第一个参…

      2022年7月18日
      18
    • 更新Git工具到最新版本「建议收藏」

      Ubuntu16.04默认的软件源目前最多只能更新到2.7.4版本,而官方早就已经迭代到2.20.1了,差十几个版本号。新版的git命令工具增加了很多新功能,比如分支HEAD高亮等,相比以前,可以更加方便地脱离图形化界面操作。1、首先查看一下自己的版本是不是低于最新版:git–version2、若不是,添加Git官方的软件源:sudoadd-apt-repositorypp…

      2022年4月9日
      93

    发表回复

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

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