Lucene查看分词结果

来源:转载


/** * 获取分词结果 * @param 输入的字符串 * @param 分词器 * @return 分词结果 */public static List<String> getWords(String str,Analyzer analyzer){ List<String> result = new ArrayList<String>(); TokenStream stream = null; try { stream = analyzer.tokenStream("content", new StringReader(str)); CharTermAttribute attr = stream.addAttribute(CharTermAttribute.class); stream.reset(); while(stream.incrementToken()){ result.add(attr.toString()); } } catch (IOException e) { e.printStackTrace(); }finally{ if(stream != null){ try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } return result;}


调用方式:

String str = "欢迎光临xsi64的博客。";List<String> lists = AnalyzerUtil.getWords(str, new IKAnalyzer());for (String s : lists) { System.out.println(s);}




分享给朋友:
您可能感兴趣的文章:
随机阅读: