<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          【NLP】在機(jī)器學(xué)習(xí)中開發(fā)情感分析器的5種方法

          共 4714字,需瀏覽 10分鐘

           ·

          2021-05-05 12:17

          作者 | Satyam Kumar

          編譯 | VK
          來源 | Towards Data Science

          情感分析是一種自然語言處理技術(shù),用于確定給定文本的情感或觀點(diǎn)。情感分析模型可以通過從自然語言中提取意義并將其分配分?jǐn)?shù)來預(yù)測給定的文本數(shù)據(jù)是正的、負(fù)的還是中性的。

          開發(fā)或訓(xùn)練情緒分析模型有多種方法,本文中我們將討論5種不同的方法:

          • 定制訓(xùn)練監(jiān)督模型
          • TextBlob
          • 基于詞典的模型
          • Bert
          • 基于命名實(shí)體的情感分析器
          情緒分析被各種組織用來了解客戶的情緒,并相應(yīng)地做出更快速和準(zhǔn)確的商業(yè)決策。


          定制訓(xùn)練監(jiān)督模型:


          你可以訓(xùn)練一個定制的機(jī)器學(xué)習(xí)或深度學(xué)習(xí)情感分析模型。一個有標(biāo)記的數(shù)據(jù)集是訓(xùn)練一個健壯的ML模型的關(guān)鍵。ML模型將學(xué)習(xí)數(shù)據(jù)集中的各種模式,并能預(yù)測文本的情感。
          要訓(xùn)練自定義情緒分析模型,必須遵循以下步驟:
          • 收集原始標(biāo)記數(shù)據(jù)集進(jìn)行情緒分析。
          • 文本預(yù)處理
          • 文本的數(shù)字編碼
          • 選擇合適的ML算法
          • 調(diào)參與訓(xùn)練ML模型
          • 預(yù)測
          閱讀下面的文章,了解如何利用樸素貝葉斯分類器算法開發(fā)一個影評情感分析模型。
          https://satyam-kumar.medium.com/imdb-movie-review-polarity-using-naive-bayes-classifier-9f92c13efa2d


          TextBlob:


          TextBlob是一個開源的Python庫,用于處理文本數(shù)據(jù),允許你在其簡單API的框架下指定要使用的算法。TextBlobs的API可以用來執(zhí)行諸如詞性標(biāo)注、名詞短語提取、分類、翻譯、情感分析等任務(wù)。
          對于情緒分析,TextBlob庫提供了兩種實(shí)現(xiàn):
          • patternalyzer:(默認(rèn))基于模式庫。
          • NaiveBayesAnalyzer:一個基于電影評論語料庫的NLTK分類器。

          安裝:

          pip install -U textblob

          實(shí)施:

          from textblob import TextBlob
          from textblob.sentiments import NaiveBayesAnalyzer, PatternAnalyzer

          text = 'I liked the movie, the actors performance was too good.'

          # NaiveBayesAnalyzer
          blob = TextBlob(text, analyzer=NaiveBayesAnalyzer())
          print(blob.sentiment)

          # PatternAnalyzer
          blob = TextBlob(text, analyzer=PatternAnalyzer())
          print(blob.sentiment)


          基于詞典的模型


          它涉及到從文本語料庫中創(chuàng)建一個n-gram的正負(fù)詞詞典。該方法需要一個帶標(biāo)簽的文本語料庫,并使用自定義python函數(shù)分別為正文本和負(fù)文本創(chuàng)建一個n-gram詞典。
          自定義詞也可以添加到字典的基礎(chǔ)上領(lǐng)域知識,作為一個額外的優(yōu)勢。
          在下一步中,創(chuàng)建一個自定義函數(shù),該函數(shù)可以使用上面形成的正負(fù)詞詞典來分析給定的輸入文本,并可以將其分類為正面情緒或負(fù)面情緒。
          • 輸入文本中的每個積極詞都會增加情感得分,而消極詞則會減少情感得分。
          • 將最后的情緒分?jǐn)?shù)除以該文本中的字?jǐn)?shù),以使分?jǐn)?shù)標(biāo)準(zhǔn)化。
          積極情緒得分介于0到1之間,表示積極情緒,其中1表示100%置信度的積極情緒預(yù)測。然而,負(fù)面情緒得分在-1到0之間,其中-1是100%置信度的負(fù)面情緒預(yù)測。

          實(shí)施:

          import nltk

          pos_words = []
          neg_words = []

          def compute_sentiment_score(text):
              sentiment_score = 0
              words = nltk.word_tokenize(text)
              for word in words:
                  if word in pos_words:
                      print('pos:',word)
                      sentiment_score=sentiment_score+1
                  if word in neg_words:
                      print('neg:',word)
                      sentiment_score=sentiment_score-1
              return sentiment_score/len(words)



          with open('datapath'as file:
              for line in file:
                  line_attrib = line.split()
                  word = line_attrib[2].split('=')[1#2nd column in the file
                  polarity = line_attrib[-1].split('=')[1#last column in the file
                  if polarity =='positive':
                      pos_words.append(word)
                  elif polarity=='negative':
                      neg_words.append(word)

          print('Total positive words found: ',len(pos_words))
          print('Total negative words found: ',len(neg_words))


          text = 'I loved the movie, the actors performance was mindblowing.'
          sentiment = compute_sentiment_score(text)
          print('The sentiment score of this text is: {:.2f}'.format(sentiment))


          BERT:


          BERT代表來自Google開發(fā)的Transformers的雙向編碼器表示,它是用于NLP任務(wù)的最先進(jìn)的ML模型。要使用BERT訓(xùn)練情緒分析模型,請執(zhí)行以下步驟:
          • 安裝Transformer庫
          • 加載BERT分類器和標(biāo)記器
          • 創(chuàng)建已處理的數(shù)據(jù)集
          • 配置和訓(xùn)練加載的BERT模型,并對其超參數(shù)進(jìn)行微調(diào)
          • 進(jìn)行情緒分析預(yù)測

          實(shí)現(xiàn):

          按照下面提到的文章使用BERT實(shí)現(xiàn)情緒分析模型。
          https://towardsdatascience.com/sentiment-analysis-in-10-minutes-with-bert-and-hugging-face-294e8a04b671


          基于命名實(shí)體的情感分析器:


          基于命名實(shí)體的情感分析器主要針對實(shí)體詞或重要詞。也可以稱為目標(biāo)情緒分析,它只關(guān)注重要的詞語或?qū)嶓w,比上述三種方法更準(zhǔn)確、更有用。
          • 第一步是在文本語料庫中找到所有命名實(shí)體。
          • 在文本上應(yīng)用名稱實(shí)體識別來查找各種實(shí)體,如PERSON、ORG、GPE。
          • 基于命名實(shí)體的情感分析。
          • 以找到包含命名實(shí)體的句子為目標(biāo),只對這些句子逐一進(jìn)行情感分析。


          結(jié)論:


          在本文中,我們討論了開發(fā)情緒分析模型的5種不同方法。讓我們明白,在開發(fā)情緒分析模型時,有各種各樣可用的方法。它需要根據(jù)問題陳述和數(shù)據(jù)集對算法進(jìn)行規(guī)劃和調(diào)整。

          參考文獻(xiàn):

          [1] BERT Wiki: https://en.wikipedia.org/wiki/BERT
          [2] Sentiment Analysis using BERT by Orhan G. Yal??n: https://towardsdatascience.com/sentiment-analysis-in-10-minutes-with-bert-and-hugging-face-294e8a04b671

          往期精彩回顧





          本站qq群851320808,加入微信群請掃碼:

          瀏覽 99
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  大鸡吧插逼逼视频 | 无码人妻一区二区一牛影视 | 午夜免费黄色视频 | 红桃视频一区二区三区四区五区在线视频 | sm骚逼麻豆 |