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


相关推荐

  • 误删Oracle中数据快速恢复方式

    误删Oracle中数据快速恢复方式在开发或者维护过程存在需要对Oracle数据的恢复工作,下面提供几种方式:1.数据查询(20分钟前)select*fromTABLE_NAMEasoftimestampsysdate-20/1440;2.更新数据恢复2.1清空当前表,恢复20分钟前数据(不推荐)DELETETABLE_NAMEwhere1=1;COMMIT;inserti…

    2022年7月17日
    13
  • 编程必备,程序员应该都知道的7款文本编辑器

    编程必备,程序员应该都知道的7款文本编辑器

    2022年3月2日
    50
  • 苹果开发者账号申请流程_最新苹果开发者申请

    苹果开发者账号申请流程_最新苹果开发者申请苹果开发者帐号申请流程

    2022年4月21日
    44
  • JS大数运算_大数阶乘运算

    JS大数运算_大数阶乘运算/***大数与或操作*/global.andorOpera=function(a,b,type){//type=1是与运算,type=2是或运算,默认是与运算a=a+””;b=b+””;varaStr=toBin(a);varbStr=toBi

    2022年10月7日
    1
  • WinServer2012配置AD域

    WinServer2012配置AD域WinServer2012配置AD域一、修改域控主机网卡IP,配置为静态IP,DNS配置为本机,并修改计算机名修改计算机名后需要重启注:若发现域控主机外部无法ping通请注意修改防火墙配置二、安装AD服务,将主机作为域控主机点击添加功能后,单击下一步随后添加功能组件,根据需求添加,点击下一步等待安装完成安装完成点击关闭安装完成后,点击上方的通知选择将此服务器提升为域控制器选择添加新林,设置好域名后点击下一步设置域还原密码,用于当域主机出现故障

    2022年5月16日
    41
  • h5页面 请在微信客户端打开链接_模拟微信接口时,提示“请在微信客户端打开链接”(转)…[通俗易懂]

    h5页面 请在微信客户端打开链接_模拟微信接口时,提示“请在微信客户端打开链接”(转)…[通俗易懂]背景描述相信有模拟微信页面请求的测试都有看到过这个页面,简单点说就是爬虫爬微信页面,进行回放的时候会出现这个页面。大概在1年前,专门安排了一个人去解决这个技术问题,遗憾的是当时没有找到解决方案,接下来所有微信端的接口测试和性能测试都无法进行,今天和大家分享下我们的解决方案,希望大家可以绕过微信的坑。业务场景我这里以JMeter来举例,我们可以通过在JMeter上开启代理,手机上设置代理来录制微信端…

    2022年6月7日
    32

发表回复

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

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