java?使用HanLP?安裝入門詳細教程
HanLP是一系列模型與算法組成的NLP工具包,目標是普及自然語言處理在生產(chǎn)環(huán)境中的應用。HanLP具備功能完善、性能高效、架構(gòu)清晰、語料時新、可自定義的特點。
HanLP 是基于 Java開發(fā)的 NLP工具包,由一系列模型與算法組成,目標是普及自然語言處理在生產(chǎn)環(huán)境中的應用。而且 HanLP具備功能完善、性能高效、架構(gòu)清晰、語料時新、可自定義的特點,因此十分好上手,下面看下java 使用HanLP 入門教程。
1. 安裝 HanLP
Maven 依賴
<dependency>
<groupId>com.hankcs</groupId>
<artifactId>hanlp</artifactId>
<version>portable-1.8.4</version> <!-- 最新版本請查看官網(wǎng) -->
</dependency>
注意:
portable版本內(nèi)置小型詞典,適合基礎(chǔ)任務(wù);若需完整功能,需下載完整數(shù)據(jù)包。
2. 基礎(chǔ)功能
(1) 分詞
import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;
public class BasicDemo {
public static void main(String[] args) {
String text = "你好,歡迎使用HanLP!這是一段測試文本。";
// 標準分詞
List<Term> termList = HanLP.segment(text);
System.out.println(termList);
// 輸出: [你好/vl, ,/w, 歡迎/v, 使用/v, HanLP/nx, !/w, 這是/r, 一段/m, 測試/vn, 文本/n, 。/w]
}
}(2) 詞性標注
HanLP 的分詞結(jié)果已包含詞性(如 n=名詞,v=動詞):
for (Term term : termList) {
System.out.println(term.word + " : " + term.nature);
}常用詞性標記:
n:名詞v:動詞w:標點符號nx:外文單詞
3. 進階功能
(1) 關(guān)鍵詞提取
import com.hankcs.hanlp.summary.TextRankKeyword; List<String> keywords = HanLP.extractKeyword(text, 5); // 提取前5個關(guān)鍵詞 System.out.println(keywords); // 輸出: [文本, 測試, HanLP, 歡迎, 使用]
(2) 命名實體識別(NER)
List<Term> termList = HanLP.segment("馬云在阿里巴巴工作。");
for (Term term : termList) {
if (term.nature.toString().startsWith("nr")) { // nr=人名
System.out.println("人名: " + term.word);
} else if (term.nature.toString().startsWith("ns")) { // ns=地名
System.out.println("地名: " + term.word);
}
}
// 輸出: 人名: 馬云 地名: 阿里巴巴
(3) 自定義詞典
// 方式1:臨時添加單詞
HanLP.Config.CustomDictionaryPath = new String[]{"data/dictionary/custom/CustomDictionary.txt"};
HanLP.Config.enableDebug();
// 方式2:動態(tài)添加
CustomDictionary.add("量子計算", "n 1024");
CustomDictionary.insert("神經(jīng)網(wǎng)絡(luò)", "n 1024");
// 使用自定義詞典分詞
System.out.println(HanLP.segment("量子計算是未來趨勢"));
// 輸出: [量子計算/n, 是/v, 未來/t, 趨勢/n]4. 高級配置
(1) 切換分詞模式
// 極速詞典分詞(不標注詞性) List<String> fastSegResult = HanLP.segmentFaster(text); // 標準分詞(帶詞性) List<Term> stdSegResult = HanLP.segment(text); // NLP分詞(高精度,需完整數(shù)據(jù)包) List<Term> nlpSegResult = HanLP.newSegment().enableNameRecognize(true).seg(text);
(2) 加載完整數(shù)據(jù)包
- 下載數(shù)據(jù)包并解壓。
- 配置
hanlp.properties:root=path/to/hanlp-data
5. 完整示例
import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;
import java.util.List;
public class HanLPFullDemo {
public static void main(String[] args) {
String text = "清華大學位于北京市海淀區(qū)。";
// 分詞 + 詞性標注
List<Term> terms = HanLP.segment(text);
System.out.println("分詞結(jié)果: " + terms);
// 命名實體識別
terms = HanLP.newSegment().enablePlaceRecognize(true).seg(text);
for (Term term : terms) {
if (term.nature.toString().startsWith("ns")) {
System.out.println("地名: " + term.word);
}
}
// 關(guān)鍵詞提取
List<String> keywords = HanLP.extractKeyword(text, 3);
System.out.println("關(guān)鍵詞: " + keywords);
}
}輸出:
分詞結(jié)果: [清華大學/nt, 位于/v, 北京市/ns, 海淀區(qū)/ns, 。/w]
地名: 北京市
地名: 海淀區(qū)
關(guān)鍵詞: [海淀區(qū), 北京市, 清華大學]
6. 常見問題
- 詞典加載失敗:檢查
hanlp.properties中的root路徑是否正確。 - 內(nèi)存不足:使用
portable版本或增加 JVM 內(nèi)存:-Xms512m -Xmx1024m。 - 性能優(yōu)化:對長文本使用
HanLP.segmentFaster()。
官方資源
HanLP 功能強大且靈活,適合中文 NLP 的各種場景!
到此這篇關(guān)于java 使用HanLP 入門教程的文章就介紹到這了,更多相關(guān)java 使用hanlp內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java自動化實現(xiàn)提取PDF表格數(shù)據(jù)
在數(shù)字化背景下,數(shù)據(jù)是企業(yè)決策的重要依據(jù),如何高效、準確地將PDF中的表格數(shù)據(jù)提取出來,并轉(zhuǎn)換為可分析的結(jié)構(gòu)化格式是面臨的普遍問題,下面我們就來看看具體實現(xiàn)方法吧2025-09-09
maven項目中<scope>provided</scope>的作用及說明
這篇文章主要介紹了maven項目中<scope>provided</scope>的作用及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Spring Boot中配置文件application.properties使用
這篇文章主要介紹了Spring Boot中配置文件application.properties使用及spring boot讀取application.properties文件的方式,需要的朋友參考下吧2018-01-01

