hanlp是一款開源的中文語言處理工具。
環(huán)境:jdk1.7驳糯、myeclipse8.5、win64
官網(wǎng):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.官網(wǎng)下載本地詞庫
2.下載jar包與配置文件
3.新建工程導(dǎo)入jar伦仍,配置文件
4.修改配置文件 root=D:/datacjy/hanlp 為本地下載好的詞庫
5.開始使用
上面給的百度云鏈接是本人下載的目前官網(wǎng)最新的1.6.8版本结窘,里面有詞庫,jar包與配置文件充蓝、官網(wǎng)demo工程隧枫,本人測試工程
工程解析:
1.com.hankcs包下是官網(wǎng)demo中的 test文件夾下的代碼,工程完善可直接運(yùn)行
2.hanlp.properties 是配置文件需要修改下載后的本地詞庫位置
3.lib下是jar包谓苟,源碼包
配置文件修改:
配置文件的作用是告訴HanLP數(shù)據(jù)包的位置官脓,只需修改第一行
root=D:/datacjy/hanlp
為data的父目錄即可,比如data目錄是/Users/hankcs/Documents/data涝焙,那么root=/Users/hankcs/Documents/ 卑笨。
測試
package com.hankcs.demo;
import com.hankcs.hanlp.HanLP;
/**
* 第一個(gè)Demo,驚鴻一瞥
*
* @author hankcs
*/
public class DemoAtFirstSight
{
? ? public static void main(String[] args)
? ? {
? ? ? ? System.out.println("首次編譯運(yùn)行時(shí)仑撞,HanLP會(huì)自動(dòng)構(gòu)建詞典緩存赤兴,請(qǐng)稍候……");
//? ? ? ? HanLP.Config.enableDebug();? ? ? ? // 為了避免你等得無聊妖滔,開啟調(diào)試模式說點(diǎn)什么:-)
? ? ? ? System.out.println(HanLP.segment("你好,歡迎使用HanLP漢語處理包桶良!接下來請(qǐng)從其他Demo中體驗(yàn)HanLP豐富的功能~"));
? ? }
}
/**
首次編譯運(yùn)行時(shí)座舍,HanLP會(huì)自動(dòng)構(gòu)建詞典緩存,請(qǐng)稍候……
[你好/vl, 陨帆,/w, 歡迎/v, 使用/v, HanLP/nx, 漢語/gi, 處理/vn, 包/v, 曲秉!/w, 接下來/vl, 請(qǐng)/v, 從/p, 其他/rzv, Demo/nx, 中/f, 體驗(yàn)/v, HanLP/nx, 豐富/a, 的/ude1, 功能/n, ~/nx]
*/
演示用戶詞典的動(dòng)態(tài)增刪
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;
/**
* 演示用戶詞典的動(dòng)態(tài)增刪
*
* @author hankcs
*/
public class DemoCustomDictionary
{
? ? public static void main(String[] args)
? ? {
? ? ? ? // 動(dòng)態(tài)增加
? ? ? ? CustomDictionary.add("攻城獅");
? ? ? ? // 強(qiáng)行插入
? ? ? ? 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);
? ? ? ? }
? ? ? ? // 標(biāo)準(zhǔn)分詞
? ? ? ? System.out.println(HanLP.segment(text));
? ? ? ? // Note:動(dòng)態(tài)增刪不會(huì)影響詞典文件
? ? ? ? // 目前CustomDictionary使用DAT儲(chǔ)存詞典文件中的詞語,用BinTrie儲(chǔ)存動(dòng)態(tài)加入的詞語纲爸,前者性能高亥鸠,后者性能低
? ? ? ? // 之所以保留動(dòng)態(tài)增刪功能,一方面是歷史遺留特性缩焦,另一方面是調(diào)試用读虏;未來可能會(huì)去掉動(dòng)態(tài)增刪特性。
? ? }
}
//true
//[攻城獅/nz, 逆襲/nz, 單身狗/nz, 袁滥,/w, 迎娶/v, 白富美/nz, ,/w, 走上/v, 人生/n, 巔峰/n]
//自定義詞:攻城獅灾螃、單身狗题翻、白富美
文章來源于風(fēng)zi的博客