title: Twitter sentiment analysis using python
body: <p>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.</p>
  
  <pre><code>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 &lt;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&gt;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'])
  #
  </code></pre>