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


相关推荐

  • 电脑显示器尺寸对照表_显示器选购攻略

    显示器是属于电脑的I/O设备,即输入输出设备。它可以分为CRT、LCD等多种。它是一种将一定的电子文件通过特定的传输设备显示到屏幕上再反射到人眼的显示工具。当用电脑来放松娱乐时,一个好的显示器则是必不可少的,看VCD时画面稳定;玩游戏时现场逼真,有一种身临其境的感觉,那种感觉一定特棒,这一切都取决于你选择的显示器品质的高低,对显示器的知识有一个综合的了解无疑会对你有所帮助,下面将就这一问…

    2022年4月4日
  • leetcode 三数之和_leetcode数组交集

    leetcode 三数之和_leetcode数组交集原题链接给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。注意:答案中不可以包含重复的三元组。示例 1:输入:nums = [-1,0,1,2,-1,-4]输出:[[-1,-1,2],[-1,0,1]]示例 2:输入:nums = []输出:[]示例 3:输入:nums = [0]输出:[] 提示:0 <= nums.length <= 300

    2022年8月8日
  • RFFE中MIPI协议

    RFFE中MIPI协议移动产业处理器接口(MobileIndustryProcessorInterface简称MIPI)联盟。MIPI(MobileIndustryProcessorInterface)协议是MIPI联盟(MIPIAlliance)提出的用于标准化移动终端系统各器件间通信的通信协议。MIPI联盟2003年成立,最早的初衷是为了标准化显示接口。

    2022年6月11日
  • 本地连接redis_redis入门

    本地连接redis_redis入门通过前面的学习,相信读者对RedisSentinel有了一定的了解,本节将介绍应用方如何正确地连接RedisSentinel。有人会说这有什么难的,已经知道了主节点的ip地址和端口,用对应编程语言的客户端连接主节点不就可以了吗?但试想一下,如果这样使用客户端,客户端连接RedisSentinel和主从复制的Redis又有什么区别呢,如果主节点挂掉了,虽然RedisSentinel可以完成故障转移,但是客户端无法获取这个变化,那么使用RedisSentinel的意义就不大了,所以各个语言

    2022年9月15日
  • 【深度学习系列】卷积神经网络CNN原理详解(一)——基本原理

    【深度学习系列】卷积神经网络CNN原理详解(一)——基本原理上篇文章我们给出了用paddlepaddle来做手写数字识别的示例,并对网络结构进行到了调整,提高了识别的精度。有的同学表示不是很理解原理,为什么传统的机器学习算法,简单的神经网络(如多层感知机)都可

    2022年7月4日
  • 史上最简单的Hibernate入门简单介绍

    史上最简单的Hibernate入门简单介绍

    2021年12月7日

发表回复

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

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