I have a df that looks like this:
text
0 Thanks, I’ll have a read!
1 Am I too late
How do I apply TextBlob tokenization to every word in sentence and average the polarity scores of every word in each sentence?
for example, I can do this with a single sentence in a variable:
from textblob import TextBlob
import import statistics as s
#tokenize word in sentence
a = TextBlob("""Thanks, I'll have a read!""")
print a.words
WordList(['Thanks', 'I', "'ll", 'have', 'a', 'read'])
#get polarity of every word
for i in a.words:
print( a.sentiment.polarity)
0.25
0.25
0.25
0.25
0.25
0.25
#calculating the mean of the scores
c=[]
for i in a.words:
c.append(a.sentiment.polarity)
d = s.mean(c)
print (d)
0.25
How do I apply the a.words
to every row of dataframe column for sentence?
New df:
text score
0 Thanks, I’ll have a read! 0.25
1 Am I too late 0.24
closet I come is that I can get polarity of every sentence using this function on the dataframe:
def sentiment_calc(text):
try:
return TextBlob(text).sentiment.polarity
except:
return None
df_sentences['sentiment'] = df_sentences['text'].apply(sentiment_calc)
Thank you in advance.