在Lucene
3.0中,删除了next()方法。现在,您应该使用增量令牌遍历令牌,并且在到达输入流的末尾时将返回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++; }}...


