2017-06-21 50 views
3

我沒有從我的Analyzer獲得預期結果,並且想要測試標記化過程。如何測試Lucene分析器?

回答這個問題:How to use a Lucene Analyzer to tokenize a String?

List<String> result = new ArrayList<String>(); 
TokenStream stream = analyzer.tokenStream(field, new StringReader(keywords)); 

try { 
    while(stream.incrementToken()) { 
     result.add(stream.getAttribute(TermAttribute.class).term()); 
    } 
} 
catch(IOException e) { 
    // not thrown b/c we're using a string reader... 
} 

return result; 

採用TermAttribute來提取數據流的標記。問題是TermAttribute不再在Lucene 6中。

它被什麼取代?

與Lucene 6.6.0相當的是什麼?

+0

爲[這個答案](HTTPS:/ /stackoverflow.com/a/2638252/981744)描述,TermAttribute被替換爲'CharTermAttribute'。我不是Lucene的專家,但是這是你從一個谷歌搜索得到的。 –

+1

謝謝@ErwinBolwidt,那個答案正是我在找的......我的lucene詞彙顯然是缺乏的。你能回答它嗎? – Martinffx

回答