自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用深度學(xué)習(xí)模型在 Java 中執(zhí)行文本情感分析

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

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

[[442118]]

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

斯坦福 CoreNLP 情感分類器

要執(zhí)行情感分析,您需要一個(gè)情感分類器,這是一種可以根據(jù)從訓(xùn)練數(shù)據(jù)集中學(xué)習(xí)的預(yù)測來識別情感信息的工具。

在斯坦福 CoreNLP 中,情感分類器建立在遞歸神經(jīng)網(wǎng)絡(luò) (RNN) 深度學(xué)習(xí)模型之上,該模型在斯坦福情感樹庫 (SST) 上進(jìn)行訓(xùn)練。

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

為了更好地了解 SST 數(shù)據(jù)集的結(jié)構(gòu),您可從斯坦福 CoreNLP 情感分析頁面下載數(shù)據(jù)集文件。

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

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

斯坦福 CoreNLP 以每個(gè)句子為基礎(chǔ)計(jì)算情緒。 因此,將文本分割成句子的過程始終遵循應(yīng)用情感注釋器。

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

簡單來說,樹的節(jié)點(diǎn)由輸入句子的標(biāo)記確定,并包含注釋,指示從句子導(dǎo)出的所有短語的從非常消極到非常積極的五個(gè)情感類別中的預(yù)測類別。 基于這些預(yù)測,情感注釋器計(jì)算整個(gè)句子的情感。

設(shè)置斯坦福 CoreNLP

在開始使用斯坦福 CoreNLP 之前,您需要進(jìn)行以下設(shè)置:

要運(yùn)行斯坦福 CoreNLP,您需要 Java 1.8 或更高版本。

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

下載地址:

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

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

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

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

首先新建一個(gè)maven項(xiàng)目,并手動將stanford-corenlp-4.3.2添加到Libraries中:

 

使用深度學(xué)習(xí)模型在 Java 中執(zhí)行文本情感分析

 

在以下示例中,您將實(shí)現(xiàn)一個(gè)簡單的 Java 程序,該程序運(yùn)行斯坦福 CoreNLP 管道,以對包含多個(gè)句子的文本進(jìn)行情感分析。

首先,實(shí)現(xiàn)一個(gè)NlpPipeline類,該類提供初始化管道的方法和使用此管道將提交的文本拆分為句子然后對每個(gè)句子的情感進(jìn)行分類的方法。 下面是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 管道,它還初始化使用該情感工具所需的分詞器、依賴解析器和句子拆分器。 要初始化管道,請將帶有相應(yīng)注釋器列表的 Properties 對象傳遞給 StanfordCoreNLP() 構(gòu)造函數(shù)。 這將創(chuàng)建一個(gè)定制的管道,準(zhǔn)備好對文本執(zhí)行情感分析。

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

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

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

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

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

 

  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.  

 

執(zhí)行結(jié)果:

 

使用深度學(xué)習(xí)模型在 Java 中執(zhí)行文本情感分析

 

分析在線客戶評論

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

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

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

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

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

將下述代碼添加到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.     } 

 

您可能會注意到,上面的代碼類似于上一節(jié)中定義的 estimatingSentiment() 方法中的代碼。 唯一的顯著區(qū)別是這次您沒有迭代輸入文本中的句子。 相反,您只會得到第一句話,因?yàn)樵诖蠖鄶?shù)情況下,評論的標(biāo)題由一個(gè)句子組成。

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

 

  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.     } 

 

執(zhí)行結(jié)果:

 

使用深度學(xué)習(xí)模型在 Java 中執(zhí)行文本情感分析

 

完整代碼:

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.     } 

 

 

責(zé)任編輯:華軒 來源: 今日頭條
相關(guān)推薦

2016-11-16 15:05:42

情感分析

2017-07-12 10:44:31

CNNLSTMNLP

2023-02-03 11:40:49

機(jī)器學(xué)習(xí)分析情感

2023-11-28 09:00:00

機(jī)器學(xué)習(xí)少樣本學(xué)習(xí)SetFit

2017-05-04 08:48:36

達(dá)觀數(shù)據(jù)分析架構(gòu)

2016-11-06 23:21:49

深度學(xué)習(xí)情感分析

2016-12-07 14:45:25

KNIME情感分析數(shù)據(jù)分析

2017-02-07 10:22:53

2017-05-15 14:00:28

大數(shù)據(jù)Python情感極性分析

2023-01-09 08:00:00

遷移學(xué)習(xí)機(jī)器學(xué)習(xí)數(shù)據(jù)集

2017-09-06 14:56:09

深度學(xué)習(xí)CTR應(yīng)用

2023-02-23 07:46:48

學(xué)習(xí)模型數(shù)據(jù)倉庫

2022-06-20 06:19:25

深度學(xué)習(xí)5G攔截系統(tǒng)5G不良管控平臺

2017-07-24 13:37:42

深度學(xué)習(xí)KerasTensorFlow

2021-08-30 07:57:26

OpenAttack文本對抗攻擊

2018-03-27 13:33:48

百度

2019-08-08 08:00:00

深度學(xué)習(xí)機(jī)器學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)

2023-05-29 08:00:00

ChatGPT人工智能機(jī)器學(xué)習(xí)

2018-01-04 13:07:43

Python機(jī)器學(xué)習(xí)情感分析

2024-06-26 10:50:35

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號