NLP Analysis of Yelp Restaurant Reviews

Ankur Vishwakarma
5 min readMar 12, 2018

--

As cliché as it may sound, it really does feel like yesterday was day 1 at Metis but here I am in Week 8. For my fourth project, I looked into Yelp reviews and use Natural Language Processing (NLP) to extract more meaning out of them.

Project Abstract

Opening and managing a restaurant in the United States is a difficult business. According to Business Insider, as many as 60% of restaurants close within the 1st year of opening. Yelp is a great source of positive and negative feedback from customers. However, there are some issues:

  1. It is time consuming to read all reviews. Even if a restaurant owner decided to read all reviews,
  2. That still would not allow him/her to compare feedback against other restaurants in the area.

This project aims to analyze restaurant reviews on Yelp via topic modeling to determine main topics for positive and negative reviews. Once each review was assigned its topic weights, that information was averaged to assign topic weights for each restaurant.

With all restaurants and their reviews translated into a topic space, direct comparison of positive and negative review topics was made possible.

Data Source

  1. Data in JSON format was downloaded from Yelp and set up in MongoDB on an AWS instance.

Exploratory Data Analysis

Of the 5.2 million reviews the dataset contains, 130,000 restaurant reviews for the state of Ohio were kept for analysis. Why Ohio? It happened to contain the right number of businesses in this dataset where I could get a varied representation but not end up with something too computationally expensive.

Top 10 states represented in the Yelp dataset by number of businesses.

Lengths of the reviews were calculated and as the histogram below shows, all star ratings consisted of the same distribution of short, long, and medium-length reviews.

Tokenization

Flowchart of the tokenization process, resulting in two topic spaces.

Documents (yelp reviews) were tokenized using TFIDF and 1-grams, which gave the most consistent results and distinct topics. For topic modeling, the following approaches were used:

Latent Semantic Indexing (LSI)

LSI is really easy and friendly to use with gensim. However, negative word weights in resulting topics were not easily interpretable for this application.

Latent Dirichlet Allocation (LDA)

I thought LDA could be a useful technique since it assumes that each document (yelp review, in this case) is a mixture of a small number of topics. However, my LDA topics had lots of overlap in meaning among each other, which could be the result of relatively short document size.

Non-negative Matrix Factorization (NMF)

NMF gave the most interpretable results and separable topics. For example, one of the topics returned had the following word weights:

  • 2.295 * beer,
  • 2.214 * bar,
  • 1.432 * selection,
  • 1.147 * nice,
  • 1.144 * happy,
  • 1.048 * hour

With those word weights, we can assume that this topic had something to do with a restaurant’s bar and its happy hour specials. Therefore, rest of the analysis was done using NMF. The chart below shows the elbow plot of relative topic strengths which was used to settle on 6 topics for the rest of the project.

Relative topic strengths determined by LSA. Although NMF was used to move forward, this information was still valuable in determining how many topics to use.

Mapping All Reviews to Topic Space

Mapping all reviews (and businesses) to the topic space

The diagram above is an illustrated overview of the next few steps.

  1. Map all reviews to topics using Non-negative Matrix Factorization (NMF).
  2. Normalize topic weights to sum to 1 to increase interpretability.
  3. Average the topic distributions for all reviews of a restaurant to map that restaurant to the topic space.

Now that all restaurants had been mapped to the topic space, we can do three things:

  1. Visually show to a restaurant owner what topics their positive and negative reviews reside in. This makes it clear if a restaurant needs to improve its service quality, or if its much more popular for its pizza than its bar.
  2. Take a mean of all the restaurants and create an “average restaurant” in Ohio. This will allow comparison of any restaurant against this calculated mean.
  3. Use cosine similarity to find restaurants similar to each other based not on attributes or definitions, but its user reviews. This is analogous to getting restaurants that elicit a similar sentiment from yelp reviewers.

Visualizing the Results

The interactive visualization on Tableau public can be reached at this link. The screenshot below shows what the dashboard looks like for a random restaurant from this analysis.

A screenshot of the Tableau dashboard for a restaurant showing positive attributes, negative attributes, and similarity to other restaurants.

Final Thoughts

It was a lot of fun to take something as chaotic and noisy as text and turn it into actionable information for a business owner. For future efforts, I’d really like to:

  • Work on better tokenization to result in more varied topics.
  • Create one topic space for all reviews instead of positive/negative spaces. There is a lot of overlap between the two.
  • Incorporate sentiment analysis to extract negative sentences from positive reviews and vice versa.
  • Compare findings across cities/states.

Thanks for reading! I would love to hear your thoughts, either here as a comment or on Twitter. The code used for this is located in this github repository.

--

--

Ankur Vishwakarma

Data scientist. Background in energy management and mechanical engineering. Motorcyclist.