当前位置: 代码迷 >> J2EE >> lucene 获取分词后的关键字,该如何解决
  详细解决方案

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

热度:83   发布时间:2016-04-22 01:47:31.0
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();
  相关解决方案