2015-02-05 46 views
0

我想調試Lucene令牌過濾器並查看結果。如何將令牌過濾器應用於令牌流以查看結果? (使用Lucene 4.10.3Lucene - 將TokenFilter應用於字符串並獲得結果

import java.io.IOException; 
import java.io.StringReader; 
import java.util.Iterator; 

import org.apache.lucene.analysis.core.LowerCaseFilter; 
import org.apache.lucene.analysis.standard.StandardTokenizer; 
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; 

public class TokenFilterExample { 
    public static void main(String[] args) throws IOException { 
     // 1] Create token stream 
     StringReader r = new StringReader("Hello World"); 
     StandardTokenizer s = new StandardTokenizer(r); 

     // Create lower-case token filter 
     LowerCaseFilter f = new LowerCaseFilter(s); 

     // Print result 
     System.out.println(??????); 

     // close 
     f.close(); 
     s.close(); 
    } 
} 

回答

0

解決的辦法是這樣的:

// Print result 
    f.reset(); 
    Iterator it = f.getAttributeClassesIterator(); 
    while (f.incrementToken()) { 
     System.out.println(f 
       .getAttribute(CharTermAttribute.class)); 
    } 
相關問題