基于Lucene3.5.0如何从TokenStream获得Token
http://blog.csdn.net/hiphopmattshi/article/details/7226326
通过学习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();
}
}
}
分享到:
相关推荐
最新版本的lucene,3.5.0版本。
lucene3.5源码。 方便开发人员对lucene进行扩展
Lucene Java Documentation This is the official documentation for Lucene Java Please use the menu on the left to access the Javadocs and different documents.
lucene3.5.0jar包,用到的可以下载,为了攥点积分也是费劲啊~天了个噜
java6se-spring3-archetype.zip,简单弹簧3原型基于弹簧3的简单原型
基于Lucene的搜索引擎的研究与应用基于Lucene的搜索引擎的研究与应用
lucene-3.5.0.chm文档,英文版
资源中包含lucene2.3API中文文档及lucene-3.5.0JAR包。
基于lucene技术的增量索引,实现索引的首次创建,动态增删改
基于Lucene的全文检索系统研究与开发基于Lucene的全文检索系统研究与开发基于Lucene的全文检索系统研究与开发
管理器来自 Lucene 3.5.0 的 NrtManager 和 SearcherManager 的 C# 端口
一种基于Lucene检索引擎的全文数据库的研究与实现一种基于Lucene检索引擎的全文数据库的研究与实现一种基于Lucene检索引擎的全文数据库的研究与实现
基于lucene的开发JavaEE项目 基于lucene的开发JavaEE项目 基于lucene的开发JavaEE项目 基于lucene的开发JavaEE项目 基于lucene的开发JavaEE项目
java代码,基于Lucene和mysql的简单的字符串匹配分词系统
毕业设计:基于lucene的桌面搜索引擎
基于LUCENE的搜索引擎的设计与实现源代码..
基于Lucene的一个财经类网页搜索引擎,对于Lucene爱好者与新手来说,是个很不错的东东哦,呵呵。
Lueene是一个强大的全文索引引擎工具包,应用它可以快速地开发一个搜索引擎。介绍了基于英特 网的中文搜索引擎的系统结构,Lucene的索引和搜索,并且设计...结果表明,基于Lucene的搜索引擎在索引和查找上的效率很高。