java-hanlp中文語言處理

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)建詞典緩存,請稍候……");

//? ? ? ? HanLP.Config.enableDebug();? ? ? ? // 為了避免你等得無聊,開啟調(diào)試模式說點(diǎn)什么:-)

? ? ? ? System.out.println(HanLP.segment("你好,歡迎使用HanLP漢語處理包!接下來請從其他Demo中體驗(yàn)HanLP豐富的功能~"));

? ? }

}

/**

首次編譯運(yùn)行時(shí),HanLP會(huì)自動(dòng)構(gòu)建詞典緩存,請稍候……

[你好/vl, ,/w, 歡迎/v, 使用/v, HanLP/nx, 漢語/gi, 處理/vn, 包/v, !/w, 接下來/vl, 請/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的博客

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容