基於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了
[java] view plaincopy
  1. StringReader reader = new StringReader(s);  
  2. TokenStream ts =analyzer.tokenStream(s, reader);  
  3. TermAttribute ta = ts.getAttribute(TermAttribute.class);  

通過分析Api文檔信息 可知,CharTermAttribute已經成爲替換TermAttribute的接口因此我編寫了一個例子來更好的從TokenStream中提取Token

[html] view plaincopy
  1. package com.segment;  
  2.   
  3. import java.io.StringReader;  
  4. import org.apache.lucene.analysis.Analyzer;  
  5. import org.apache.lucene.analysis.Token;  
  6. import org.apache.lucene.analysis.TokenStream;  
  7. import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;  
  8. import org.apache.lucene.analysis.tokenattributes.TermAttribute;  
  9. import org.apache.lucene.util.AttributeImpl;  
  10. import org.wltea.analyzer.lucene.IKAnalyzer;  
  11.   
  12.   
  13. public class Segment {  
  14.     public static String show(Analyzer a, String s) throws Exception {  
  15.   
  16.         StringReader reader = new StringReader(s);  
  17.         TokenStream ts = a.tokenStream(s, reader);  
  18.         String s1 = ""s2 = "";  
  19.         boolean hasnextts.incrementToken();  
  20.         //Token t = ts.next();  
  21.         while (hasnext) {  
  22.             //AttributeImpl ta = new AttributeImpl();  
  23.             CharTermAttribute ta = ts.getAttribute(CharTermAttribute.class);  
  24.             //TermAttribute ta = ts.getAttribute(TermAttribute.class);  
  25.               
  26.             s2 = ta.toString() + " ";  
  27.             s1 += s2;  
  28.             hasnext = ts.incrementToken();  
  29.         }  
  30.         return s1;  
  31.     }  
  32.   
  33.     public String segment(String s) throws Exception {  
  34.         Analyzer a = new IKAnalyzer();  
  35.         return show(a, s);  
  36.     }  
  37.     public static void main(String args[])  
  38.     {  
  39.         String name = "我是俊傑,我愛編程,我的測試用例";  
  40.         Segment s = new Segment();  
  41.         String test = "";  
  42.         try {  
  43.             System.out.println(test+s.segment(name));  
  44.         } catch (Exception e) {  
  45.             // TODO Auto-generated catch block  
  46.             e.printStackTrace();  
  47.         }  
  48.     }  
  49.   
  50. }  

發佈了110 篇原創文章 · 獲贊 16 · 訪問量 41萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章