Text Sentiment with R

Artificial intelligence is a vital tool in the DH community. R Studio’s Text Sentiment analysis is a great tool to help academics analyze historical documents and try to understand how they communicate. In this short tutorial, we will go over the basics of R Studio’s Text Sentiment analysis software on a step by step process. Moreover, students will gain a baseline knowledge of text analysis enough to dive into more in-depth artificial intelligence software like IB Watson.

  • Download RStudio. R is a free software for statistical computing and statistical graphs. RStudio is an interface to R, an intergraded development environment.
  • Once RStudio is downloaded, it helps to work on RMarkdown. Just click on where the tab down is and then click RMarkdown.
  • Download the RSentiment package. Go to library tab and then type in “RSentiment”
  • Calculate_custom_score -Calculate the score of sentences The first function looks  loads text and calculates score of each sentence on basis of presence of words of positive and negative sentiment supplied externally as parameter, presence of negation, and checking for sarcasm. 0 indicates neutral sentiment. Positive value indicates positive sentiment(1). Negative value indicates negative sentiment(-1). 99 indicates sarcasm.
    • Syntax: calculate_custom_score(text, positivewords, negativewords, check = 0)
  • Calculate_custom_sentiment-Predicts the sentiment of sentences This function loads text and words of positive and negative sentiment supplied externally as parameter and calculates sentiment of each sentence. It classifies sentences into 6 categories: Positive, Negative, Very Positive, Very Negative Sarcasm and Neutral.
    • Syntax:calculate_custom_sentiment(text, positivewords, negativewords, check = 0)
  • Calculate_score -Calculate the score of sentences This function loads text and calculates score of each sentence on basis of presence of words of positive and negative sentiment, presence of negation, and checking for sarcasm. 0 indicates neutral sentiment. Positive value indicates positive sentiment. Negative value indicates negative sentiment. 99 indicates sarcasm.
    • Syntax: calculate_score(text)
  • Calculate_sentiment -Predicts the sentiment of sentences This function loads text and calculates sentiment of each sentence. It classifies sentences into 6 categories: Positive, Negative, Very Positive, Very Negative Sarcasm and Neutral.
    • Now that we have the basics. Let’s turn our project into a useful tool in the digital humanities. I am going to analyze a portion of Winston Churchill’s famous speech, “We shall fight on the Beaches,” during World War II. The excerpt of the speech is “The British Empire and the French Republic, linked together in their cause and their need, will defend to the death their native soil, aiding each other like good comrades to the utmost of their strength.”
    • Syntax: calculate_sentiment(text)

Additional Sources:

https://cran.r-project.org/web/packages/RSentiment/index.html

https://medium.com/@randerson112358/simple-short-and-easy-sentiment-analysis-34834082e6fa

https://rdrr.io/cran/RSentiment/man/calculate_score.html

https://rdrr.io/cran/RSentiment/f/vignettes/Introduction.Rmd

Author: Kwaku

1 thought on “Text Sentiment with R

  1. This is really cool and super easy! I had worked with some text sentiment analysis tools before – a couple of python libraries and the IBM Watson API. This was probably the easiest one to work with. It certainly needed less setup that using Watson, and working in RStudio is easier and more intuitive than messing with python in the command line. Pretty cool how you can get an accurate sentiment analysis tool up and running in just a couple of minutes!

Leave a Reply

Your email address will not be published. Required fields are marked *