首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Python中根据Textblob的极性从Textblob中获取积极和消极的单词(情感分析)

在Python中根据Textblob的极性从Textblob中获取积极和消极的单词(情感分析)
EN

Stack Overflow用户
提问于 2018-04-09 19:05:57
回答 2查看 5.2K关注 0票数 5

我有一个文本blob,其中如果极性> 0,则将文本分类为正文本;如果极性= 0,则将文本分类为中性文本;如果极性< 0,则将文本分类为负文本。我如何才能得到它分类为正面、负面或中性的单词?

EN

回答 2

Stack Overflow用户

发布于 2018-07-14 12:55:02

我希望下面的代码能对你有所帮助:

代码语言:javascript
运行
复制
from textblob import TextBlob
from textblob.sentiments import NaiveBayesAnalyzer
import nltk
nltk.download('movie_reviews')
nltk.download('punkt')

text          = "I feel the product is so good" 

sent          = TextBlob(text)
# The polarity score is a float within the range [-1.0, 1.0]
# where negative value indicates negative text and positive
# value indicates that the given text is positive.
polarity      = sent.sentiment.polarity
# The subjectivity is a float within the range [0.0, 1.0] where
# 0.0 is very objective and 1.0 is very subjective.
subjectivity  = sent.sentiment.subjectivity

sent          = TextBlob(text, analyzer = NaiveBayesAnalyzer())
classification= sent.sentiment.classification
positive      = sent.sentiment.p_pos
negative      = sent.sentiment.p_neg

print(polarity,subjectivity,classification,positive,negative)
票数 5
EN

Stack Overflow用户

发布于 2021-01-25 02:12:23

给维达打一针吧。Vader是一个基于规则的情感分析工具,适用于社交媒体文本和常规文本。

代码语言:javascript
运行
复制
# import SentimentIntensityAnalyzer class 
import nltk
from nltk.tokenize import word_tokenize, RegexpTokenizer
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer 
  
# function to print sentiments 
# of the sentence. 
def sentiment_scores(sentence): 
  
    # Create a SentimentIntensityAnalyzer object. 
    sid_obj = SentimentIntensityAnalyzer() 
  
    # polarity_scores method of SentimentIntensityAnalyzer 
    # oject gives a sentiment dictionary. 
    # which contains pos, neg, neu, and compound scores. 
    sentiment_dict = sid_obj.polarity_scores(sentence) 
      
    print("Overall sentiment dictionary is : ", sentiment_dict) 
    print("sentence was rated as ", sentiment_dict['neg']*100, "% Negative") 
    print("sentence was rated as ", sentiment_dict['neu']*100, "% Neutral") 
    print("sentence was rated as ", sentiment_dict['pos']*100, "% Positive") 
  
    print("Sentence Overall Rated As", end = " ") 
  
    # decide sentiment as positive, negative and neutral 
    if sentiment_dict['compound'] >= 0.05 : 
        print("Positive") 
  
    elif sentiment_dict['compound'] <= - 0.05 : 
        print("Negative") 
  
    else : 
        print("Neutral") 
  
  
    
# Driver code 
if __name__ == "__main__" : 
  
    print("\n1st statement :") 
    sentence = "This is the best movie I have watched ever!" 
  
    # function calling 
    sentiment_scores(sentence) 
  
    print("\n2nd Statement :") 
    sentence = "I went to the market"
    sentiment_scores(sentence) 
  
    print("\n3rd Statement :") 
    sentence = "I would not recommend this product to you"
    sentiment_scores(sentence)

输出

代码语言:javascript
运行
复制
1st statement :
Overall sentiment dictionary is :  {'neg': 0.0, 'neu': 0.64, 'pos': 0.36, 'compound': 0.6696}
sentence was rated as  0.0 % Negative
sentence was rated as  64.0 % Neutral
sentence was rated as  36.0 % Positive
Sentence Overall Rated As Positive

2nd Statement :
Overall sentiment dictionary is :  {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}
sentence was rated as  0.0 % Negative
sentence was rated as  100.0 % Neutral
sentence was rated as  0.0 % Positive
Sentence Overall Rated As Neutral

3rd Statement :
Overall sentiment dictionary is :  {'neg': 0.232, 'neu': 0.768, 'pos': 0.0, 'compound': -0.2755}
sentence was rated as  23.200000000000003 % Negative
sentence was rated as  76.8 % Neutral
sentence was rated as  0.0 % Positive
Sentence Overall Rated As Negative

参考文献:

  1. https://pypi.org/project/vaderSentiment/
  2. https://www.geeksforgeeks.org/python-sentiment-analysis-using-vader/
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49731478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档