磐创AI分享
作者 | Satyam Kumar
编译 | VK
来源 | Towards Data Science
情感分析是一种自然语言处理技术,用于确定给定文本的情感或观点。情感分析模型可以通过从自然语言中提取意义并将其分配分数来预测给定的文本数据是正的、负的还是中性的。
开发或训练情绪分析模型有多种方法,本文中我们将讨论5种不同的方法:
定制训练监督模型:
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)
基于词典的模型
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:
基于命名实体的情感分析器:
结论:
2021-05-02
2021-05-01
2021-04-30
看到这里,说明你喜欢这篇文章,请点击「在看」或顺手「转发」「点赞」。
欢迎微信搜索「panchuangxx」,添加小编磐小小仙微信,每日朋友圈更新一篇高质量推文(无广告),为您提供更多精彩内容。
▼ ▼ 扫描二维码添加小编 ▼ ▼