免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
基于Lucene3.5.0如何從TokenStream獲得Token

基于Lucene3.5.0如何從TokenStream獲得Token

分類: 編程練習 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)
  • 以上信息可以知道,原來的通過的方法已經(jīng)不能夠提取響應(yīng)的Token了
    1. StringReader reader = new StringReader(s);  
    2. TokenStream ts =analyzer.tokenStream(s, reader);  
    3. TermAttribute ta = ts.getAttribute(TermAttribute.class);  

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


    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.   
  • 本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
    打開APP,閱讀全文并永久保存 查看更多類似文章
    猜你喜歡
    類似文章
    翻譯:如何提高和優(yōu)化Lucene索引速度
    Lucene3.0正式發(fā)布了
    開源中文分詞框架分詞效果對比smartcn與IKanalyzer
    重溫 Lucene--yysun 的.NET 筆記
    strtok函數(shù)的使用
    strstr和strtok函數(shù)
    更多類似文章 >>
    生活服務(wù)
    分享 收藏 導長圖 關(guān)注 下載文章
    綁定賬號成功
    后續(xù)可登錄賬號暢享VIP特權(quán)!
    如果VIP功能使用有故障,
    可點擊這里聯(lián)系客服!

    聯(lián)系客服