2

I am trying to do a sentiment analysis from a csv file where each row has a sentence.

Reprex:

print(your_list)
[['Patience and Kindness and I know they truly love and care for animals, my dog also enjoys the events like seeing Santa and the Easter Bunny'], ['They are so sweet to my pets and try to fit them into the schedule when needed'], ['they call and check on our pet a day or 2 after visit make sure we fully understand treatment before we leave'], ['every member of the staff understands how our pets are our family; we never feel rushed and always have or questions answered, and are given reassurance if and when needed; they are compassionate and kind, respectful and very caring'], ['They made it a very peaceful experience when we had to put our pug to sleep '], ['They interact with my dogs and you can see the care they have for them.'], ['they make every effort to accomodate us']    


    from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

        import csv
        with open('Before.csv', "r", errors='ignore') as f:
            reader = csv.reader(f)
        your_list = list(reader)

    print(your_list)

    analyser = SentimentIntensityAnalyzer()

    def print_sentiment_scores(sentence):
        snt = analyser.polarity_scores(sentence)
        print("{:-<40} {}".format(sentence, str(snt)))

    print_sentiment_scores(your_list)

However, I receive the below error:

analyser = SentimentIntensityAnalyzer()

def print_sentiment_scores(sentence):
    snt = analyser.polarity_scores(sentence)
    print("{:-<40} {}".format(sentence, str(snt)))


print_sentiment_scores(your_list)

Traceback (most recent call last):

  File "<ipython-input-24-a7a32425d261>", line 8, in <module>
    print_sentiment_scores(your_list)

  File "<ipython-input-24-a7a32425d261>", line 4, in print_sentiment_scores
    snt = analyser.polarity_scores(sentence)

  File "C:\Users\abc\AppData\Local\Continuum\anaconda3\lib\site-packages\vaderSentiment\vaderSentiment.py", line 248, in polarity_scores
    text_token_list = text.split()

AttributeError: 'list' object has no attribute 'split'

.split(" ") function on your_list is not helping

  • change the last line to `[print_sentiment_scores(line) for line in your_list]`, that way you perform operation on the string and not the list – Tobey Aug 20 '19 at 15:48
  • 1
    @Tobey Do NOT use list comprehension unless you plan to use the resulting list. – Error - Syntactical Remorse Aug 20 '19 at 15:53
  • 1
    Other than being a waste of memory... why not (besides the fact that it is ugly)? It just generates a list full of `None` which is then thrown away because it wasn't assigned to anything – Gillespie Aug 20 '19 at 16:17

3 Answers3

3

Vader's 'polarity_scores(sentence)' takes a string parameter, not a list.

Your code should be:

analyser = SentimentIntensityAnalyzer()

def print_sentiment_scores(alist):
    for aSentence in alist: 
      aSnt = analyser.polarity_scores(aSentence[0])
      print(str(aSnt))


print_sentiment_scores(your_list)

So I finally got this to work with the following code and csv:

#!/usr/bin/python3
import csv
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

with open('Before.csv', "r", errors='ignore') as f:
    reader = csv.reader(f)
    your_list = list(reader)

analyser = SentimentIntensityAnalyzer()

def print_sentiment_scores(alist):
    for aSentence in alist: 
      aSnt = analyser.polarity_scores(aSentence[0])
      print(str(aSnt))

print_sentiment_scores(your_list)

Associated .csv's contents:

['Patience and Kindness and I know they truly love and care for 
animals, my dog also enjoys the events like seeing Santa and the Easter 
Bunny'], ['They are so sweet to my pets and try to fit them into the 
schedule when needed'], ['they call and check on our pet a day or 2 after 
visit make sure we fully understand treatment before we leave'], ['every 
member of the staff understands how our pets are our family; we never feel 
rushed and always have or questions answered, and are given reassurance if 
and when needed; they are compassionate and kind, respectful and very 
caring']

Output: Final Output

If you want the output strings to be formatted, please do some research on string-formatting. Or post another question on SO, if you cannot find an answer.

Matthew E. Miller
  • 557
  • 1
  • 5
  • 13
  • @Jay Taggert what is the output when you print(your_list) – Matthew E. Miller Aug 20 '19 at 16:09
  • print(your_list) [['Patience and Kindness and I know they truly love and care for animals, my dog also enjoys the events like seeing Santa and the Easter Bunny'], ['They are so sweet to my pets and try to fit them into the schedule when needed'], ['they call and check on our pet a day or 2 after visit make sure we fully understand treatment before we leave'], ['every member of the staff understands how our pets are our family; we never feel rushed and always have or questions answered, and are given reassurance if and when needed; they are compassionate and kind, respectful and very caring'] –  Aug 20 '19 at 16:10
  • @JayTaggert there is something wrong with the way your_list was initialized. I will post correct code to set your_list. Your list should have printed with another ']' at the end. – Matthew E. Miller Aug 20 '19 at 16:15
  • `aSentence` is still a list because `your_list` is a list of lists because this is a CSV. `analyser.polarity_scores(aSentence[0])` should work. – Alex Hall Aug 20 '19 at 16:19
  • I want your list to pickup a csv file or word doc that has the text in it. I dont want to manually paste it for your_list. –  Aug 20 '19 at 16:19
  • @AlexHall ohh... Does this edited version work? I don't access to a development environment. – Matthew E. Miller Aug 20 '19 at 16:19
  • @AlexHall analyser.polarity_scores(aSentence[0]) gives TypeError: unsupported format string passed to list.__format__ –  Aug 20 '19 at 16:23
  • @JayTaggert are you sure that isn't an issue with the print.format() statement, try commenting out. – Matthew E. Miller Aug 20 '19 at 16:25
  • @MatthewE.Miller I commented it out. but there is no output when i run it –  Aug 20 '19 at 16:29
  • 1
    @JayTaggert well ofcourse... the point of commenting out is to see where the error persists from. So, you commented it out and now there is no error? so we need to modify the print statement. Give me few minutes to setup my dev enviornment. – Matthew E. Miller Aug 20 '19 at 16:31
  • Yes theres no error after commenting out print("{:-<40} {}".format(aSentence, str(aSnt))) –  Aug 20 '19 at 16:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198196/discussion-between-matthew-e-miller-and-jay-taggert). – Matthew E. Miller Aug 20 '19 at 16:57
  • mathew can you please say how i can convert the output to a csv f = open('After.csv','w') f.write(str(aSnt)) #Give your csv text here. ## Python will convert \n to os.linesep f.close() did not work –  Aug 20 '19 at 17:21
0

SentimentIntensityAnalyzer.polarity_scores(text) takes as argument text/str. you pass list of lists. Probably you want to pass the whole content of the file as single text or pass each sentence separately, but not list of lists.

buran
  • 13,682
  • 10
  • 36
  • 61
0

The problem lies in the call to the function 'print_sentiment_scores'. The parameter passed is a list. Based on your requirement, I think you can pass the elements of the list to get the split. Split() works on string type as per the documentation here