博客
关于我
Java 实现去除中文文本的停用词
阅读量:411 次
发布时间:2019-03-05

本文共 1850 字,大约阅读时间需要 6 分钟。

目录


1.  整体思路

第一步:先将中文文本进行分词,这里使用的HanLP-汉语言处理包进行中文文本分词。

第二步:使用停用词表,去除分好的词中的停用词。

2.  中文文本分词环境配置

使用的HanLP-汉语言处理包进行中文文本分词。

  • HanLP-汉语言处理包官网如下:
  • HanLP 的环境配置有两种方式:方式一、Maven;方式二、下载jar、data、hanlp.properties。

官方环境配置网址如下:

  • 环境配置好后,java使用HanLP进行中文分词文档如下:

3.  下载停用词表

停用词表可以去百度搜索,这是我搜到的一个:

我将它放在了百度云里面。

停用词.txt : 链接:  提取码:8uq1 

4.  去除停用词工具类

使用这个工具类的之前,请先完成中文文本分词环境配置,并测试一下。停用词 .txt 文件路径请修改为自己的本地路径

public class FormatUtil {    /**     * 去除停用词     * @param oldString:原中文文本     * @return 去除停用词之后的中文文本     * @throws IOException     */    public static String RemovalOfStopWords(String oldString) throws IOException {        String newString = oldString;        // 分词        List
termList = 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.  工具类测试

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/

你可能感兴趣的文章
MySQL之2003-Can‘t connect to MySQL server on ‘localhost‘(10038)的解决办法
查看>>
MySQL之CRUD
查看>>
MySQL之DML
查看>>
Mysql之IN 和 Exists 用法
查看>>
MYSQL之REPLACE INTO和INSERT … ON DUPLICATE KEY UPDATE用法
查看>>
MySQL之SQL语句优化步骤
查看>>
MYSQL之union和order by分析([Err] 1221 - Incorrect usage of UNION and ORDER BY)
查看>>
Mysql之主从复制
查看>>