How to build a Recommender System for Airbnb in Python
By: Alexandra Gastone
Tags: How To, Recommendations, Recommender Systems, Sentiment Analysis, Airbnb

Part 1: Estimate the sentiment of comments

Context

Sentiment Analysis is a subfield of NLP (Natural Language Processing) that as of late has been gaining lots of traction at the industry level, with more and more companies looking to extract the opinions of their clients or users from textual data such as reviews, chats, or emails in order to gain actionable insights. Given that most of the data that’s readily available usually comes in the form of text-heavy unstructured datasets, finding ways of extracting valuable information from these sources has become a key target for data-driven company growth.

Preparing our data

The dataset itself that we’ll be working with includes all unique listing and user IDs (listing_id, reviewer_id), and each user’s reviews on the listings they visited (comments).

A first look at our comments feature shows a couple of things we might want to take care of before doing any computations:

some entries contain empty or null comments, remove those

comments, remove those some reviews will have the prototypical “Host canceled this booking x days before…” comment when a stay is canceled , so we can also drop those entries

, so we can also drop those entries not all reviews are in English, we have to detect the language of the comments and, depending on what percentage of them are non-English, either drop them or translate them (it turns out the non-English comments only constitute 0.06% of all comments at this point, so we can go ahead and drop them for now, but feel free to try your hand at translating — Google offers a free python library implementing Google Translate’s API called googletrans)

of the comments and, depending on what percentage of them are non-English, either drop them or translate them (it turns out the non-English comments only constitute 0.06% of all comments at this point, so we can go ahead and drop them for now, but feel free to try your hand at translating — Google offers a free python library implementing Google Translate’s API called googletrans) other text normalization steps (i.e. remove numerical characters)

Preprocessing the ‘comments’ column for Sentiment Analysis

Note that we’re purposefully not going to be removing punctuation or capitalization, which are typically part of the text normalization step of the NLP pipeline. The reason for this is that the Sentiment Analyzer we’ll be working with actually incorporates this kind of information when estimating sentiment! NLTK’s Vader (Valence Aware Dictionary and sEntiment Reasoner) lexicon and pre-trained sentiment analysis tool has had a lot of success in dealing with reviews and social sentiment. Part of the reason it works so well is that it adds valence to words that are capitalized, that have degree modifiers (eg. very), are accompanied by punctuation (eg. !!!) or emoticons (eg. :) ), and it can even handle shifts in sentiment in a single sentence, also known as conjunctions.

Implementation

Vader actually returns a score for positive, negative and neutral sentiment, but we’ll only be looking at the compound score of all three for our system.

Generating polarity scores

Example of sentiment polarity compound scores for comments in English

For our example comment (it’s the second comment in the image above):

polarity = 0.91

which seems to agree pretty well with the content of the message. Other negative and positive comments also look like they’re being picked up pretty well, so looks like our Sentiment Analyzer is doing a good job!

Data exploration and data-driven actionable insights

Since the goal of this project is not to analyze the polarity feature per se, we won’t be going into too much detail here, but there are a couple of preliminary observations that I’ll point out that might pique your interest. I also created this dashboard using Streamlit and deployed on Heroku to visualize some relevant data, so feel free to play around with it!

Screencast of polarity dashboard

Distribution

Looking at the distribution of polarity, we can see that the values are overwhelmingly positive. Some food for thought: do we tend to write comments more often when they are positive? Do we avoid writing negative comments? Do we have a high threshold of what constitutes a negative experience?

Relation to other features

Relation between polarity and other selected features

We can also look towards answering the question of what contributes to a positive comment by examining the correlation between the polarity scores and other features in our data such as rating scores for cleanliness, location, value, etc… Polarity seems to be most highly correlated with the global review_scores_rating feature. Interestingly, location seems to be the least important factor taken into account for comment sentiment: polarity is least correlated with review_scores_location. If we further look at the polarity scores grouped by neighbourhood, we can see that the neighbourhoods with highest polarity vs location scores are different. For instance, the top three neighbourhoods ranked by polarity are the Leather District, Roslindale, and Jamaica Plain, whereas the top three neighbourhoods ranked by review_scores_location are Longwood Medical Area, Back Bay, and the North End. If you were in charge of providing advice to hosts regarding ways to increase their rate of positive comments, you’re in luck! Arguably the hardest feature for a host to change is the location of their listing, but by working on improving their other scores (eg. cleanliness, ease of check in, etc…) the data would suggest that this will actually reflect more strongly on their comments!