下面代码可以输出关键字, 但是方法已经过期了, 现在用什么代替?
用的是3.5
- Java code
@Testpublic void test() throws Exception{ analyze(new StandardAnalyzer(Version.LUCENE_35), "What's your a name?");}private void analyze(Analyzer analyzer, String text) throws Exception { TokenStream ts = analyzer.reusableTokenStream("content", new StringReader(text)); while (ts.incrementToken()) { Token t = ts.get TermAttribute ta = ts.getAttribute(TermAttribute.class); System.out.println(ta.term()); } }
------解决方案--------------------
OffsetAttribute offsetAttr = tokens.getAttribute(OffsetAttribute.class);
CharTermAttribute charTermAttr = tokens.getAttribute(CharTermAttribute.class);
PositionIncrementAttribute posIncrAttr = tokens.addAttribute(PositionIncrementAttribute.class);
TypeAttribute typeAttr = tokens.addAttribute(TypeAttribute.class);
while (tokens.incrementToken()) {
charBuf = charTermAttr.buffer();
termIncr = posIncrAttr.getPositionIncrement();
String term = new String(charBuf, 0, offsetAttr.endOffset() - offsetAttr.startOffset());
System.out.println(term );
}
tokens.close();