Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

# 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.

## 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.

## Subtitles preprocessing

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 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.