java 正则去除中文标点符号

来源:互联网 发布:php reflection的作用 编辑:程序博客网 时间:2024/06/03 09:26

在作文本分析,尤其是分词的时候,我们需要把不需要的标点符号去除,防止在词转向量的时候,把中文符号添加进去。

"[\\pP+~$`^=|<>~`$^+=|<>¥×]""[\\p{P}+~$`^=|<>~`$^+=|<>¥×]"

我使用的是ansj分词器,其5.x版本有提供Recognition接口,用于在分词的时候剔除不需要的,比如剔除停用词、标点符号,根据词性剔除一批词,也支持正则表达式,很强大。
我只写了demo:

JSONObject jsonObject = JSONObject.parseObject(ss);                String content = jsonObject.getString("content");                FilterRecognition filterRecognition = new FilterRecognition();                filterRecognition.insertStopNatures("m","w");                filterRecognition.insertStopRegex("[\\pP+~$`^=|<>~`$^+=|<>¥×]");                filterRecognition.insertStopRegex("•");                Result parse = NlpAnalysis.parse(content.replaceAll("\\s+","")).recognition(filterRecognition);                Set<String> str = new HashSet<String>();
原创粉丝点击