2

For a current project, I am planning to perform a sentiment analysis for a number of word combinations with TextBlob.

When running the sentiment analysis line polarity = common_words.sentiment.polarity and calling the results with print(i, word, freq, polarity), I am receiving the following error message:

polarity = common_words.sentiment.polarity
AttributeError: 'list' object has no attribute 'sentiment'

Is there any smart tweak to get this running? The corresponding code section looks like this:

for i in ['Text_Pro','Text_Con','Text_Main']:
    common_words = get_top_n_trigram(df[i], 150)
    polarity = common_words.sentiment.polarity
    for word, freq in common_words:
        print(i, word, freq, polarity)

Edit: Please find below the full solution for the situation (in accordance with discussions with user leopardxpreload):

for i in ['Text_Pro','Text_Con','Text_Main']:
    common_words = str(get_top_n_trigram(df[i], 150))
    polarity_list = str([TextBlob(i).sentiment.polarity for i in common_words])
    for element in polarity_list:
        print(i, element)
    for word, freq in common_words:
        print(i, word, freq)
Malte Susen
  • 767
  • 3
  • 13
  • 36

1 Answers1

1

It seems like you are trying to use the TextBlob calls on a list and not a TextBlob object.

for i in ['Text_Pro','Text_Con','Text_Main']:
    common_words = get_top_n_trigram(df[i], 150)
    # Not sure what is in common_words, but it needs to be a string
    polarity = TextBlob(common_words).sentiment.polarity
    for word, freq in common_words:
        print(i, word, freq, polarity)

If common_words is a list you might need to add:

polarity_list = [TextBlob(i).sentiment.polarity for i in common_words]

Possible copy-paste solution:

for i in ['Text_Pro','Text_Con','Text_Main']:
    common_words = get_top_n_trigram(df[i], 150)
    polarity_list = [TextBlob(i).sentiment.polarity for i in common_words]
    for element in polarity_list:
        print(i, element)
leopardxpreload
  • 767
  • 5
  • 17
  • That's a very good thought, many thanks. I am however receiving the following message now `polarity = TextBlob(common_words).sentiment.polarity, File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/textblob/blob.py", line 370, in __init__ 'must be a string, not {0}'.format(type(text))), TypeError: The 'text' argument passed to __init__(text) must be a string, not ` – Malte Susen Jun 10 '20 at 09:25
  • 1
    Yes, so you have a list and the `TextBlob` function *only* accepts a string -- Do a `print(common_words)` after initializing the variable and you should see an output with square brackets. – leopardxpreload Jun 10 '20 at 09:28
  • 1
    Also, I think trying `for word, freq in common_words` will cause an 'unpacking issue' - You wil need to do a `for element in comon_words` and then do indexing: `element[1]` .. etc. – leopardxpreload Jun 10 '20 at 09:33
  • Ok thanks, let me see where I get with these points. For now (after including the `print(common_words) point above`), I am still seeing the error `TypeError: The 'text' argument passed to '__init__(text)' must be a string, not ` – Malte Susen Jun 10 '20 at 09:34
  • 1
    `#` comment out everything below `common_words = ...` – leopardxpreload Jun 10 '20 at 09:35
  • Yup that makes the scrip run again but lacks the sentiment analysis. Tough call as it seems... – Malte Susen Jun 10 '20 at 09:37
  • 1
    Yes but whats the print-out? – leopardxpreload Jun 10 '20 at 09:38
  • Text_Pro work life balance 100, Text_Pro good work life 23, Text_Pro great place work 14, Text_Pro great work life 10, Text_Pro life balance good 9, Text_Pro flexible working hours 9 etc - a classical word count output – Malte Susen Jun 10 '20 at 09:40
  • Saw your updated code and tried that. It yields the following message `TypeError: The 'text' argument passed to '__init__(text)' must be a string, not ` – Malte Susen Jun 10 '20 at 09:44
  • 1
    I made it work by defining common_words and the polarity_list as strings. Will highlight this in the question text for other users. Thanks again for your help. – Malte Susen Jun 10 '20 at 11:57
  • 1
    @M.S. Glad to hear you got his working, my pleasure - Thanks – leopardxpreload Jun 10 '20 at 22:35