pythonpandastwittersentiment-analysistextblob

How to perform Sentiment Analysis on Noun Phrases in Pandas?


I need your help as i tried every method but not able to perform sentiment analysis on my noun phrases, extracted from tweets in dataframe, using TextBlob. Also i think TextBlob.noun_phrases function is not producing the correct results. See for yourself in the image below. I am really new to Python, please help!!

So my code for extracting the Noun phrase from dataframe is:

from textblob import TextBlob
nltk.download('wordnet')
nltk.download('brown')
nltk.download('punkt')

def blob(text):
  return TextBlob(text).noun_phrases

df['Noun_Phrases'] = df['Tweets'].apply(blob)

df

enter image description here

Next, my code for sentiment analysis is below, and i get the error as shown in below image:

def getsubjectivity(text):
return TextBlob(text).sentiment.subjectivity 

df['Subjectivity'] = df['Noun_Phrases'].apply(getsubjectivity)

Error : TypeError: The text argument passed to __init__(text) must be a string, not <class 'textblob.blob.WordList'>

enter image description here


Solution

  • not sure about your objective. in your getsubjectivity function the input need to be string, seems like you are feeding it a list.

    if you make the below change, you will overcome the error.

    def getsubjectivity(text):
        text=''.join(text)
        return TextBlob(text).sentiment.subjectivity