通過(guò)Java實(shí)現(xiàn)中文分詞與文本關(guān)鍵詞提取
我當(dāng)前在做的項(xiàng)目需求:在xx單子中提取出我想要的關(guān)鍵詞,涉及中文分詞的內(nèi)容,可以借助IK分詞器實(shí)現(xiàn)此功能。
1、引入依賴
ik用于分詞,commons-io用來(lái)讀取文件內(nèi)容(我懶)
<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>注意:如果項(xiàng)目使用了ElasticSearch,可能會(huì)出現(xiàn)沖突,需根據(jù)你的情況手動(dòng)排除,如下
<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
<exclusions>
<exclusion>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>2、創(chuàng)建自己的詞典
創(chuàng)建文件,在里面輸入自己想要擴(kuò)充的詞語(yǔ),放到resources中,命名如“keywords.dic”

3、創(chuàng)建分詞工具類
package com.iherb.user.util;
import org.apache.commons.io.IOUtils;
import org.wltea.analyzer.cfg.Configuration;
import org.wltea.analyzer.cfg.DefaultConfig;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import org.wltea.analyzer.dic.Dictionary;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class KeywordUtil {
Configuration cfg;
List<String> expandWords = new ArrayList<>();
/**
* 每個(gè)詞的最小長(zhǎng)度
*/
private static final int MIN_LEN = 2;
KeywordUtil() {
cfg = DefaultConfig.getInstance();
cfg.setUseSmart(true); //設(shè)置useSmart標(biāo)志位 true-智能切分 false-細(xì)粒度切分
boolean flag = loadDictionaries("keywords.dic");
if (!flag) {
throw new RuntimeException("讀取失敗");
}
Dictionary.initial(cfg);
Dictionary.getSingleton().addWords(expandWords); //詞典中加入自定義單詞
}
/**
* 加載自定義詞典,若無(wú)想要添加的詞則無(wú)需調(diào)用,使用默認(rèn)的詞典
* @param filenames
* @return
*/
private boolean loadDictionaries(String... filenames) {
try {
for (String filename : filenames) {
expandWords.addAll(
IOUtils.readLines(
KeywordUtil.class.getClassLoader().getResourceAsStream(filename),
StandardCharsets.UTF_8
)
);
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 提取詞語(yǔ),結(jié)果將按頻率排序
* @param text 待提取的文本
* @return 提取出的詞
*/
public List<String> extract(String text) {
StringReader reader = new StringReader(text);
IKSegmenter ikSegmenter = new IKSegmenter(reader, cfg);
Lexeme lex;
Map<String, Integer> countMap = new HashMap<>();
try {
while ((lex = ikSegmenter.next()) != null) {
String word = lex.getLexemeText();
if (word.length() >= MIN_LEN) { //取出的詞至少#{MIN_LEN}個(gè)字
countMap.put(word, countMap.getOrDefault(word, 0) + 1);
}
}
List<String> result = new ArrayList<>(countMap.keySet());
//根據(jù)詞出現(xiàn)頻率從大到小排序
result.sort((w1, w2) -> countMap.get(w2) - countMap.get(w1));
return result;
} catch (Exception e) {
e.printStackTrace();
}
return Collections.emptyList();
}
/**
* 提取存在于我擴(kuò)充詞典的詞
* @param num 需要提取的詞個(gè)數(shù)
* @return
*/
public List<String> getKeywords(String text, Integer num) {
List<String> words = extract(text);
List<String> result = new ArrayList<>();
int count = 0;
for (String word : words) {
if (expandWords.contains(word)) {
result.add(word);
if (++count == num) {
break;
}
}
}
return result;
}
public static void main(String[] args) {
String text = "哈哈無(wú)花果翠云草酢漿草是什么,。我是帥哥666無(wú)花果真好吃還有北沙參穿心蓮翠云草,草豆蔻和蟬蛻酢漿草也不錯(cuò)的";
KeywordUtil keywordUtil = new KeywordUtil();
List<String> keywords = keywordUtil.getKeywords(text, 5);
keywords.forEach(System.out::println);
}
}4、測(cè)試

以上就是通過(guò)Java實(shí)現(xiàn)中文分詞與文本關(guān)鍵詞提取的詳細(xì)內(nèi)容,更多關(guān)于Java分詞的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MyBatis-Plus聯(lián)表查詢以及分頁(yè)代碼實(shí)例
在開發(fā)中遇到了一個(gè)問(wèn)題,需要進(jìn)行聯(lián)表查詢并進(jìn)行分頁(yè),因?yàn)椴幌胱约簛?lái)寫分頁(yè),所以還是依靠MybatisPlus來(lái)實(shí)現(xiàn)想要的功能,下面這篇文章主要給大家介紹了關(guān)于MyBatis-Plus聯(lián)表查詢以及分頁(yè)的相關(guān)資料,需要的朋友可以參考下2023-06-06
解讀maven項(xiàng)目中Tomcat10與JSTL的問(wèn)題匯總(Debug親身經(jīng)歷)
這篇文章主要介紹了解讀maven項(xiàng)目中Tomcat10與JSTL的問(wèn)題匯總(Debug親身經(jīng)歷),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Java如何使用multipartFile對(duì)象解析Execl
本文介紹了如何使用Spring的MultipartFile類解析Excel文件(.xls和.xlsx),包括文件上傳、數(shù)據(jù)校驗(yàn)、輸入流獲取、文件解析、數(shù)據(jù)保存和異常處理的詳細(xì)步驟2025-02-02
淺談Maven鏡像更換為阿里云中央倉(cāng)庫(kù)(精)
本篇文章主要介紹了Maven鏡像更換為阿里云中央倉(cāng)庫(kù)(精),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Java中Hashtable類與HashMap類的區(qū)別詳解
Hashtable的應(yīng)用非常廣泛,HashMap是新框架中用來(lái)代替Hashtable的類,也就是說(shuō)建議使用HashMap,不要使用Hashtable。可能你覺(jué)得Hashtable很好用,為什么不用呢?這里簡(jiǎn)單分析他們的區(qū)別。2016-01-01
詳解在IDEA中使用MyBatis Generator逆向工程生成代碼
這篇文章主要介紹了詳解在IDEA中使用MyBatis Generator逆向工程生成代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
Springboot整合freemarker和相應(yīng)的語(yǔ)法詳解
FreeMarker是一款Spring官方推薦使用的模板引擎。接下來(lái)通過(guò)本文給大家介紹Springboot整合freemarker和相應(yīng)的語(yǔ)法,感興趣的朋友一起看看吧2021-09-09

