在機器學習中開發情感分析器的 5 種方法


作者 | Satyam Kumar
編譯 | VK
來源 | Towards Data Science

情感分析是一種自然語言處理技術,用於確定給定文本的情感或觀點。情感分析模型可以通過從自然語言中提取意義並將其分配分數來預測給定的文本數據是正的、負的還是中性的。

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

情緒分析被各種組織用來了解客戶的情緒,並相應地做出更快速和準確的商業決策。

定製訓練監督模型:

你可以訓練一個定製的機器學習或深度學習情感分析模型。一個有標記的數據集是訓練一個健壯的 ML 模型的關鍵。ML 模型將學習數據集中的各種模式,並能預測文本的情感。

要訓練自定義情緒分析模型,必須遵循以下步驟:

閱讀下面的文章,瞭解如何利用樸素貝葉斯分類器算法開發一個影評情感分析模型。

https://satyam-kumar.medium.com/imdb-movie-review-polarity-using-naive-bayes-classifier-9f92c13efa2d

TextBlob:

TextBlob 是一個開源的 Python 庫,用於處理文本數據,允許你在其簡單 API 的框架下指定要使用的算法。TextBlobs 的 API 可以用來執行諸如詞性標註、名詞短語提取、分類、翻譯、情感分析等任務。

對於情緒分析,TextBlob 庫提供了兩種實現:

安裝:

pip install -U textblob

實施:

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)

基於詞典的模型

它涉及到從文本語料庫中創建一個 n-gram 的正負詞詞典。該方法需要一個帶標籤的文本語料庫,並使用自定義 python 函數分別爲正文本和負文本創建一個 n-gram 詞典。

自定義詞也可以添加到字典的基礎上領域知識,作爲一個額外的優勢。

在下一步中,創建一個自定義函數,該函數可以使用上面形成的正負詞詞典來分析給定的輸入文本,並可以將其分類爲正面情緒或負面情緒。

積極情緒得分介於 0 到 1 之間,表示積極情緒,其中 1 表示 100% 置信度的積極情緒預測。然而,負面情緒得分在 - 1 到 0 之間,其中 - 1 是 100% 置信度的負面情緒預測。

實施:

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 開發的 Transformers 的雙向編碼器表示,它是用於 NLP 任務的最先進的 ML 模型。要使用 BERT 訓練情緒分析模型,請執行以下步驟:

實現:

按照下面提到的文章使用 BERT 實現情緒分析模型。

https://towardsdatascience.com/sentiment-analysis-in-10-minutes-with-bert-and-hugging-face-294e8a04b671

基於命名實體的情感分析器:

基於命名實體的情感分析器主要針對實體詞或重要詞。也可以稱爲目標情緒分析,它只關注重要的詞語或實體,比上述三種方法更準確、更有用。

結論:

在本文中,我們討論了開發情緒分析模型的 5 種不同方法。讓我們明白,在開發情緒分析模型時,有各種各樣可用的方法。它需要根據問題陳述和數據集對算法進行規劃和調整。

參考文獻:

[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

謝謝你的閱讀

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/uxjDEFBdFG57J612gjgSAQ