首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

lucene 获取分词后的关键字,该如何解决

2012-04-23 
lucene 获取分词后的关键字下面代码可以输出关键字, 但是方法已经过期了, 现在用什么代替?用的是3.5Java c

lucene 获取分词后的关键字
下面代码可以输出关键字, 但是方法已经过期了, 现在用什么代替?
用的是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();

热点排行