Advertisement
Guest User

Untitled

a guest
May 20th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. import tweepy
  2. import csv
  3. import json
  4. from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
  5. import string
  6. from matplotlib import pyplot as plt
  7. import numpy as np
  8. import math
  9. import statistics as stats
  10.  
  11. with open('twitter_credentials.json') as cred_data:
  12. info = json.load(cred_data)
  13. consumer_key = info['CONSUMER_KEY']
  14. consumer_secret = info['CONSUMER_SECRET']
  15. access_key = info['ACCESS_KEY']
  16. access_secret = info['ACCESS_SECRET']
  17.  
  18. auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  19. api = tweepy.API(auth)
  20.  
  21. maximum_number_of_tweets_to_be_extracted = \
  22. int(input('Enter the number of tweets that you want to extract - '))
  23.  
  24. hashtag = input('Enter the keyword(s) you want to scrape - ').replace(", ", " OR ")
  25. lines = [["Tweet", "Score", "Location", "User"]]
  26. analyzer = SentimentIntensityAnalyzer()
  27. scores = []
  28.  
  29. for tweet in tweepy.Cursor(api.search, q= hashtag, geocode="37.752721,-122.327076,30mi", rpp=100).items(maximum_number_of_tweets_to_be_extracted):
  30. loc = tweet.user.location
  31. user = tweet.user.name
  32. tweet = str(tweet.text.encode("utf-8")).strip("'b")
  33. tweet = tweet.translate(string.punctuation)
  34. line = [tweet]
  35. score = analyzer.polarity_scores(tweet)["compound"]
  36. scores.append(score)
  37. line.append(score)
  38. line.append(loc)
  39. line.append(user)
  40. lines.append(line)
  41.  
  42.  
  43. with open(hashtag+'.csv', 'w') as csvFile:
  44. writer = csv.writer(csvFile)
  45. writer.writerows(lines)
  46.  
  47. csvFile.close()
  48.  
  49. plt.xlim([-1, 1])
  50. plt.hist(scores, bins=20, alpha=0.5)
  51. plt.title('Sentiment Analysis Score Distribution for keyword(s) ' + hashtag.replace(" OR ", ", "))
  52. plt.xlabel('Score Interval')
  53. plt.ylabel('Count')
  54.  
  55. print("Average Score: " + str(stats.mean(scores)))
  56. print("Median Score: " + str(stats.median(scores)))
  57. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement