关于分词器的比较和选择,可以看这篇文章:Lucene的各中文分词比较
doToken()
方法可以对传入的字符串进行分词
/***
*
* @param ts 需要拆词的字符串
* @return
* @throws IOException
*/
public static List<String> doToken(TokenStream ts) throws IOException {
List<String> stringList = new ArrayList<>();
ts.reset();
CharTermAttribute cta = ts.getAttribute(CharTermAttribute.class);
while (ts.incrementToken()) {
stringList.add(cta.toString());
}
System.out.println();
ts.end();
ts.close();
return stringList;
}