📜  Python – 使用 Affin 进行情感分析

📅  最后修改于: 2022-05-13 01:54:31.878000             🧑  作者: Mango

Python – 使用 Affin 进行情感分析

Afinn是由Finn Årup Nielsen开发的用于情感分析的最简单但最流行的词典。它包含 3300 多个单词,每个单词都有一个极性分数。在Python中,此词典有一个内置函数。

让我们看看它的语法——

安装库:

# code
print("GFG")
pip install afinn / 
#instaalling in windows
pip3 install afinn /
#installing in linux
!pip install afinn
#installing in jupyter

代码:使用 Affin 进行情感分析的Python代码

#importing necessary libraries
from afinn import Afinn
import pandas as pd
  
#instantiate afinn
afn = Afinn()
  
#creating list sentences
news_df = ['les gens pensent aux chiens','i hate flowers',
         'hes kind and smart','we are kind to good people']
           
# compute scores (polarity) and labels
scores = [afn.score(article) for article in news_df]
sentiment = ['positive' if score > 0 
                          else 'negative' if score < 0 
                              else 'neutral' 
                                  for score in scores]
      
# dataframe creation
df = pd.DataFrame()
df['topic'] =  news_df
df['scores'] = scores
df['sentiments'] = sentiment
print(df)

输出:

topic  scores sentiments
0  les gens pensent aux chiens     0.0    neutral
1               i hate flowers    -3.0   negative
2           hes kind and smart     3.0   positive
3   we are kind to good people     5.0   positive

这个库包最好的部分是你还可以找到不同语言的分数情绪。

afn = Afinn(language = 'da')
  
#assigning 'da' danish to the object variable.
afn.score('du er den mest modbydelige tæve')

输出:

-5.0

因此,我们可以轻松地使用 Afinn 立即获得分数。