分類: 編程練習(xí) 2012-02-01 17:36 268人閱讀 評(píng)論(0) 收藏 舉報(bào)
通過學(xué)習(xí)Lucene3.5.0的doc文檔,對(duì)不同release版本 lucene版本的API改動(dòng)做分析。最后找到了有價(jià)值的改動(dòng)信息。 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)
以上信息可以知道,原來的通過的方法已經(jīng)不能夠提取響應(yīng)的Token了 [java] view plaincopy?
StringReaderreader=newStringReader(s);
TokenStreamts=analyzer.tokenStream(s,reader);
TermAttributeta=ts.getAttribute(TermAttribute.class);
通過分析Api文檔信息 可知,CharTermAttribute已經(jīng)成為替換TermAttribute的接口

因此我編寫了一個(gè)例子來更好的從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="我是俊杰,我愛編程,我的測(cè)試用例";
Segments=newSegment();
Stringtest="";
try{
System.out.println(test+s.segment(name));
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
愛華網(wǎng)



