The goal of twitterAnalysis is to assess a twitter user’s character based on their recent tweets
Summary
Twitter is a popular social media app with over 1 billion user accounts. While a diversity of users is a strength, some individuals have concerns with the prevalence of “troll” accounts and individuals who exhibit unconstructive tone and diction whom they deem not worth engaging with. The package twitterAnalysis is intended to provide insight into a twitter user based on their tweet history in effort to determine if an account is worth engaging with. The package provides an easy to use interface for determining the general sentiment expressed by a user.
Installation
You can install the development version of twitterAnalysis from GitHub with:
# install.packages("devtools")
devtools::install_github("UBC-MDS/twitterAnalysis")
To get twitter developer account, please find follow instructions and apply one at https://developer.twitter.com/en
- Log-in to Twitter and verify your email address. (Note that the email and phone number verification from your Twitter account may be needed to apply for a developer account, review on the Twitter help center: email address confirmation or add phone number.)
- Click sign up at developer.twitter.com to enter your developer account name, location and use case details
- Review and accept the developer agreement and submit
- Check your email to verify your developer account. Look for an email from developer-accounts@twitter.com that has the subject line: “Verify your Twitter Developer Account” Note: the developer-accounts@twitter.com email is not available for inbound requests.
- You should now have access to the Developer Portal to create a new App and Project with Essential access, or will need to continue your application with Elevated access If you apply for Elevated access (or Academic Research access) please continue to check your verified email for information about your application.
Example
Here is an example analysis using 300 recent tweets from a user.
library(twitterAnalysis)
#retrieve 300 tweets from @elonmusk
tokens <- user_info("YOUR_consumer_key",
"YOUR_consumer_secret",
"YOUR_access_token",
"YOUR_access_token_secret")
elon_tweets <- load_twitter_by_user("elonmusk", 300, tokens)
#clean up tweets
cleaned_tweets <- generalPreprocessing(elon_tweets)
#label sentiment
labeled_tweets <- sentiment_labeler(cleaned_tweets, "text_clean")
#count frequency of each sentiment
sentiment_freq <- count_tweets(labeled_tweets)
#generate wordcloud
cleaned_tweets |>
clean_tweets() |>
count_words() |>
create_wordcloud()
For a more detailed example usage, see here
Functions
load_twitter
: Returns a user’s recent tweets (as a dataframe) given their user id
using the Twitter API.
generalPreprocessing
: Cleans the recent tweet dataframe generated by load_twitter
. Includes features such as removing punctuation and extracting emojis.
sentiment_labeler
: Determines the general (average) sentiment of recent tweets.
count_tweets
: Return the proportion of three sentiments.
create_wordcloud
: Generates a word cloud of most frequently used words in tweets.
Scope and Fit
There are existing R packages that preform tweet analysis and preprocessing (including textprepr and tweetr). However, none of these packages focus of providing metrics in the context of determining if the twitter user is worth engaging with.