SpringBoot集成自然语言处理hanlp工具包

HanLP是一系列模型与算法组成的NLP工具包,目标是普及自然语言处理在生产环境中的应用。

下载与配置

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

零配置,即可使用基本功能(除由字构词、依存句法分析外的全部功能)

如果用户有自定义的需求,使用hanlp.properties进行配置(Portable版同样支持hanlp.properties)
下载jar、data、hanlp.properties

①、下载:data.zip
下载后解压到任意目录,接下来通过配置文件告诉HanLP数据包的位置。
HanLP中的数据分为词典和模型,其中词典是词法分析必需的,模型是句法分析必需的

data

├─dictionary
└─model

用户可以自行增删替换,如果不需要句法分析等功能的话,随时可以删除model文件夹。

  • 模型跟词典没有绝对的区别,隐马模型被做成人人都可以编辑的词典形式,不代表它不是模型。
  • GitHub代码库中已经包含了data.zip中的词典,直接编译运行自动缓存即可;模型则需要额外下载

②、下载jar和配置文件:hanlp-release.zip
配置文件的作用是告诉HanLP数据包的位置,只需修改第一行
root=D:/JavaProjects/HanLP/
为data的父目录即可,比如data目录是/Users/hankcs/Documents/data,那么root=/Users/hankcs/Documents/ 。

最后将hanlp.properties放入classpath即可,对于多数项目,都可以放到src或resources目录下,编译时IDE会自动将其复制到classpath中。除了配置文件外,还可以使用环境变量HANLP_ROOT来设置root。安卓项目请参考demo。

如果放置不当,HanLP会提示当前环境下的合适路径,并且尝试从项目根目录读取数据集。

一、依赖(这里采用方式一的集成)


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hanlp-demo</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>com.hankcs</groupId>
            <artifactId>hanlp</artifactId>
            <version>portable-1.8.4</version>
        </dependency>


    </dependencies>
</project>

二、配置文件和启动类

server:
  port: 8088
@SpringBootApplication
public class DemoApplication {

   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

三、测试

/**
 * 演示用户词典的动态增删
 *
 * @author hankcs
 */
public class DemoCustomDictionary
{
    public static void main(String[] args)
    {
        // 动态增加
        CustomDictionary.add("攻城狮");
       // CustomDictionary.add("小金保");
        // 强行插入
        CustomDictionary.insert("白富美", "nz 1024");

        // 删除词语(注释掉试试)
//        CustomDictionary.remove("攻城狮");
        System.out.println(CustomDictionary.add("单身狗", "nz 1024 n 1"));
        System.out.println(CustomDictionary.get("单身狗"));


        String text = "攻城狮逆袭单身狗,迎娶白富美,走上人生巅峰,小金保值得你需要";  // 怎么可能噗哈哈!

        // AhoCorasickDoubleArrayTrie自动机扫描文本中出现的自定义词语
        final char[] charArray = text.toCharArray();
        CustomDictionary.parseText(charArray, new AhoCorasickDoubleArrayTrie.IHit<CoreDictionary.Attribute>()
        {
            @Override
            public void hit(int begin, int end, CoreDictionary.Attribute value)
            {
                System.out.printf("[%d:%d]=%s %s\n", begin, end, new String(charArray, begin, end - begin), value);
            }
        });
        System.out.println("########################################");
        // 自定义词典在所有分词器中都有效
        System.out.println(HanLP.segment(text));
    }
}

效果如下:

true
nz 1024 n 1
[0:3]=攻城狮 nz 1
[5:8]=单身狗 nz 1024 n 1
[11:14]=白富美 nz 1024
[0:2]=攻城 vi 15
[3:5]=逆袭 nz 199
########################################
[攻城狮/nz, 逆袭/nz, 单身狗/nz, ,/w, 迎娶/v, 白富美/nz, ,/w, 走/v, 上/f, 人生/n, 巅峰/n]

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值