TweetsListener.py provides classes to perform tweets collection and sentiment analysis
There are several classes based on StreamListener from tweepy
- TweetsListener is a base class to listen tweets only and stop listening when a specific number of tweets collected or time listening is out. If both parameters are 0 then it can run forever.
- TWeetSentimentAnalyzed analyzes the tweet emotions based on emoticons in the tweet if any or applies NLP procedures from TextBlob package to perform sentiment analysis
- TWeetsToFile saves collected tweets in a csv file. In addition to the limit number of tweets or maximum time collection reached, the collection of tweets can be stopped when the file reaches a limit. The class can split the data in several files, based on the tweets number or an individual file size.
- In addition to the tweet itself, TWeetsCoordinatesToFile saves GEO info in a separate file. "_geo_" is added to the file name The GEO file is in csv format, many-to-one relations to the tweets file All files total size is taken into account to stop tweets collection at file_size_limit Only tweets file is splitted in multiply files
- TWeets class inherits TWeetsCoordinatesToFile and TWeetSentimentAnalyzed methods. It saves the tweet, it's sentiments in a csv file and the coordinates in a separate file
- TWeetsTotals saves only total sentiments daily in a csv file. There is also hourly data but they are overwritten
The script has a usage example of the classes
To run the script you need to have a Twitter Developer Account.
The usage example expects the account details and other parameters in a project resources yaml-formatted file.
To collect tweets, use the following command, where search_term is your search term and ProjectResources.yml is optional, project resources file in yaml format. If it is not provided, ProjectResources.yml is used by default
python TweetsListener.py search_term ProjectResources.ymlTo use a multi-word term, enclose it in quotation marks