在Lucene的3.0,next()方法被移除。現在您應該使用incrementToken來遍歷令牌,並且當您到達輸入流的末尾時它將返回false。要獲得每個令牌,您應該使用AttributeSource類的方法。根據您想要獲得的屬性(術語,類型,有效載荷等),您需要使用addAttribute方法將相應屬性的類類型添加到您的標記器。
以下部分代碼示例來自WikipediaTokenizer的測試類,如果您下載Lucene的源代碼,您可以找到它。
...
WikipediaTokenizer tf = new WikipediaTokenizer(new StringReader(test));
int count = 0;
int numItalics = 0;
int numBoldItalics = 0;
int numCategory = 0;
int numCitation = 0;
TermAttribute termAtt = tf.addAttribute(TermAttribute.class);
TypeAttribute typeAtt = tf.addAttribute(TypeAttribute.class);
while (tf.incrementToken()) {
String tokText = termAtt.term();
//System.out.println("Text: " + tokText + " Type: " + token.type());
String expectedType = (String) tcm.get(tokText);
assertTrue("expectedType is null and it shouldn't be for: " + tf.toString(), expectedType != null);
assertTrue(typeAtt.type() + " is not equal to " + expectedType + " for " + tf.toString(), typeAtt.type().equals(expectedType) == true);
count++;
if (typeAtt.type().equals(WikipediaTokenizer.ITALICS) == true){
numItalics++;
} else if (typeAtt.type().equals(WikipediaTokenizer.BOLD_ITALICS) == true){
numBoldItalics++;
} else if (typeAtt.type().equals(WikipediaTokenizer.CATEGORY) == true){
numCategory++;
}
else if (typeAtt.type().equals(WikipediaTokenizer.CITATION) == true){
numCitation++;
}
}
...
參見http://stackoverflow.com/questions/3916806/3916947#3916947 – 2010-10-15 12:52:26