简体   繁体   中英

Twitter sentiment analysis using python

I'm trying to create a dataset of food tweets classified as healthy and unhealthy .I wrote two scripts which are streaming the tweets having keywords I specified and then I applied sentiment analysis on it so it will be easy for me to classify them into healthy and unhealthy but the sentiment analysis of text blob is not that much satisfying and the script also fetching the tweets that don't contain those keywords. If anyone knows food tweets dataset so that would be ver helpful.

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
import os
from textblob import TextBlob
import json

ck`enter code here`ey = "xxx"
csecret = "xx"
atok`enter code here`en = "xx"
asecret = "xx"


class listener(StreamListener):
    def on_data(self, data):
        try:

            tweet = data.split(',"text":"')[1].split('","source')[0]
            print tweet
            saveThis = str(time.time()) + '::' + tweet
            #tweet = data.split(',"text":"')[1]
            analysis=TextBlob(tweet)
            polarity=analysis.sentiment.polarity
            print(polarity)
            if polarity <0 :
                #username = data["user"]["screen_name"]

                saveThis = tweet + '::' + str(polarity)
                out = open('out1.csv', 'a')
                out.write(saveThis)
                out.write('\n')
                out.close()
                #return (True)
                #saveThis = str(time.time()) + '::' + tweet + '::' + str(polarity)
                #saveFile = open('unhealthytweet1.json', 'a')
                #saveFile.write(saveThis)
                #saveFile.write('\n')
                #saveFile.close()
                return (True)
            elif polarity>0 :
                #username = data["user"]["screen_name"]

                 #username, " :: ",
                saveThis =tweet + '::' + str(polarity)
                out = open('out2.csv', 'a')
                out.write(saveThis)
                out.write('\n')
                out.close()
                # return (True)
                # saveThis = str(time.time()) + '::' + tweet + '::' + str(polarity)
                # saveFile = open('unhealthytweet1.json', 'a')
                # saveFile.write(saveThis)
                # saveFile.write('\n')
                # saveFile.close()
                return (True)
        except BaseException, e:
            print 'failed on_date,', str(e)
            time.sleep(5)
            pass


auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

twitterStream = Stream(auth, listener())
twitterStream.filter(track=["vegetable soup", "fruits", "green tea", "vegetables", "fresh juice", "salad","sea food"], languages=['en'])
#

I'm really new to programming but I had similar problems to you. What I found and this may be incorrect but it works with my programme​, is apply a raw input for the keyword passing that variable through the listener then running an if in data['text'] <¬¬ I also referenced this near the top as a variable with a encoding('utf-8').

if the keywords aren't in it then just pass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM