一、HanLP
HanLP (Hankcs' NLP) 是一個自然語言處理工具包,具有功能強大、性能高效、易于使用的特點。HanLP 主要支持中文文本處理,包括分詞、詞性標注、命名實體識別、依存句法分析、關鍵詞提取、文本分類、情感分析等多種功能。 HanLP 可以在 Java、Python、Go 等多種編程語言中使用,也提供了各種語言的 API 接口,方便用戶進行二次開發。HanLP 采用了深度學習和傳統機器學習相結合的方法,具有較高的準確度和通用性。
二、java中用HanLP做情感分詞場景
首先,下載HanLP jar包。可以從官方網站(https://github.com/hankcs/HanLP/releases)下載或者使用Maven配置。
<dependency>
? ? <groupId>com.hankcs</groupId>
? ? <artifactId>hanlp</artifactId>
? ? <version>portable-1.7.8</version>
</dependency>
引入完成后,在代碼中調用HanLP工具類的方法,例如:
import com.hankcs.hanlp.HanLP;public class TestHanLP {public static void main(String[] args) {String text = "中華人民共和國成立了!";System.out.println(HanLP.segment(text));}
}
運行以上代碼,可以得到分詞結果:
[中華人民共和國, 成立, 了, !]
除了分詞外,HanLP還提供了許多其他功能,例如實體識別、關鍵詞提取、自動摘要等。可以通過調用不同的方法來實現這些功能,具體可參考HanLP官方文檔(https://github.com/hankcs/HanLP)。
需要注意的是,HanLP默認使用的是繁體中文模型,如果需要使用簡體中文模型,可以在代碼中添加以下語句:
HanLP.Config.enableDebug();
HanLP.Config.Normalization = true;
這樣就可以使用簡體中文模型進行處理了。
三、SpringBoot中如何使用Hanlp進行文本情感分析
????????第一步:
????????????????在pom.xml文件中添加Hanlp的依賴
<dependency><groupId>com.hankcs</groupId><artifactId>hanlp</artifactId><version>portable-1.7.8</version>
</dependency>
????????第二步:
????????????????創建一個SpringBoot的Controller,用于接收文本數據,并進行情感分析
@RestController
public class SentimentAnalysisController {@PostMapping("/sentimentAnalysis")public String sentimentAnalysis(@RequestBody String text) {String[] sentences = HanLP.extractSentence(text);int positiveCount = 0;int negativeCount = 0;for (String sentence : sentences) {List<String> keywords = HanLP.extractKeyword(sentence, 5);for (String keyword : keywords) {if (SentimentUtil.isPositive(keyword)) {positiveCount++;} else if (SentimentUtil.isNegative(keyword)) {negativeCount++;}}}if (positiveCount > negativeCount) {return "Positive";} else if (positiveCount < negativeCount) {return "Negative";} else {return "Neutral";}}
}
????????第三步:
????????????????上述代碼中用到了SentimentUtil類,可以參考以下實現,用于判斷一個詞語的情感傾向
public class SentimentUtil {private static final Set<String> POSITIVE_WORDS = new HashSet<>(Arrays.asList("好", "美", "樂", "棒", "贊", "愛", "優秀", "高興", "滿意", "友好", "感動"));private static final Set<String> NEGATIVE_WORDS = new HashSet<>(Arrays.asList("壞", "丑", "難受", "差", "批評", "悲", "痛苦", "憤怒", "失望", "憎惡", "恐懼", "憂郁", "抱怨"));public static boolean isPositive(String word) {return POSITIVE_WORDS.contains(word);}public static boolean isNegative(String word) {return NEGATIVE_WORDS.contains(word);}
}
最后:
啟動SpringBoot應用,可以使用curl或其他工具,向http://localhost:8080/sentimentAnalysis發送POST請求,請求體為要進行情感分析的文本數據。返回結果可以是Positive、Negative或Neutral。
注意:上述代碼僅僅是示例代碼,可以根據具體的需求進行修改和優化。在實際使用中,也需要根據具體情況對Hanlp的功能進行擴展和調整。