成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用深度學習模型在 Java 中執行文本情感分析

人工智能 深度學習 后端
本文介紹如何使用集成到斯坦福 CoreNLP(一個用于自然語言處理的開源庫)中的情感工具在 Java 中實現此類任務。

積極的? 消極的? 中性的? 使用斯坦福 CoreNLP 組件以及幾行代碼便可對句子進行分析。

[[442118]]

本文介紹如何使用集成到斯坦福 CoreNLP(一個用于自然語言處理的開源庫)中的情感工具在 Java 中實現此類任務。

斯坦福 CoreNLP 情感分類器

要執行情感分析,您需要一個情感分類器,這是一種可以根據從訓練數據集中學習的預測來識別情感信息的工具。

在斯坦福 CoreNLP 中,情感分類器建立在遞歸神經網絡 (RNN) 深度學習模型之上,該模型在斯坦福情感樹庫 (SST) 上進行訓練。

SST 數據集是一個帶有情感標簽的語料庫,從數千個使用的句子中推導出每個句法上可能的短語,從而允許捕獲文本中情感的構成效果。簡單來說,這允許模型根據單詞如何構成短語的含義來識別情緒,而不僅僅是通過孤立地評估單詞。

為了更好地了解 SST 數據集的結構,您可從斯坦福 CoreNLP 情感分析頁面下載數據集文件。

在 Java 代碼中,Stanford CoreNLP 情感分類器使用如下。

首先,您通過添加執行情感分析所需的注釋器(例如標記化、拆分、解析和情感)來構建文本處理管道。 就斯坦福 CoreNLP 而言,注釋器是一個對注釋對象進行操作的接口,其中后者表示文檔中的一段文本。 例如,需要使用 ssplit 注釋器將標記序列拆分為句子。

斯坦福 CoreNLP 以每個句子為基礎計算情緒。 因此,將文本分割成句子的過程始終遵循應用情感注釋器。

一旦文本被分成句子,解析注釋器就會執行句法依賴解析,為每個句子生成一個依賴表示。 然后,情感注釋器處理這些依賴表示,將它們與底層模型進行比較,以構建帶有每個句子的情感標簽(注釋)的二值化樹。

簡單來說,樹的節點由輸入句子的標記確定,并包含注釋,指示從句子導出的所有短語的從非常消極到非常積極的五個情感類別中的預測類別。 基于這些預測,情感注釋器計算整個句子的情感。

設置斯坦福 CoreNLP

在開始使用斯坦福 CoreNLP 之前,您需要進行以下設置:

要運行斯坦福 CoreNLP,您需要 Java 1.8 或更高版本。

下載 Stanford CoreNLP 包并將該包解壓縮到您機器上的本地文件夾中。

下載地址:

https://nlp.stanford.edu/software/stanford-corenlp-latest.zip

本文以將上述代碼解壓到如下目錄為例:

c:/softwareInstall/corenlp/stanford-corenlp-4.3.2

完成上述步驟后,您就可以創建運行斯坦福 CoreNLP 管道來處理文本的 Java 程序了。

首先新建一個maven項目,并手動將stanford-corenlp-4.3.2添加到Libraries中:

 

使用深度學習模型在 Java 中執行文本情感分析

 

在以下示例中,您將實現一個簡單的 Java 程序,該程序運行斯坦福 CoreNLP 管道,以對包含多個句子的文本進行情感分析。

首先,實現一個NlpPipeline類,該類提供初始化管道的方法和使用此管道將提交的文本拆分為句子然后對每個句子的情感進行分類的方法。 下面是NlpPipeline類代碼:

 

  1. package com.zh.ch.corenlp; 
  2.  
  3. import edu.stanford.nlp.ling.CoreAnnotations; 
  4. import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations; 
  5. import edu.stanford.nlp.pipeline.Annotation; 
  6. import edu.stanford.nlp.pipeline.StanfordCoreNLP; 
  7. import edu.stanford.nlp.sentiment.SentimentCoreAnnotations; 
  8. import edu.stanford.nlp.trees.Tree; 
  9. import edu.stanford.nlp.util.CoreMap; 
  10.  
  11. import java.util.Properties; 
  12.  
  13. public class NlpPipeline { 
  14.     StanfordCoreNLP pipeline = null
  15.     public  void init() 
  16.     { 
  17.         Properties props = new Properties(); 
  18.         props.setProperty("annotators""tokenize, ssplit, parse, sentiment"); 
  19.         pipeline = new StanfordCoreNLP(props); 
  20.     } 
  21.     public void estimatingSentiment(String text) 
  22.     { 
  23.         int sentimentInt; 
  24.         String sentimentName; 
  25.         Annotation annotation = pipeline.process(text); 
  26.         for(CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) 
  27.         { 
  28.             Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class); 
  29.             sentimentInt = RNNCoreAnnotations.getPredictedClass(tree); 
  30.             sentimentName = sentence.get(SentimentCoreAnnotations.SentimentClass.class); 
  31.             System.out.println(sentimentName + "\t" + sentimentInt + "\t" + sentence); 
  32.         } 
  33.     } 

 

