分类: 编程练习 2012-02-01 17:36 268人阅读 评论(0) 收藏 举报
通过学习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了 [java] view plaincopy?
StringReaderreader=newStringReader(s);
TokenStreamts=analyzer.tokenStream(s,reader);
TermAttributeta=ts.getAttribute(TermAttribute.class);
通过分析Api文档信息 可知,CharTermAttribute已经成为替换TermAttribute的接口

因此我编写了一个例子来更好的从TokenStream中提取Token
[html] view plaincopy?
packagecom.segment;
importjava.io.StringReader;
importorg.apache.lucene.analysis.Analyzer;
importorg.apache.lucene.analysis.Token;
importorg.apache.lucene.analysis.TokenStream;
importorg.apache.lucene.analysis.tokenattributes.CharTermAttribute;
importorg.apache.lucene.analysis.tokenattributes.TermAttribute;
importorg.apache.lucene.util.AttributeImpl;
importorg.wltea.analyzer.lucene.IKAnalyzer;
publicclassSegment{
publicstaticStringshow(Analyzera,Strings)throwsException{
StringReaderreader=newStringReader(s);
TokenStreamts=a.tokenStream(s,reader);
Strings1="",s2="";
booleanhasnext=ts.incrementToken();
//Tokent=ts.next();
while(hasnext){
//AttributeImplta=newAttributeImpl();
CharTermAttributeta=ts.getAttribute(CharTermAttribute.class);
//TermAttributeta=ts.getAttribute(TermAttribute.class);
s2=ta.toString()+"";
s1+=s2;
hasnext=ts.incrementToken();
}
returns1;
}
publicStringsegment(Strings)throwsException{
Analyzera=newIKAnalyzer();
returnshow(a,s);
}
publicstaticvoidmain(Stringargs[])
{
Stringname="我是俊杰,我爱编程,我的测试用例";
Segments=newSegment();
Stringtest="";
try{
System.out.println(test+s.segment(name));
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}