/** * 获取分词结果 * @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; }
时间: 2024-10-10 00:54:13