0

I'm analyzing the full text of a book using Textblob, aggregating the mood of the chapter by analyzing the sentences individually. I have a script which converts the chapters into lists of individual sentences, but I can't figure out a way to pass these list objects as strings to the Naive Bayes Analyzer since it only expects string inputs.

So far I've tried only passing the entire list as the argument, but it has always given me the same error.

 TypeError: The `text` argument passed to `__init__(text)` must be a string, 
 not <class 'list'>

This is the code I have:

from textblob import TextBlob
from textblob.sentiments import NaiveBayesAnalyzer
blob = TextBlob("This is a horrible idea.", analyzer=NaiveBayesAnalyzer())
blob.sentiment
print(blob.sentiment)

My list looks something like this:

sentences = ['Maria was five years old the first time she heard the word 
hello.\n', 'It happened on a Thursday.\n',]

How do I alter this code to take in the entire list of sentences and pass the output as a dataframe? If possible, I would like something like this:

                                         Line          Polarity Subjectivity Classification
0    Mariam was five years old the first time sh      0.175000   0.266667   Pos                                                 
1    It happened on a Thursday.                       0.000000   0.000000 Neu
Aman
  • 387
  • 8
  • 33
  • Unless `textblob` allows you to pass in anything but a string, you'll have to manually map it over your strings. – Mateen Ulhaq Nov 08 '19 at 08:05
  • You mean manually inputting every string? That would be quite impossible since there are thousands such lines and readings to be noted. Is there a way to use a loop which runs over the list, entering strings into textblob one by one? – Aman Nov 08 '19 at 08:11
  • Try the solution here https://stackoverflow.com/questions/43485469/apply-textblob-in-for-each-row-of-a-dataframe – Jimmy Nov 08 '19 at 08:55

1 Answers1

0

Did you mean to construct a dataframe something like this. Or atleast that is what I understood from you question. I am assuming you have list of sentences and i am joining them into a paragraph before running the analyzer on it.

import pandas as pd
from textblob import TextBlob

from textblob.sentiments import NaiveBayesAnalyzer

df = pd.DataFrame(columns = ['Line','Polarity', 'Subjectivity' ,'Classification'])
sentences = ['Maria was five years old the first time she heard the word hello.\n', 'It happened on a Thursday.\n',]

blob = TextBlob("".join(sentences),analyzer=NaiveBayesAnalyzer())
for sentence in blob.sentences:
    df.loc[len(df)] = [str(sentence),sentence.polarity, sentence.subjectivity,sentence.sentiment.classification]
print(df)
nitin3685
  • 825
  • 1
  • 9
  • 20