/**
* 获取分词结果
* @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);
}