在 Maven 项目中使用 HanLP

今天写 Java 程序的时候遇到了中文分词的需求,我找了一个基于 NLP 的中文分词工具,感觉挺好用的,分享一下。

想要更好的阅读体验,可以转我的个人博客

导入Maven库

pom.xml 中添加,这里我们使用最新的 1.7.8 版本:

<dependencies>
    <dependency>
        <groupId>com.hankcs</groupId>
        <artifactId>hanlp</artifactId>
        <version>portable-1.7.8</version>
    </dependency>
</dependencies>

下载 data 文件

根据最新版的 release note ,需要下载 data-for-1.7.5.zip,现在服务器应该放到国内了,我记得以前下得很慢的,都要在网盘里备份好多版。

解压后找到 data 文件夹,移动到 src/main/resources 中,移动完成之后的目录结构:

.
└── resources
    ├── Z01-Example.txt
    └── data
        ├── README.url
        ├── dictionary
        ├── model
        └── version.txt

data 文件包括 dictionarymodel 两部分, HanLP 的大部分库都会调用这些资源,HanLP 还支持自定义的词典,但不是这篇文章的重点。

下载配置文件

我不知道 HanLP 的开发者怎么想的,Mavenportable 版本还缺少一个非常重要的文件,hanlp.properties

我们需要下载 hanlp-1.7.8-release.zip,因为 hanlp.propertieshanlp-1.7.8-release.zip 里面。

把这个配置文件放到项目根目录中,和 pom.xmlsrc 同级。

.
├── hanlp.properties
├── pom.xml
├── src
└── target

因为前面,我们把 data 文件放到了 src/main/resources 里,所以还需要更改 hanlp.properties 中的 root 参数。

#Windows用户请注意,路径分隔符统一使用/
root=./src/main/resources

如果不想下载,这是配置改好之后的文件内容:

#本配置文件中的路径的根目录,根目录+其他路径=完整路径(支持相对路径,请参考:https://github.com/hankcs/HanLP/pull/254)
#Windows用户请注意,路径分隔符统一使用/
root=./src/main/resources

#好了,以上为唯一需要修改的部分,以下配置项按需反注释编辑。

#核心词典路径
#CoreDictionaryPath=data/dictionary/CoreNatureDictionary.txt
#2元语法词典路径
#BiGramDictionaryPath=data/dictionary/CoreNatureDictionary.ngram.txt
#自定义词典路径,用;隔开多个自定义词典,空格开头表示在同一个目录,使用“文件名 词性”形式则表示这个词典的词性默认是该词性。优先级递减。
#所有词典统一使用UTF-8编码,每一行代表一个单词,格式遵从[单词] [词性A] [A的频次] [词性B] [B的频次] ... 如果不填词性则表示采用词典的默认词性。
CustomDictionaryPath=data/dictionary/custom/CustomDictionary.txt; 现代汉语补充词库.txt; 全国地名大全.txt ns; 人名词典.txt; 机构名词典.txt; 上海地名.txt ns;data/dictionary/person/nrf.txt nrf;
#停用词词典路径
#CoreStopWordDictionaryPath=data/dictionary/stopwords.txt
#同义词词典路径
#CoreSynonymDictionaryDictionaryPath=data/dictionary/synonym/CoreSynonym.txt
#人名词典路径
#PersonDictionaryPath=data/dictionary/person/nr.txt
#人名词典转移矩阵路径
#PersonDictionaryTrPath=data/dictionary/person/nr.tr.txt
#繁简词典根目录
#tcDictionaryRoot=data/dictionary/tc
#HMM分词模型
#HMMSegmentModelPath=data/model/segment/HMMSegmentModel.bin
#分词结果是否展示词性
#ShowTermNature=true
#IO适配器,实现com.hankcs.hanlp.corpus.io.IIOAdapter接口以在不同的平台(Hadoop、Redis等)上运行HanLP
#默认的IO适配器如下,该适配器是基于普通文件系统的。
#IOAdapter=com.hankcs.hanlp.corpus.io.FileIOAdapter
#感知机词法分析器
#PerceptronCWSModelPath=data/model/perceptron/pku1998/cws.bin
#PerceptronPOSModelPath=data/model/perceptron/pku1998/pos.bin
#PerceptronNERModelPath=data/model/perceptron/pku1998/ner.bin
#CRF词法分析器
#CRFCWSModelPath=data/model/crf/pku199801/cws.txt
#CRFPOSModelPath=data/model/crf/pku199801/pos.txt
#CRFNERModelPath=data/model/crf/pku199801/ner.txt
#更多配置项请参考 https://github.com/hankcs/HanLP/blob/master/src/main/java/com/hankcs/hanlp/HanLP.java#L59 自行添加

测试

先在 src/main/resources 下面创建一个 testdata.txt,里面随便放点文章。

主类的代码:

import java.io.*;

import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.tokenizer.NLPTokenizer;

import java.util.List;

public class Main{
    private String text;

    public static void main(String[] args) {
        String fileName = Main.class.getResource("testdata.txt").getFile();
        File file = new File(fileName);
        BufferedReader br;
        StringBuffer sb = new StringBuffer();
        try {
            br = new BufferedReader(new FileReader(file));
            while (br.ready()) {
                sb.append(br.readLine().concat("\n"));
            }
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        final String text = sb.toString();
        List<Term> terms = NLPTokenizer.segment(text);
        System.out.println(terms);
    }
}
  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

真实的hello world

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值