├── .gitattributes ├── License.txt ├── README.md ├── main.py └── setup.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto -------------------------------------------------------------------------------- /License.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Muhammad Ali Zia 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Twitter-Sentiment-Analysis 2 | 3 | This script can tell you the sentiments of people regarding to any events happening in the world by analyzing tweets related to that event. It will search for tweets about any topic and analyze each tweet to see how positive or negative it's emotion is. You might want to check out this complete text and video based detailed [tutorial link](http://www.letscodepro.com/Twitter-Sentiment-Analysis/) 4 | 5 | ![alt text](http://www.letscodepro.com/wp-content/uploads/2017/09/TwitterSentimentAnalysis.png) 6 | 7 | 8 | ## Getting Started 9 | 10 | First of all login from your Twitter account and goto [Twitter Apps](https://apps.twitter.com/). Create a new app ([How to create twitter app](http://www.letscodepro.com/twitter-sentiment-analysis/)) and goto __Keys and access tokens__ and copy Consumer Key, Consumer Secret, Access Token and Access Token Secret. We will need them later. 11 | 12 | ### Installation 13 | 14 | Download or Clone the repo, Navigate to the directory containing the files and run 15 | ``` 16 | python setup.py install 17 | ``` 18 | or if you have different versions of python installed then 19 | ``` 20 | python3 setup.py install 21 | ``` 22 | to install the dependencies. 23 | 24 | 25 | ### Usage 26 | 27 | Once you have created an app on twitter and installed all the dependencies by running __setup.py__, open main.py and paste your Consumer Key, Consumer Secret, Access Token and Access Token Secret. After that save and run the script. You will be prompted to enter the keyword/hashtag you want to analyze and the number of tweets you want to analyze. Once the analysis is completed, a pie chart will be generated disclosing the results of analysis. 28 | 29 | ## Built With 30 | 31 | * Python 3.6 32 | * tweepy 33 | * textblob 34 | * matplotlib 35 | 36 | ## Contributing 37 | 38 | 1. Fork it 39 | 2. Create your feature branch: git checkout -b my-new-feature 40 | 3. Commit your changes: git commit -am 'Add some feature' 41 | 4. Push to the branch: git push origin my-new-feature 42 | 5. Submit a pull request 43 | 44 | ## Authors 45 | 46 | Muhammad Ali Zia 47 | 48 | ## License 49 | 50 | This project is licensed under the MIT License - see the [LICENSE.md](https://github.com/the-javapocalypse/Twitter-Sentiment-Analysis/blob/master/License.txt) file for details 51 | 52 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import sys,tweepy,csv,re 2 | from textblob import TextBlob 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | class SentimentAnalysis: 7 | 8 | def __init__(self): 9 | self.tweets = [] 10 | self.tweetText = [] 11 | 12 | def DownloadData(self): 13 | # authenticating 14 | consumerKey = 'your key here' 15 | consumerSecret = 'your key here' 16 | accessToken = 'your key here' 17 | accessTokenSecret = 'your key here' 18 | auth = tweepy.OAuthHandler(consumerKey, consumerSecret) 19 | auth.set_access_token(accessToken, accessTokenSecret) 20 | api = tweepy.API(auth) 21 | 22 | # input for term to be searched and how many tweets to search 23 | searchTerm = input("Enter Keyword/Tag to search about: ") 24 | NoOfTerms = int(input("Enter how many tweets to search: ")) 25 | 26 | # searching for tweets 27 | self.tweets = tweepy.Cursor(api.search, q=searchTerm, lang = "en").items(NoOfTerms) 28 | 29 | # Open/create a file to append data to 30 | csvFile = open('result.csv', 'a') 31 | 32 | # Use csv writer 33 | csvWriter = csv.writer(csvFile) 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 fetched 48 | for tweet in self.tweets: 49 | #Append to temp so that we can store in csv later. I use encode UTF-8 50 | self.tweetText.append(self.cleanTweet(tweet.text).encode('utf-8')) 51 | # print (tweet.text.translate(non_bmp_map)) #print tweet's text 52 | analysis = TextBlob(tweet.text) 53 | # print(analysis.sentiment) # print tweet's polarity 54 | polarity += analysis.sentiment.polarity # adding up polarities to find the average later 55 | 56 | if (analysis.sentiment.polarity == 0): # adding reaction of how people are reacting to find average later 57 | neutral += 1 58 | elif (analysis.sentiment.polarity > 0 and analysis.sentiment.polarity <= 0.3): 59 | wpositive += 1 60 | elif (analysis.sentiment.polarity > 0.3 and analysis.sentiment.polarity <= 0.6): 61 | positive += 1 62 | elif (analysis.sentiment.polarity > 0.6 and analysis.sentiment.polarity <= 1): 63 | spositive += 1 64 | elif (analysis.sentiment.polarity > -0.3 and analysis.sentiment.polarity <= 0): 65 | wnegative += 1 66 | elif (analysis.sentiment.polarity > -0.6 and analysis.sentiment.polarity <= -0.3): 67 | negative += 1 68 | elif (analysis.sentiment.polarity > -1 and analysis.sentiment.polarity <= -0.6): 69 | snegative += 1 70 | 71 | 72 | # Write to csv and close csv file 73 | csvWriter.writerow(self.tweetText) 74 | csvFile.close() 75 | 76 | # finding average of how people are reacting 77 | positive = self.percentage(positive, NoOfTerms) 78 | wpositive = self.percentage(wpositive, NoOfTerms) 79 | spositive = self.percentage(spositive, NoOfTerms) 80 | negative = self.percentage(negative, NoOfTerms) 81 | wnegative = self.percentage(wnegative, NoOfTerms) 82 | snegative = self.percentage(snegative, NoOfTerms) 83 | neutral = self.percentage(neutral, NoOfTerms) 84 | 85 | # finding average reaction 86 | polarity = polarity / NoOfTerms 87 | 88 | # printing out data 89 | print("How people are reacting on " + searchTerm + " by analyzing " + str(NoOfTerms) + " tweets.") 90 | print() 91 | print("General Report: ") 92 | 93 | if (polarity == 0): 94 | print("Neutral") 95 | elif (polarity > 0 and polarity <= 0.3): 96 | print("Weakly Positive") 97 | elif (polarity > 0.3 and polarity <= 0.6): 98 | print("Positive") 99 | elif (polarity > 0.6 and polarity <= 1): 100 | print("Strongly Positive") 101 | elif (polarity > -0.3 and polarity <= 0): 102 | print("Weakly Negative") 103 | elif (polarity > -0.6 and polarity <= -0.3): 104 | print("Negative") 105 | elif (polarity > -1 and polarity <= -0.6): 106 | print("Strongly Negative") 107 | 108 | print() 109 | print("Detailed Report: ") 110 | print(str(positive) + "% people thought it was positive") 111 | print(str(wpositive) + "% people thought it was weakly positive") 112 | print(str(spositive) + "% people thought it was strongly positive") 113 | print(str(negative) + "% people thought it was negative") 114 | print(str(wnegative) + "% people thought it was weakly negative") 115 | print(str(snegative) + "% people thought it was strongly negative") 116 | print(str(neutral) + "% people thought it was neutral") 117 | 118 | self.plotPieChart(positive, wpositive, spositive, negative, wnegative, snegative, neutral, searchTerm, NoOfTerms) 119 | 120 | 121 | def cleanTweet(self, tweet): 122 | # Remove Links, Special Characters etc from tweet 123 | return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t]) | (\w +:\ / \ / \S +)", " ", tweet).split()) 124 | 125 | # function to calculate percentage 126 | def percentage(self, part, whole): 127 | temp = 100 * float(part) / float(whole) 128 | return format(temp, '.2f') 129 | 130 | def plotPieChart(self, positive, wpositive, spositive, negative, wnegative, snegative, neutral, searchTerm, noOfSearchTerms): 131 | labels = ['Positive [' + str(positive) + '%]', 'Weakly Positive [' + str(wpositive) + '%]','Strongly Positive [' + str(spositive) + '%]', 'Neutral [' + str(neutral) + '%]', 132 | 'Negative [' + str(negative) + '%]', 'Weakly Negative [' + str(wnegative) + '%]', 'Strongly Negative [' + str(snegative) + '%]'] 133 | sizes = [positive, wpositive, spositive, neutral, negative, wnegative, snegative] 134 | colors = ['yellowgreen','lightgreen','darkgreen', 'gold', 'red','lightsalmon','darkred'] 135 | patches, texts = plt.pie(sizes, colors=colors, startangle=90) 136 | plt.legend(patches, labels, loc="best") 137 | plt.title('How people are reacting on ' + searchTerm + ' by analyzing ' + str(noOfSearchTerms) + ' Tweets.') 138 | plt.axis('equal') 139 | plt.tight_layout() 140 | plt.show() 141 | 142 | 143 | 144 | if __name__== "__main__": 145 | sa = SentimentAnalysis() 146 | sa.DownloadData() 147 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | from codecs import open 3 | from os import path 4 | 5 | here = path.abspath(path.dirname(__file__)) 6 | 7 | with open(path.join(here, 'README.md'), encoding='utf-8') as f: 8 | long_description = f.read() 9 | 10 | setup( 11 | name='Twitter Sentiment Analysis', 12 | version='1.3.0', 13 | 14 | description='This script can tell you the sentiments of people regarding to any events happening in the world by analyzing tweets related to that event. It will search for tweets about any topic and analyze each tweet to see how positive or negative it's emotion is.', 15 | long_description=long_description, 16 | 17 | 18 | url='https://github.com/the-javapocalypse/Twitter-Sentiment-Analysis', 19 | 20 | 21 | author='Muhammad Ali Zia', 22 | author_email='muhammad.17ali@gmail.com', 23 | 24 | 25 | classifiers=[ 26 | 27 | 'Development Status :: 4 - Beta', 28 | 29 | 'Programming Language :: Python :: 3', 30 | 'Programming Language :: Python :: 3.3', 31 | 'Programming Language :: Python :: 3.4', 32 | 'Programming Language :: Python :: 3.5', 33 | 'Programming Language :: Python :: 3.6', 34 | ], 35 | 36 | 37 | keywords='python twitter sentiment-analysis textblob nlp tweepy nltk', 38 | 39 | 40 | install_requires=['tweepy','textblob','matplotlib'], 41 | 42 | 43 | ) 44 | --------------------------------------------------------------------------------