init() 方法初始化StanfordCoreNLP 管道,它還初始化使用該情感工具所需的分詞器、依賴解析器和句子拆分器。 要初始化管道,請將帶有相應注釋器列表的 Properties 對象傳遞給 StanfordCoreNLP() 構造函數。 這將創建一個定制的管道,準備好對文本執行情感分析。

在NlpPipeline類的estimatingSentiment()方法中,調用之前創建的管道對象的process()方法,傳入文本進行處理。 process() 方法返回一個注釋對象,該對象存儲對提交的文本的分析。

接下來,迭代注釋對象,在每次迭代中獲得一個句子級 CoreMap 對象。對于這些對象中的每一個,獲取一個包含用于確定底層句子情緒的情緒注釋的 Tree 對象。

將 Tree 對象傳遞給 RNNCoreAnnotations 類的 getPredictedClass() 方法,以提取對應句子的預測情緒的編號代碼。然后,獲取預測情緒的名稱并打印結果。

要測試上述功能,請使用調用 init() 方法的 main() 方法實現一個類,然后調用 nlpPipeline 類的 estimatingSentiment() 方法,將示例文本傳遞給后者。

在以下實現中,為了簡單起見,直接指定text文本。示例句子旨在涵蓋斯坦福 CoreNLP 可用的整個情緒評分范圍:非常積極、積極、中立、消極和非常消極。

 

  1. package com.zh.ch.corenlp; 
  2.  
  3. import java.io.FileReader; 
  4. import java.io.IOException; 
  5.  
  6. public class Main { 
  7.      
  8.     static NlpPipeline nlpPipeline = null
  9.  
  10.     public static void processText(String text) { 
  11.         nlpPipeline.estimatingSentiment(text); 
  12.     } 
  13.  
  14.     public static void main(String[] args) { 
  15.         String text = "This is an excellent book. I enjoy reading it. I can read on Sundays. Today is only Tuesday. Can't wait for next Sunday. The working week is unbearably long. It's awful."
  16.         nlpPipeline  = new NlpPipeline(); 
  17.         nlpPipeline.init(); 
  18.         processText(text); 
  19.     } 
  20.  

 

執行結果:

 

使用深度學習模型在 Java 中執行文本情感分析

 

分析在線客戶評論

正如您從前面的示例中了解到的,Stanford CoreNLP 可以返回句子的情緒。 然而,有許多用例需要分析多段文本的情緒,每段文本可能包含不止一個句子。 例如,您可能想要分析來自電子商務網站的推文或客戶評論的情緒。

要使用斯坦福 CoreNLP 計算多句文本樣本的情緒,您可能會使用幾種不同的技術。

在處理推文時,您可能會分析推文中每個句子的情緒,如果有一些正面或負面的句子,您可以分別對整個推文進行排名,忽略帶有中性情緒的句子。 如果推文中的所有(或幾乎所有)句子都是中性的,則該推文可以被列為中性。

然而,有時您甚至不必分析每個句子來估計整個文本的情緒。 例如,在分析客戶評論時,您可以依賴他們的標題,標題通常由一個句子組成。

要完成以下示例,您需要一組客戶評論。 您可以使用本文隨附的 NlpBookReviews.csv 文件中的評論。 該文件包含在 Amazon Review Export 的幫助下從 Amazon 網頁下載的一組實際評論,這是一個 Google Chrome 瀏覽器擴展程序,允許您將產品評論及其標題和評級下載到逗號分隔值 (CSV) 文件中 . (您可以使用該工具探索一組不同的評論以進行分析。)

將下述代碼添加到NlpPipeline中

 

  1. public String findSentiment(String text) { 
  2.         int sentimentInt = 2; 
  3.         String sentimentName = "NULL"
  4.         if (text != null && text.length() > 0) { 
  5.             Annotation annotation = pipeline.process(text); 
  6.             CoreMap sentence = annotation 
  7.                     .get(CoreAnnotations.SentencesAnnotation.class).get(0); 
  8.             Tree tree = sentence 
  9.                     .get(SentimentCoreAnnotations.SentimentAnnotatedTree.class); 
  10.             sentimentInt = RNNCoreAnnotations.getPredictedClass(tree); 
  11.             sentimentName = sentence.get(SentimentCoreAnnotations.SentimentClass.class); 
  12.         } 
  13.         return sentimentName; 
  14.     } 

 

您可能會注意到,上面的代碼類似于上一節中定義的 estimatingSentiment() 方法中的代碼。 唯一的顯著區別是這次您沒有迭代輸入文本中的句子。 相反,您只會得到第一句話,因為在大多數情況下,評論的標題由一個句子組成。

下述代碼將從 CSV 文件中讀取評論并將它們傳遞給新創建的 findSentiment() 進行處理,如下所示:

 

  1. public static void processCsvComment(String csvCommentFilePath) { 
  2.         try (CSVReader reader = new CSVReaderBuilder(new FileReader(csvCommentFilePath)).withSkipLines(1).build()) 
  3.         { 
  4.             String[] row; 
  5.             while ((row = reader.readNext()) != null) { 
  6.                 System.out.println("Review: " + row[1] + "\t" + " Amazon rating: " + row[4] + "\t" + " Sentiment: " + nlpPipeline.findSentiment(row[1])); 
  7.             } 
  8.         } 
  9.         catch (IOException | CsvValidationException e) { 
  10.             e.printStackTrace(); 
  11.         } 
  12.     } 

 

執行結果:

 

使用深度學習模型在 Java 中執行文本情感分析

 

完整代碼:

NlpPipeline.java

 

  1. package com.zh.ch.corenlp; 
  2.  
  3. import edu.stanford.nlp.ling.CoreAnnotations; 
  4. import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations; 
  5. import edu.stanford.nlp.pipeline.Annotation; 
  6. import edu.stanford.nlp.pipeline.StanfordCoreNLP; 
  7. import edu.stanford.nlp.sentiment.SentimentCoreAnnotations; 
  8. import edu.stanford.nlp.trees.Tree; 
  9. import edu.stanford.nlp.util.CoreMap; 
  10.  
  11. import java.util.Properties; 
  12.  
  13. public class NlpPipeline { 
  14.     StanfordCoreNLP pipeline = null
  15.     public  void init() { 
  16.         Properties props = new Properties(); 
  17.         props.setProperty("annotators""tokenize, ssplit, parse, sentiment"); 
  18.         pipeline = new StanfordCoreNLP(props); 
  19.     } 
  20.     public void estimatingSentiment(String text) { 
  21.         int sentimentInt; 
  22.         String sentimentName; 
  23.         Annotation annotation = pipeline.process(text); 
  24.         for(CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) 
  25.         { 
  26.             Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class); 
  27.             sentimentInt = RNNCoreAnnotations.getPredictedClass(tree); 
  28.             sentimentName = sentence.get(SentimentCoreAnnotations.SentimentClass.class); 
  29.             System.out.println(sentimentName + "\t" + sentimentInt + "\t" + sentence); 
  30.         } 
  31.     } 
  32.  
  33.     public String findSentiment(String text) { 
  34.         int sentimentInt = 2; 
  35.         String sentimentName = "NULL"
  36.         if (text != null && text.length() > 0) { 
  37.             Annotation annotation = pipeline.process(text); 
  38.             CoreMap sentence = annotation 
  39.                     .get(CoreAnnotations.SentencesAnnotation.class).get(0); 
  40.             Tree tree = sentence 
  41.                     .get(SentimentCoreAnnotations.SentimentAnnotatedTree.class); 
  42.             sentimentInt = RNNCoreAnnotations.getPredictedClass(tree); 
  43.             sentimentName = sentence.get(SentimentCoreAnnotations.SentimentClass.class); 
  44.         } 
  45.         return sentimentName; 
  46.     } 

 

Main.java

 

  1. package com.zh.ch.corenlp; 
  2.  
  3. import com.opencsv.CSVReader; 
  4. import com.opencsv.CSVReaderBuilder; 
  5. import com.opencsv.exceptions.CsvValidationException; 
  6.  
  7. import java.io.FileReader; 
  8. import java.io.IOException; 
  9.  
  10. public class Main { 
  11.     static NlpPipeline nlpPipeline = null
  12.  
  13.     public static void processCsvComment(String csvCommentFilePath) { 
  14.         try (CSVReader reader = new CSVReaderBuilder(new FileReader(csvCommentFilePath)).withSkipLines(1).build()) 
  15.         { 
  16.             String[] row; 
  17.             while ((row = reader.readNext()) != null) { 
  18.                 System.out.println("Review: " + row[1] + "\t" + " Amazon rating: " + row[4] + "\t" + " Sentiment: " + nlpPipeline.findSentiment(row[1])); 
  19.             } 
  20.         } 
  21.         catch (IOException | CsvValidationException e) { 
  22.             e.printStackTrace(); 
  23.         } 
  24.     } 
  25.  
  26.     public static void processText(String text) { 
  27.         nlpPipeline.estimatingSentiment(text); 
  28.     } 
  29.  
  30.     public static void main(String[] args) { 
  31.         String text = "This is an excellent book. I enjoy reading it. I can read on Sundays. Today is only Tuesday. Can't wait for next Sunday. The working week is unbearably long. It's awful."
  32.         nlpPipeline  = new NlpPipeline(); 
  33.         nlpPipeline.init(); 
  34. //        processText(text); 
  35.         processCsvComment("src/main/resources/NlpBookReviews.csv"); 
  36.     } 

 

 

責任編輯:華軒 來源: 今日頭條
相關推薦

2016-11-16 15:05:42

情感分析

2017-07-12 10:44:31

CNNLSTMNLP

2023-02-03 11:40:49

機器學習分析情感

2023-11-28 09:00:00

機器學習少樣本學習SetFit

2017-05-04 08:48:36

達觀數據分析架構

2016-11-06 23:21:49

深度學習情感分析

2016-12-07 14:45:25

KNIME情感分析數據分析

2017-05-15 14:00:28

大數據Python情感極性分析

2017-02-07 10:22:53

2017-09-06 14:56:09

深度學習CTR應用

2023-01-09 08:00:00

遷移學習機器學習數據集

2017-07-24 13:37:42

深度學習KerasTensorFlow

2023-02-23 07:46:48

學習模型數據倉庫

2019-08-08 08:00:00

深度學習機器學習神經網絡

2022-06-20 06:19:25

深度學習5G攔截系統5G不良管控平臺

2023-05-29 08:00:00

ChatGPT人工智能機器學習

2018-03-27 13:33:48

百度

2021-08-30 07:57:26

OpenAttack文本對抗攻擊

2024-06-26 10:50:35

2025-06-10 01:00:00

深度學習LLM預訓練
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久精品网 | 亚洲人人舔人人 | 亚洲高清免费视频 | 亚洲国产精品久久久久秋霞不卡 | 免费观看www7722午夜电影 | 玖玖综合网 | 午夜丰满寂寞少妇精品 | 日日干日日操 | 日韩久久网 | 久草视频观看 | 在线一区观看 | 欧美电影在线 | 成人一区av| 99综合| 国产精品久久久免费 | 在线成人www免费观看视频 | av片在线免费看 | 久久99久久99久久 | 国产精品高潮呻吟久久久久 | 伊人狠狠 | 精品欧美一区二区精品久久久 | 日韩欧美在线观看视频 | 美女视频一区二区三区 | 亚洲精品在线观 | 午夜一区二区三区视频 | 五月天国产视频 | 国产黄色在线观看 | 国产精品自产拍 | 国产人久久人人人人爽 | 欧美一区二区免费视频 | 亚洲免费视频播放 | 亚洲视频二区 | 91精品国产91久久久久久 | 免费国产视频 | 日韩亚洲欧美综合 | 国产精品久久久久久久久 | 欧美精品片 | 久久久久国产一区二区三区 | 日韩高清中文字幕 | 五月婷六月丁香 | 日韩精品久久久 |