基于Lucene3.5.0如何从TokenStream获得Token

基于Lucene3.5.0如何从TokenStream获得Token通过学习Lucene3.5.0的doc文档,对不同release版本lucene版本的API改动做分析。最后找到了有价值的改动信息。LUCENE-2302:DeprecatedTermAttributeandreplacedbyanewCharTermAttribute.Thechangeisbackwardscompatible,somixednew/old

大家好,又见面了,我是你们的朋友全栈君。通过学习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了
    1. StringReader reader = new StringReader(s);  
    2. TokenStream ts =analyzer.tokenStream(s, reader);  
    3. TermAttribute ta = ts.getAttribute(TermAttribute.class);  

  • 通过分析Api文档信息 可知,CharTermAttribute已经成为替换TermAttribute的接口
  • 因此我编写了一个例子来更好的从TokenStream中提取Token
    1. package com.segment;  
    2.   
    3. import java.io.StringReader;  
    4. import org.apache.lucene.analysis.Analyzer;  
    5. import org.apache.lucene.analysis.Token;  
    6. import org.apache.lucene.analysis.TokenStream;  
    7. import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;  
    8. import org.apache.lucene.analysis.tokenattributes.TermAttribute;  
    9. import org.apache.lucene.util.AttributeImpl;  
    10. import org.wltea.analyzer.lucene.IKAnalyzer;  
    11.   
    12.   
    13. public class Segment {  
    14.     public static String show(Analyzer a, String s) throws Exception {  
    15.   
    16.         StringReader reader = new StringReader(s);  
    17.         TokenStream ts = a.tokenStream(s, reader);  
    18.         String s1 = “”s2 = “”;  
    19.         boolean hasnextts.incrementToken();  
    20.         //Token t = ts.next();  
    21.         while (hasnext) {  
    22.             //AttributeImpl ta = new AttributeImpl();  
    23.             CharTermAttribute ta = ts.getAttribute(CharTermAttribute.class);  
    24.             //TermAttribute ta = ts.getAttribute(TermAttribute.class);  
    25.               
    26.             s2 = ta.toString() + ” “;  
    27.             s1 += s2;  
    28.             hasnext = ts.incrementToken();  
    29.         }  
    30.         return s1;  
    31.     }  
    32.   
    33.     public String segment(String s) throws Exception {  
    34.         Analyzer a = new IKAnalyzer();  
    35.         return show(a, s);  
    36.     }  
    37.     public static void main(String args[])  
    38.     {  
    39.         String name = “我是俊杰,我爱编程,我的测试用例”;  
    40.         Segment s = new Segment();  
    41.         String test = “”;  
    42.         try {  
    43.             System.out.println(test+s.segment(name));  
    44.         } catch (Exception e) {  
    45.             // TODO Auto-generated catch block  
    46.             e.printStackTrace();  
    47.         }  
    48.     }  
    49.   
    50. }  
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163085.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月22日 下午2:46
下一篇 2022年7月22日 下午2:46


相关推荐

  • python对随机森林分类结果绘制roc曲线

    python对随机森林分类结果绘制roc曲线上图:附上代码:一个函数,传入三个参数…..传入参数,训练模型,然后:fit=model.fit(x_train,y_training)#ROCy_score=model.fit(x_train,y_training).predict_proba(x_test)#随机森林fpr,tpr,thresholds=roc_curve(y_test,…

    2022年5月17日
    84
  • 基本粒子群算法小结及算法实例(附Matlab代码)

    基本粒子群算法小结及算法实例(附Matlab代码)1、基本粒子群算法假设在一个D维的目标搜索空间中,有N个粒子组成一个群落,其中第i个粒子表示为一个D维的向量:$$X_i=(x_{i1},x_{i2},\cdots,x_{iD}),\quadi=1,2,\cdots,N\quad\text{(1)}$$第i个粒子的“飞行”速度也是一个D维的向量,记为:$$V_i=(v_{i1},v_{i2},\cdots,v_{iD}),\quadi=1,2,\cdots,N\quad\te…

    2022年5月29日
    36
  • UNIX基本操作命令

    UNIX基本操作命令UNIX命令基础登录系统:输入密码,密码不显示退出系统:ctrl+dexitlogout创建/修改密码:passwd密码输入后不显示,新密码要输入两次,匹配成功才算是修改密码成功UNIX命令格式:命令选项参数快捷键:ctrl+c:中断当前命令执行并返回shellctrl+d:终端传输或结束文本编辑ctrl+s:临时中断命令的屏幕输出ctrl+q:恢复由ctrl+s的临时中断ctrl+u:清除输入的整个命令行backspace:删除某个错误输入的字符常用操作命令

    2022年5月31日
    435
  • rabbitmq基本原理_计算尺使用的是什么原理

    rabbitmq基本原理_计算尺使用的是什么原理RabbitMQ使用以及原理解析RabbitMQ是一个由erlang开发的AMQP(AdvanvedMessageQueue)的开源实现;在RabbitMQ官网上主要有这样的模块信息,Workqueues消息队列,Publish/Subscribe发布订阅服务,Routing,Topics,RPC等主要应用的模块功能.几个概念说明:Broker:它提供一种传输服务,它的角色…

    2026年4月13日
    2
  • POJ2309 BST

    POJ2309 BST

    2022年2月21日
    47
  • jsonschema校验json数据_接口校验不通过

    jsonschema校验json数据_接口校验不通过何为Json-SchemaJson-schema是描述你的JSON数据格式;JSON模式(应用程序/模式+JSON)有多种用途,其中之一就是实例验证。验证过程可以是交互式或非交互式的。例如,应用程序可以使用JSON模式来构建用户界面使互动的内容生成除了用户输入检查或验证各种来源获取的数据。(来自百度百科)相关jar包<dependency><groupId>com.github.fge</groupId><artifactId&g

    2025年10月1日
    5

发表回复

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

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