基于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();
    		}
    	}
    
    }

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

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

    赞 (0)
    全栈程序员-站长的头像全栈程序员-站长


    相关推荐

    • 装水的容器大全_盛水的容器什么最好

      装水的容器大全_盛水的容器什么最好原题链接给你 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。说明:你不能倾斜容器。示例 1:输入:[1,8,6,2,5,4,8,3,7]输出:49解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。示例 2:输入:he

      2022年8月8日
    • golang ipc 通信_golang 2.0

      golang ipc 通信_golang 2.0NTP(NetworkTimeProtocol,网络时间协议)是由RFC1305定义的时间同步协议,用来在分布式时间服务器和客户端之间进行时间同步。NTP基于UDP报文进行传输,使用的UDP端口号为123。使用NTP的目的是对网络内所有具有时钟的设备进行时钟同步,使网络内所有设备的时钟保持一致,从而使设备能够提供基于统一时间的多种应用。对于运行NTP的本地系统,既可以接收来自其他时钟…

      2022年10月12日
    • 基于Vue的电商后台管理系统(2)

      基于Vue的电商后台管理系统(2)电商后台管理系统2前言上期已经实现该系统的登陆界面、路由、登录、退出及导航守卫功能,本期将继续完善该系统的以下功能:从后端获取后台列表数据并渲染到前端页面、用户列表的展示、修改、删除和添加。后台页面布局本系统后台将采用ElementUI中的Container布局容器。基本结构如下:代码框架:<el-container><el-header>Header</el-header><el-container><el-a

      2022年6月11日
    • python类型转换astype时间_python dataframe astype 字段类型转换方法

      python类型转换astype时间_python dataframe astype 字段类型转换方法使用astype实现dataframe字段类型转换#-*-coding:UTF-8-*-importpandasaspddf=pd.DataFrame([{‘col1′:’a’,’col2′:’1′},{‘col1′:’b’,’col2′:’2′}])printdf.dtypesdf[‘col2’]=df[‘col2’].astype(‘int’)print’–…

      2022年6月5日
    • react 路由权限守卫[通俗易懂]

      react 路由权限守卫[通俗易懂]app.js//路由import{HashRouterasRouter,Route,NavLink,Redirect,Switch,useHistory}from’react-router-dom’//导入哈希路由(BrowserRouter浏览器路由)起别名叫Router//Route路由页面,NavLink路由导航页面importReact,{Component}from’react’;import”./App.css”;classApp.

      2022年5月5日
    • Juniper发展史

      Juniper发展史

      2022年3月11日

    发表回复

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

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