java-hanlp中文语言处理

hanlp是一款开源的中文语言处理工具。

环境:jdk1.7、myeclipse8.5、win64

官网:http://hanlp.linrunsoft.com/
git下载使用说明地址:https://github.com/hankcs/HanLP
在线演示地址:http://hanlp.com/?sentence=http://hanlp.com/
百度云链接: https://pan.baidu.com/s/1kBJH1PAE4-S_Mfn_igp4Vw

使用步骤

1.官网下载本地词库
2.下载jar包与配置文件
3.新建工程导入jar,配置文件
4.修改配置文件 root=D:/datacjy/hanlp 为本地下载好的词库
5.开始使用

上面给的百度云链接是本人下载的目前官网最新的1.6.8版本,里面有词库,jar包与配置文件、官网demo工程,本人测试工程

工程解析:
1.com.hankcs包下是官网demo中的 test文件夹下的代码,工程完善可直接运行
2.hanlp.properties 是配置文件需要修改下载后的本地词库位置
3.lib下是jar包,源码包

配置文件修改:
配置文件的作用是告诉HanLP数据包的位置,只需修改第一行
root=D:/datacjy/hanlp
为data的父目录即可,比如data目录是/Users/hankcs/Documents/data,那么root=/Users/hankcs/Documents/ 。

测试

package com.hankcs.demo;

import com.hankcs.hanlp.HanLP;

/**

  • 第一个Demo,惊鸿一瞥
  • @author hankcs
    */
    public class DemoAtFirstSight
    {
    public static void main(String[] args)
    {
    System.out.println("首次编译运行时,HanLP会自动构建词典缓存,请稍候……");
    // HanLP.Config.enableDebug(); // 为了避免你等得无聊,开启调试模式说点什么:-)
    System.out.println(HanLP.segment("你好,欢迎使用HanLP汉语处理包!接下来请从其他Demo中体验HanLP丰富的功能~"));
    }
    }
    /*
    首次编译运行时,HanLP会自动构建词典缓存,请稍候……
    [你好/vl, ,/w, 欢迎/v, 使用/v, HanLP/nx, 汉语/gi, 处理/vn, 包/v, !/w, 接下来/vl, 请/v, 从/p, 其他/rzv, Demo/nx, 中/f, 体验/v, HanLP/nx, 丰富/a, 的/ude1, 功能/n, ~/nx]
    /

演示用户词典的动态增删

package com.hankcs.demo;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.collection.AhoCorasick.AhoCorasickDoubleArrayTrie;
import com.hankcs.hanlp.dictionary.BaseSearcher;
import com.hankcs.hanlp.dictionary.CoreDictionary;
import com.hankcs.hanlp.dictionary.CustomDictionary;

import java.util.Map;

/**

  • 演示用户词典的动态增删
  • @author hankcs
    */
    public class DemoCustomDictionary
    {
    public static void main(String[] args)
    {
    // 动态增加
    CustomDictionary.add("攻城狮");
    // 强行插入
    CustomDictionary.insert("白富美", "nz 1024");
    // 删除词语(注释掉试试)
    // CustomDictionary.remove("攻城狮");
    System.out.println(CustomDictionary.add("单身狗", "nz 1024 n 1"));
    // System.out.println(CustomDictionary.get("单身狗"));

    String text = "攻城狮逆袭单身狗,迎娶白富美,走上人生巅峰";  // 怎么可能噗哈哈!
    
    // DoubleArrayTrie分词
    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);
    }
    });
    // 首字哈希之后二分的trie树分词
    BaseSearcher searcher = CustomDictionary.getSearcher(text);
    Map.Entry entry;
    while ((entry = searcher.next()) != null)
    {
    // System.out.println(entry);
    }

    // 标准分词
    System.out.println(HanLP.segment(text));
    
    // Note:动态增删不会影响词典文件
    // 目前CustomDictionary使用DAT储存词典文件中的词语,用BinTrie储存动态加入的词语,前者性能高,后者性能低
    // 之所以保留动态增删功能,一方面是历史遗留特性,另一方面是调试用;未来可能会去掉动态增删特性。

    }
    }

//true
//[攻城狮/nz, 逆袭/nz, 单身狗/nz, ,/w, 迎娶/v, 白富美/nz, ,/w, 走上/v, 人生/n, 巅峰/n]
//自定义词:攻城狮、单身狗、白富美

注意:修改配置文件本地词库位置

文章来源于风zi的博客

转载于:https://blog.51cto.com/13993767/2333343

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值