1.Python 接口
得益于 Python 简洁的设计,使用这门动态语言调用 HanLP 会省下不少时间,因此较为推荐这样安装。
HanLP 的 Python 接口由 pyhanlp 包提供,其安装只需一句命令:
pip install pyhanlp
这个包依赖 Java 和 JPype。Windows 用户如果遇到如下错误:
building '_jpype' extensionerror: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools":http://visualstudio.microsoft.com/visual-cpp-build-tools/
可以按提示安装 Visual C++,也可以通过安装更轻量级的Miniconda或是 Anaconda来解决这个问题。
然后执行如下命令:
conda install -c conda-forge jpype1
pip install pyhanlp
如果遇到 Java 相关的问题:
jpype._jvmfinder.JVMNotFoundException: No JVM shared library file (jvm.dll) found. Try setting up the JAVA_HOME environment variable properly.
请安装 Java 运行环境。HanLP 主项目采用 Java 开发,所以需要 JDK 或 JRE。
2.检测安装与配置
一切顺利的话,在命令行中键入如下命令,可以验证安装结果并自动配置。
hanlp
如若发生错误,则需要手动配置,可根据实际提示从github:https://github.com/hankcs/pyhanlp上自行下载安装包【data-for-1.7.5.zip与hanlp-1.7.5-release.zip】,并将这两个压缩包放置在E:\Anaconda3\Lib\site-packages\pyhanlp\static之下,并解压data,而后再输入hanlp,即可安装成功。
3. 手动配置
来源:https://github.com/hankcs/pyhanlp/wiki/手动配置
4. 进入交互分词模式测试
使用命令`hanlp segment`进入交互分词模式,输入一个句子并回车,HanLP会输出分词结果: