基于Lucene3.5.0怎样从TokenStream获得Token

基于Lucene3.5.0怎样从TokenStream获得Token

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

通过学习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了
    StringReader reader = new StringReader(s);
    TokenStream ts =analyzer.tokenStream(s, reader);
    TermAttribute ta = ts.getAttribute(TermAttribute.class);
  • 通过分析Api文档信息 可知,CharTermAttribute已经成为替换TermAttribute的接口
  • 因此我编写了一个样例来更好的从TokenStream中提取Token
  • package com.segment;
    
    import java.io.StringReader;
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.Token;
    import org.apache.lucene.analysis.TokenStream;
    import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
    import org.apache.lucene.analysis.tokenattributes.TermAttribute;
    import org.apache.lucene.util.AttributeImpl;
    import org.wltea.analyzer.lucene.IKAnalyzer;
    
    
    public class Segment {
    	public static String show(Analyzer a, String s) throws Exception {
    
    		StringReader reader = new StringReader(s);
    		TokenStream ts = a.tokenStream(s, reader);
    		String s1 = "", s2 = "";
    		boolean hasnext= ts.incrementToken();
    		//Token t = ts.next();
    		while (hasnext) {
    			//AttributeImpl ta = new AttributeImpl();
    			CharTermAttribute ta = ts.getAttribute(CharTermAttribute.class);
    			//TermAttribute ta = ts.getAttribute(TermAttribute.class);
    			
    			s2 = ta.toString() + " ";
    			s1 += s2;
    			hasnext = ts.incrementToken();
    		}
    		return s1;
    	}
    
    	public String segment(String s) throws Exception {
    		Analyzer a = new IKAnalyzer();
    		return show(a, s);
    	}
    	public static void main(String args[])
    	{
    		String name = "我是俊杰,我爱编程,我的測试用例";
    		Segment s = new Segment();
    		String test = "";
    		try {
    			System.out.println(test+s.segment(name));
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    }

  • 版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

    发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/118919.html原文链接:https://javaforall.cn

    【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

    【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

    (0)


    相关推荐

    • 8000401a错误解决方案(Excel)「建议收藏」

      8000401a错误解决方案(Excel)「建议收藏」前一阵子做开发需要用到Excel和Word编程,本人用的是Vista系统,开发环境是VS2005和Office2007,测试无任何问题,可是到部署的时候出现了一些令人很头痛的问题,老是会出现例如:检索COM类工厂中CLSID为{000209FF-0000-0000-C000-000000000046}的组件时失败,原因是出现以下错误:8000401a。的错误,在网上查询了许多资

    • 前端技术周刊 2018-06-09:网络协议栈[通俗易懂]

      前端技术周刊 2018-06-09:网络协议栈

    • 电脑对于目标文件系统过大_提示文件过大

      电脑对于目标文件系统过大_提示文件过大Win10系统提示对于目标文件系统过大今天在复制MAC系统文件时,系统弹出窗口提示“对于目标文件系统,文件XXX过大”。出现这种情况的原因是FAT32的文件系统不支持复制大于4g的单个文件,而NTF

    • java异常处理之throw, throws,try和catch[通俗易懂]

      java异常处理之throw, throws,try和catch[通俗易懂]   程序运行过程中可能会出现异常情况,比如被0除、对负数计算平方根等,还有可能会出现致命的错误,比如内存不足,磁盘损坏无法读取文件等,对于异常和错误情况的处理,统称为异常处理。   Java异常处理主要通过5个关键字控制:try、catch、throw、throws和finally。try的意思是试试它所包含的代码段中是否会发生异常;而catch当有异常时抓住它,并进行相应的处理,使程序不受

    • OA工作流的定义

      OA工作流的定义OA工作流就是建立于网络办公自动化基础上的事务行政审批,业务申请审批、公文、信息等的网上流转。OA工作流类别.OA工作流软件从不同的角度,有许多不同的分类方法,比如依据底层技术分类、依据任务传递机制分类、根据所实现的业务过程等,我们从用户角度出发,由于用户购买软件其最根本的出发点是改善管理转载于:https://www.cnblogs.com/jiang–nan/p/8664959.html…

    • ORA12154和TNS03505监听错误的解决方法「建议收藏」

      ORA12154和TNS03505监听错误的解决方法「建议收藏」原ORA-12154和TNS-03505监听错误的解决方法https://blog.csdn.net/tianlesoftware/article/details/5716028版权声明:https://blog.csdn.net/tianlesoftware/article/details/5716028之前在一台测试机上装了GridControl,今天在这台机器上添加了一个监听,…

    发表回复

    您的电子邮箱地址不会被公开。

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