JAVA中文切词
程序员文章站
2022-04-03 14:51:46
...
因项目需要对中文进行切词,故找同事要了段代码,现记录下来,以便日后使用
public static String detailData(String text) throws IOException{
String returnStr = "";
//创建分词对象
Analyzer anal = new IKAnalyzer(true);
StringReader reader = new StringReader(text);
//分词
TokenStream ts = anal.tokenStream("", reader);
CharTermAttribute term = ts.getAttribute(CharTermAttribute.class);
while(ts.incrementToken()){
returnStr = returnStr + term.toString()+"#@@#";
}
reader.close();
return returnStr;
}
另:附件1和2放在lib中,附件3放在src根目录
public static String detailData(String text) throws IOException{
String returnStr = "";
//创建分词对象
Analyzer anal = new IKAnalyzer(true);
StringReader reader = new StringReader(text);
//分词
TokenStream ts = anal.tokenStream("", reader);
CharTermAttribute term = ts.getAttribute(CharTermAttribute.class);
while(ts.incrementToken()){
returnStr = returnStr + term.toString()+"#@@#";
}
reader.close();
return returnStr;
}
另:附件1和2放在lib中,附件3放在src根目录