【问题标题】:Get the positive and negative words from a Textblob based on its polarity in Python (Sentimental analysis)根据 Python 中的极性从 Textblob 中获取正面和负面的词(情感分析)
【发布时间】:2018-09-18 18:22:26
【问题描述】:

我有一个文本块,如果极性> 0,我将文本分类为正,如果 = 0,则将文本分类为中性,如果

【问题讨论】:

  • 太模糊了。网上有很多资源,图书馆。在你在这里提问之前请好好研究一下。

标签: python python-3.x machine-learning sentiment-analysis textblob


【解决方案1】:

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

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)

【讨论】:

    【解决方案2】:

    一定要给维德一枪。 Vader 是一种基于规则的情感分析工具,适用于社交媒体文本和常规文本。

    # 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)
    

    输出

    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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-14
      • 2017-10-07
      • 2018-04-29
      • 2011-06-08
      • 2019-08-17
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      相关资源
      最近更新 更多