Python Forum
Sentiment Analysis with NLTK Vader - Writing data in one row
Thread Rating:
  • 3 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sentiment Analysis with NLTK Vader - Writing data in one row
#1
Hello python experts,

for an university project I want to do an sentiment analysis of twitter tweets with the NLTK package. Therefore, my code looks this:

sid = SentimentIntensityAnalyzer()
for sentence in tweets.Mess:
        print(sentence)
        ss = sid.polarity_scores(sentence)
        for k in sorted(ss):
            print('{0}: {1}, '.format(k, ss[k]), end='')
            print()
            with open('OutputSentiment.csv','a') as outfile:
                outfile.write(str(sentence) + ',' + str(k) + ',' + str(ss[k]) + '\n')
The results in the output file look this:

[Image: sUzLk]

Unfortunatly, I get four rows for every tweet instead of only one. What do I have to do to fix this and is it possible to get the results of the sentiment analysis in different columns? The structure of the file should look like this:

Tweet,Compound,Negativ,Neutral,Possitive 

Thanks in advance!
Reply
#2
Have you checked the real contents of your data? If there are terminating linefeeds, you can remove them with trim().
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Movie lens data analysis sekhar_desiraju 4 6,876 Nov-29-2020, 01:57 AM
Last Post: kakkarshivam
  RE: Data isn't writing to the Database with Django and SQL Lite card51shor 15 5,036 Sep-16-2020, 08:27 AM
Last Post: metulburr
  Creating Self Learning Sentiment Dictionary esanjaysaini 1 1,666 Jul-26-2019, 03:27 PM
Last Post: ThomasL
  Data Analysis darkapostle 1 3,033 Sep-28-2018, 12:55 AM
Last Post: ichabod801
  python - Custom POS tagging with NLTK desul 0 4,027 May-01-2017, 09:38 AM
Last Post: desul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020