当前位置:网站首页>基于Lucene3.5.0怎样从TokenStream获得Token
基于Lucene3.5.0怎样从TokenStream获得Token
2022-07-05 11:22:00 【全栈程序员站长】
通过学习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.cn/109513.html原文链接:https://javaforall.cn
边栏推荐
- Zcmu--1390: queue problem (1)
- Wechat nucleic acid detection appointment applet system graduation design completion (7) Interim inspection report
- 分类TAB商品流多目标排序模型的演进
- 基础篇——基础项目解析
- [Oracle] use DataGrid to connect to Oracle Database
- Advanced scaffold development
- Characteristics and electrical parameters of DDR4
- Evolution of multi-objective sorting model for classified tab commodity flow
- AUTOCAD——遮罩命令、如何使用CAD对图纸进行局部放大
- SLAM 01. Modeling of human recognition Environment & path
猜你喜欢
【Office】Excel中IF函数的8种用法
How to make full-color LED display more energy-saving and environmental protection
A mining of edu certificate station
OneForAll安装使用
comsol--三维图形随便画----回转
不要再说微服务可以解决一切问题了!
[JS] extract the scores in the string, calculate the average score after summarizing, compare with each score, and output
[advertising system] incremental training & feature access / feature elimination
Ziguang zhanrui's first 5g R17 IOT NTN satellite in the world has been measured on the Internet of things
Huawei equipment configures channel switching services without interruption
随机推荐
Scaffold development foundation
Data types ntext and varchar are incompatible in the not equal to operator - 95 small pang
Paradigm in database: first paradigm, second paradigm, third paradigm
shell脚本文件遍历 str转数组 字符串拼接
Harbor镜像仓库搭建
Data type
技术管理进阶——什么是管理者之体力、脑力、心力
COMSOL--三维图形的建立
Variables///
C language current savings account management system
About the use of Vray 5.2 (self research notes) (II)
An error is reported in the process of using gbase 8C database: 80000305, host IPS long to different cluster. How to solve it?
Oneforall installation and use
FFmpeg调用avformat_open_input时返回错误 -22(Invalid argument)
Question and answer 45: application of performance probe monitoring principle node JS probe
Summary of thread and thread synchronization under window
[Oracle] use DataGrid to connect to Oracle Database
The art of communication III: Listening between people
不要再说微服务可以解决一切问题了!
Detailed explanation of MATLAB cov function