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


相关推荐

  • exec_command 详解_linux exec命令

    exec_command 详解_linux exec命令2D-Position允许通过拖曳移动绝对定位的对象。AbsolutePosition设定元素的position属性为“absolute”(绝对)。BackColor设置或获取当前选中区的背景颜色。BlockDirLTR目前尚未支持。BlockDirRTL目前尚未支持。Bold切换当前选中区的粗体显示与否。BrowseMode目前尚未支持。

    2025年6月29日
    0
  • 浅析Anycast技术[通俗易懂]

    浅析Anycast技术[通俗易懂]什么是AS号码AS号码即自治系统号码,是用来标识独立的自治系统的,在同一个自治系统内,使用相同内部路由协议,自治系统间使用外部路由协议(通常是BGP协议)。申请AS号码的单位需要与两家以上(包括两家)、有不同AS号码的网络接入商进行网络互联,并计划三个月内与他们同时运行BGP协议进行外部路由。什么是BGPAnyCast?BGPanycast就是利用一个(多个)as号码在不同的地区广播相同的一个ip段。利用bgp的寻路原则,短的aspath会选成最优路径(bgp寻路原则之n),从.

    2022年5月10日
    58
  • 关联关系和依赖关系的区别[建议收藏]

    关联关系:A类实例化的时候需要B类的对象引用或指针作为参数依赖关系:A类的某个方法使用B类,可能是方法的参数是B类或在方法中获得了一个B类的实例某个类以成员变量的形式出现在另一个类中,二者是关联关

    2021年12月18日
    44
  • linux配置虚拟IP地址方法「建议收藏」

    linux配置虚拟IP地址方法「建议收藏」linux配置虚拟IP地址方法在日常linux管理工作中,需要为应用配置单独的IP地址,以达到主机与应用的分离,在应用切换与迁移过程中可以做到动态切换,特别是在使用HA的时候,这种方案可以保证主机与应用的隔离,对日常的运维有很大的益处.但在有些应用中还没有配置HA,后期需要配置HA时,我们可以先配置虚拟IP给在线的应用使用,这要后期的系统运维可以做到更好的可扩展性.本文主要是对IP地址

    2022年10月20日
    0
  • 分类模型评估方法_政策评估的模型与方法

    分类模型评估方法_政策评估的模型与方法上图矩阵中1是正例,0是只负例对于二分类问题,可以将真实类别和分类器预测类别组合为:真正例(truepositiveTP):真实类别为正例,预测类别为正例;假正例(falsepositiveFP):真实类别为负例,预测类别为正例;假负例(falsenegativeFN):真实类别为正例,预测类别为负例;真负例(truenegativeTN):真实类别为负例,预测类别为负例;分类模…

    2022年9月27日
    0
  • 羊车门问题

    羊车门问题

    2021年5月27日
    90

发表回复

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

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