We can use the subtools package to analyze the movies’ sentiment in R by loading the movie subtitles into R and then use tidytext to work with the text data.
Working with movie data
I downloaded the srt subtitles for 5 comedies from Open Subtitles before the analysis.
Now let’s load them into R and have a sneak peak of what the data looks like.
The next step is tokenization, chopping up the subtitles into single words. At this stage I also perform a minor cleaning task, which is removing stop words and adding information about the line and its duration.
After tokenizing the data I need to classify the word sentiment. In this analysis I simply want to know if the word has positive or negative sentiment. Tidytext package comes with 3 lexicons. The bing lexicon categorizes words as positive or negative. I use bing lexicon to assign the extracted words into desired classes.
Since I am interested in deciding the sentiment of the movie line, I need to aggregate the scores on the line level. I create a simple rule: if the overall sentiment score is >= 1 we classify the line as positive, negative when <= -1 and neutral in the other cases.
Crème de la crème: data visualization
After I am done with data preparation and munging, the fun begins and I get to visualize the data. In order to achieve a similar look as the authors of “Based on a True True Story?”, I use stack horizontal bar charts in plotly. The bar length represents the movie duration in minutes.
Hint: Hover on the chart to see the actual line and time.
Recently, I learned about sentimentR package that lets you analyze the sentiment on the sentence level. This would be interesting to conduct the analysis that way and see what sentiment scores would be received.
If you enjoyed this post spread the ♥ and share this post with someone who loves R as much as you!