本文共 1850 字,大约阅读时间需要 6 分钟。
目录
第一步:先将中文文本进行分词,这里使用的HanLP-汉语言处理包进行中文文本分词。
第二步:使用停用词表,去除分好的词中的停用词。
使用的HanLP-汉语言处理包进行中文文本分词。
官方环境配置网址如下:
停用词表可以去百度搜索,这是我搜到的一个:
我将它放在了百度云里面。
停用词.txt : 链接: 提取码:8uq1
使用这个工具类的之前,请先完成中文文本分词环境配置,并测试一下。停用词 .txt 文件路径请修改为自己的本地路径。
public class FormatUtil { /** * 去除停用词 * @param oldString:原中文文本 * @return 去除停用词之后的中文文本 * @throws IOException */ public static String RemovalOfStopWords(String oldString) throws IOException { String newString = oldString; // 分词 ListtermList = HanLP.segment(newString); System.out.println(termList); // 中文 停用词 .txt 文件路径 String filePath = "F:\\主文件夹\\知识图谱\\工具资源\\停用词.txt"; File file = new File(filePath); BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); List stopWords = new ArrayList<>(); String temp = null; while ((temp = bufferedReader.readLine()) != null) { //System.out.println("*" + temp+ "*"); stopWords.add(temp.trim()); } List termStringList = new ArrayList<>(); for(Term term:termList) { termStringList.add(term.word); //System.out.println("*" + term.word + "*"); } termStringList.removeAll(stopWords); newString = ""; for (String string:termStringList) { newString += string; } return newString; }}
5.1 测试代码
public class test { public static void main(String args[]) { try { System.out.println(FormatUtil.RemovalOfStopWords("床前明月光,疑是地上霜。举头望明月,低头思故乡。")); } catch (IOException e) { e.printStackTrace(); } }}
5.2 测试结果
END。
转载地址:http://epkzz.baihongyu.com/