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


相关推荐

  • OPC服务器比较

    OPC服务器比较目前支持OPC服务器的组态软件有很多种,其中四种软件即:Intellution公司的iFIX(3.5)、GE公司的Cimplicity(6.0)、Wonderware公司的InTouch(9.5)以及Siemens公司的WinCC(6.0)应用最广、功能最强。Intellution公司和Wonderware公司是专门从事监控软件工作的,在市场占领绝大部分份额;Cimplicity和WinCC是GE

    2022年6月20日
    44
  • html 图像处理 灰度图和浮雕图类PS

    html 图像处理 灰度图和浮雕图类PS浮雕图,灰度图,用html5canvas处理,类PS风格

    2022年6月20日
    27
  • leetcode-19删除链表的倒数第 N 个结点

    leetcode-19删除链表的倒数第 N 个结点原题链接给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。进阶:你能尝试使用一趟扫描实现吗?示例 1:输入:head = [1,2,3,4,5], n = 2输出:[1,2,3,5]示例 2:输入:head = [1], n = 1输出:[]示例 3:输入:head = [1,2], n = 1输出:[1]提示:链表中结点的数目为 sz1 <= sz <= 300 <= Node.val <= 1001 <= n <= s

    2022年8月9日
    4
  • 用HTML5做一个个人网站,此文仅展示个人主页界面。内附源代码下载地址

    html5,用css去修饰自己的个人主页代码如下:&lt;!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;&lt;htmlxmlns="http://www.w3.org/1999/xh…

    2022年4月9日
    69
  • JVM调优经验分享

    JVM调优经验分享JVM调优经验分享前言一、JVM调优知识背景简介二、JVM调优参数简介三、JVM调优目标四、JVM调优经验结束语

    2022年5月31日
    39
  • SHFileOperation使用

    SHFileOperation使用总结一下SHFileOperation的用法,希望对大家有用//删除文件或者文件夹boolDeleteFile(char*lpszPath){SHFILEOPSTRUCTFileOp={0};FileOp.fFlags=FOF_ALLOWUNDO|//允许放回回收站FOF_NOCONFIRMATION;//不出现确认对话框…

    2022年7月18日
    14

发表回复

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

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