【HanLP】--自然语言处理场景应用

一、前言

HanLP 是由一系列模型与算法组成的工具包,主要功能包括分词、词性标注、关键词提取、自动摘要、依存句法分析、命名实体识别、短语提取、拼音转换、简繁转换等等。
下面将介绍HanLP如何本地集成及一些常用功能在项目的应用!

二、Springboot集成HanLP

HanLP需要一些数据集和配置【参考HanLP的GitHub项目】。准备data、hanlp.properties,放到/resources目录下。
在这里插入图片描述
引用jar包

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

----按照上面的配置好,就可以使用HanLP了。

三、HanLP分词

相关代码操作

  String[] wordStr = new String[]{
   
    
    "中华","华夏","中国","炎黄"};
        List<String> wordList = Arrays.asList(wordStr);
        wordList.forEach(item -> CustomDictionary.insert(item, "cus"));
        BinTrie<CoreDictionary.Attribute> trie =  CustomDictionary.getTrie();

 List<Term> termList1 = HanLP.segment("你好,欢迎使用HanLP汉语处理包!中华人.华夏无敌");
        System.out.println("1-->" + termList1);
        for (Term term : termList1) {
   
    
    
            String word = term.word;
            String nature = term.nature.toString();
            System.out.println("word-->" + word+"    nature--->"+nature);
        }

输出结果是:

1-->[你好/vl,/w, 欢迎/v, 使用/v, HanLP/nx, 汉语/gi, 处理/vn,/v,/w,/f, 华人/n, ./w, 华夏/cus, 无敌/vi]
word-->你好    nature--->

猜你喜欢

转载自blog.csdn.net/xunmengyou1990/article/details/131768960