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


    相关推荐

    • 数据分析sql面试必会6题经典_SQL常见面试题[通俗易懂]

      数据分析sql面试必会6题经典_SQL常见面试题[通俗易懂]SQL主要是用来到数据库里查询数据,这是数据分析过程的第一步。你要分析数据,首先要获取数据。所以,这是转行到数据分析岗位的必备技能。感谢@猴子老师的“猴子聊人物”公众号,我入门SQL就是在猴子的“转行数据分析师闯关教程”里学到的。柯本:新手如何学习SQL​zhuanlan.zhihu.com柯本:《MySQL必知必会》学习小结​zhuanlan.zhihu.com一、SQL性能优化题SQL语句…

      2022年4月19日
      208
    • 贝叶斯公式的理解【转】

      贝叶斯公式的理解【转】本文转载自:https://blog.csdn.net/qq_37953276/article/details/79297316作者:知乎用户 链接:https://www.zhihu.com/question/21134457/answer/169523403来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。最近我自己在学习一些关于机器学习的东西,…

      2022年5月5日
      51
    • 苹果备忘录导出到android,怎么把苹果的备忘录转到安卓系统?[通俗易懂]

      苹果备忘录导出到android,怎么把苹果的备忘录转到安卓系统?[通俗易懂]原标题:怎么把苹果的备忘录转到安卓系统?我的闺蜜小张之前一直使用的是苹果手机,她使用过苹果6s和iPhone8plus,不过最近这次在更换手机的时候,小张为了支持国产手机,就入手了一款华为手机。在试用了一两天的新华为手机后,小张表示这款华为手机也是比较好用的,于是小张就想把之前的苹果手机备忘录内容转到安卓系统中以便继续使用。彩色便签不过小张发现苹果自带备忘录内容是不支持授权一键导出所有数据的,这…

      2022年5月9日
      182
    • 创建外部用户_外部表

      创建外部用户_外部表

      2022年4月3日
      49
    • mysql查询前5条记录_各个数据库中,查询前n条记录的方法「建议收藏」

      mysql查询前5条记录_各个数据库中,查询前n条记录的方法「建议收藏」SQL查询前10条的方法为:1.selecttopX*fromtable_name–查询前X条记录,可以改成需要的数字,比如前10条。2.selecttopX*fromtable_nameorderbycolum_namedesc–按colum_name属性降序排序查询前X条记录,“orderby”后紧跟要排序的属性列名,其中desc表示降序,asc表示升序…

      2022年9月8日
      1
    • 具体说明 Flume介绍、安装和配置

      具体说明 Flume介绍、安装和配置

      2022年1月6日
      41

    发表回复

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

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