Background
With Valentine’s Day coming up, I was thinking about a fun analysis that I could convert into a blog post.
Inspired by a beautiful visualization “Based on a True True story?” I decided to do something similar and analyze the sentiment in the most popular romantic comedies.
After searching for romantic comedies Google suggests a list of movies, where the top 5 are: “When Harry Met Sally”, “Love Actually”, “Pretty Woman”, “Notting Hill”, and “Sleepless in Seattle”.
How to do text analysis in R?
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.
library(subtools)
library(tidytext)
library(dplyr)
library(plotly)
library(purrr)
library(lubridate)
library(methods)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.
romantic_comedies_titles %
select(-score)
assign_sentiment %
left_join(bing, by = &quot;word&quot;) %__% mutate(sentiment = ifelse(is.na(sentiment), &quot;neutral&quot;, sentiment)) %__%
mutate(score = ifelse(sentiment == &quot;positive&quot;, 1,
ifelse(sentiment == &quot;negative&quot;, -1, 0)))
}
tokenize_romantic_comedies_with_sentiment %
map(~assign_sentiment(., bing))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 %
summarise(sentiment_per_minute = sum(score),
sentiment_per_minute = ifelse(sentiment_per_minute __= 1, 1,
ifelse(sentiment_per_minute % ungroup() %__%
mutate(perc_line_duration = line_duration/sum(line_duration))
}summarized_sentiment_romantic_comedies %
map(~summarized_movie_sentiment(.))Crème de la crème - data viz
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. This only works for the orginal post.
PositiveNeutralNegative
plot_sentiment %
summarize(duration = sum(perc_line_duration)) %__% .$duration * 100, 0)
plot_title %
layout(xaxis = list(title = &quot;&quot;,
showgrid = FALSE,
showline = FALSE,
showticklabels = FALSE,
zeroline = FALSE,
domain = c(0, 1)),
yaxis = list(title = &quot;&quot;,
showticklabels = FALSE),
barmode = &quot;stack&quot;,
title = ~plot_title) %__% hide_colorbar()}
htmltools::tagList(summarized_sentiment_romantic_comedies %__% map(~plot_sentiment(.)))Next steps
Recently, I learned about sentimentR package that let’s you analyze the sentiment on the senetence 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!
Read the original post at
Appsilon Data Science Blog.