|
| 1 | +#importing dependencies |
| 2 | + |
| 3 | +from textblob import TextBlob |
| 4 | +import sys,tweepy |
| 5 | + |
| 6 | + |
| 7 | +# function to calculate percentage |
| 8 | +def percentage(part, whole): |
| 9 | + return 100 * float(part)/float(whole) |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +#BMP mapping |
| 14 | +non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd) |
| 15 | + |
| 16 | + |
| 17 | +#authenticating |
| 18 | +consumerKey = '' |
| 19 | +consumerSecret = '' |
| 20 | +accessToken = '' |
| 21 | +accessTokenSecret = '' |
| 22 | +auth = tweepy.OAuthHandler(consumerKey, consumerSecret) |
| 23 | +auth.set_access_token(accessToken, accessTokenSecret) |
| 24 | +api = tweepy.API(auth) |
| 25 | + |
| 26 | + |
| 27 | +#input for term to be searched and how many tweets to search |
| 28 | +searchTerm = input("Enter Keyword/Tag to search about: ") |
| 29 | +NoOfTerms = int(input("Enter how many tweets to search: ")) |
| 30 | + |
| 31 | + |
| 32 | +#searching for tweets |
| 33 | +tweets = tweepy.Cursor(api.search, q=searchTerm).items(NoOfTerms) |
| 34 | + |
| 35 | + |
| 36 | +#creating some variables to store info |
| 37 | +polarity = 0 |
| 38 | +positive = 0 |
| 39 | +wpositive = 0 |
| 40 | +spositive = 0 |
| 41 | +negative = 0 |
| 42 | +wnegative = 0 |
| 43 | +snegative = 0 |
| 44 | +neutral = 0 |
| 45 | + |
| 46 | + |
| 47 | +#iterating through tweets |
| 48 | +for tweet in tweets: |
| 49 | + #print (tweet.text.translate(non_bmp_map)) #print tweet's text |
| 50 | + analysis = TextBlob(tweet.text) |
| 51 | + #print(analysis.sentiment) #print tweet's polarity |
| 52 | + polarity+=analysis.sentiment.polarity #adding up polarities to find the average later |
| 53 | + |
| 54 | + if (analysis.sentiment.polarity==0): #adding reaction of how people are reacting to find average later |
| 55 | + neutral+=1 |
| 56 | + elif (analysis.sentiment.polarity>0 and analysis.sentiment.polarity<=0.3): |
| 57 | + wpositive+=1 |
| 58 | + elif (analysis.sentiment.polarity>0.3 and analysis.sentiment.polarity<=0.6): |
| 59 | + positive+=1 |
| 60 | + elif (analysis.sentiment.polarity>0.6 and analysis.sentiment.polarity<=1): |
| 61 | + spositive+=1 |
| 62 | + elif (analysis.sentiment.polarity>-0.3 and analysis.sentiment.polarity<=0): |
| 63 | + wnegative+=1 |
| 64 | + elif (analysis.sentiment.polarity>-0.6 and analysis.sentiment.polarity<=-0.3): |
| 65 | + negative+=1 |
| 66 | + elif (analysis.sentiment.polarity>-1 and analysis.sentiment.polarity<=-0.6): |
| 67 | + snegative+=1 |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +#finding average of how people are reacting |
| 72 | +positive = percentage(positive, NoOfTerms) |
| 73 | +wpositive = percentage(wpositive, NoOfTerms) |
| 74 | +spositive = percentage(spositive, NoOfTerms) |
| 75 | +negative = percentage(negative, NoOfTerms) |
| 76 | +wnegative = percentage(wnegative, NoOfTerms) |
| 77 | +snegative = percentage(snegative, NoOfTerms) |
| 78 | +neutral = percentage(neutral, NoOfTerms) |
| 79 | + |
| 80 | + |
| 81 | +#finding average reaction |
| 82 | +polarity = polarity/NoOfTerms |
| 83 | + |
| 84 | + |
| 85 | +#printing out data |
| 86 | +print("How people are reacting on "+searchTerm+" by analyzing "+str(NoOfTerms)+" tweets.") |
| 87 | +print() |
| 88 | +print("General Report: ") |
| 89 | + |
| 90 | +if (polarity==0): |
| 91 | + print("Neutral") |
| 92 | +elif (polarity>0 and polarity<=0.3): |
| 93 | + print("Weakly Positive") |
| 94 | +elif (polarity>0.3 and polarity<=0.6): |
| 95 | + print("Positive") |
| 96 | +elif (polarity>0.6 and polarity<=1): |
| 97 | + print("Strongly Positive") |
| 98 | +elif (polarity>-0.3 and polarity<=0): |
| 99 | + print("Weakly Negative") |
| 100 | +elif (polarity>-0.6 and polarity<=-0.3): |
| 101 | + print("Negative") |
| 102 | +elif (polarity>-1 and polarity<=-0.6): |
| 103 | + print("Strongly Negative") |
| 104 | + |
| 105 | + |
| 106 | +print() |
| 107 | +print("Detailed Report: ") |
| 108 | +print( str(positive) + "% people thought it was positive") |
| 109 | +print( str(wpositive) + "% people thought it was weakly positive") |
| 110 | +print( str(spositive) + "% people thought it was strongly positive") |
| 111 | +print( str(negative) + "% people thought it was negative") |
| 112 | +print( str(wnegative) + "% people thought it was weakly negative") |
| 113 | +print( str(snegative) + "% people thought it was strongly negative") |
| 114 | +print( str(neutral) + "% people thought it was neutral") |
| 115 | + |
| 116 | + |
0 commit comments