Real time Yelp reviews analysis and response solutions for restaurant owners

[This article was first published on R – NYC Data Science Academy Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


Before trying a new restaurant, we frequently consult with review platforms, such as Yelp, Zomato, or Google, where we can read comments from previous diners. Reading those reviews helps to make more informed decisions and can lead to a better dining experience with friends and family. It is clear that reviews are influential with diners, but how powerful can a single review be?

If a business receives one more star in overall rating, it can generate a 5 to 9% increase in revenue according to Michael Luca’s research, “Reviews, Reputation, and Revenue: The Case of”. On the flip side, a single negative review may cost a business 30 future customers, which implies that an unfavorable comment can significantly damage reputation, profitability, and trustworthiness of a business.

Given that a single bad review can harm a business, how can the business owner mitigate the negative impact? One simple solution is to improve the business’s customer relationship management system, by raising review complaints to the business’s attention and developing an automated real-time complaint response engine.

Our code is available on GitHub, and we have also created a Shiny App to demonstrate the responses in action.  



After selecting the problem and possible business resolution, we chose the Yelp Open Dataset published on September 1, 2017 as our dataset. After inspecting and cleaning the data, we conducted sentiment analysis to separate the positive reviews from the complaints. We then used the spaCy package to preprocess the complaint review text, and we used the gensim package to train n-gram phrase models and Latent Dirichelet Allocation topic models. Finally, we built an automated response chatbot to demonstrate the response to different types of complaints.


Sentiment Analysis

Determining the sentiment of a review is a deceptively complex problem. As our dataset was unlabeled, it was necessary for us to use an unsupervised method to identify the complaint reviews. We considered several options to achieve this goal.  

First, we considered using the number of review stars as a pseudo-label, assuming all one and two star reviews were complaints. However, upon further inspection it became clear that different reviewers have different standards; a three-star review from one reviewer may describe  a negative experience, while a two-star review from a different reviewer may describe a balanced experience.  

Second, we considered normalizing each review against the reviewer’s own standards to attempt to control for individual preferences and biases. To do this, we calculated the difference between the number of stars given for each review and that reviewer’s average star rating across all reviews. For example, if a reviewer’s average star rating is 3.5, a 2 star rating for an individual review would generate a  “stars_dif” rating of -1.5.  

However, this approach raised problems as well. The mean average star rating across all reviewers is 3.75, which means that the distribution of stars_dif will be skewed negative. We also found a large grouping of data points at stars_dif = 0, due to reviewers with a single review or reviewers giving the same star rating across all reviews.  Thus we believe that stars_dif may not be a reliable indicator of sentiment.

FInally, we decided to look into the actual text of the reviews to determine sentiment. We created a sentiment score rating by tokenizing and processing the text of the reviews using NLTK, and calculating the ratio of positive words vs. negative words in the text using dictionaries of sentiment words. We used the following formula for each review:  

(pos_words – neg_words) / total_words

Each review received a score bounded by -1 and 1; a more negative is closer to -1, while a more positive review is closer to 1.  


Topic Modeling

After we separated the complaints from the positive reviews, we built topic models to categorize the complaint reviews based on the subjects discussed in the text. We trained bigram and trigram models using the Gensim package, creating phrases by linking individual words based on a ratio of the frequency of their appearance together versus the frequency apart.  

After completing n-gram transformations, we trained a Latent Direchlet Allocation model, which is designed to uncover some of the latent topic structure within a corpus of documents. The user selects the desired number of topics as a hyperparameter, and the model will then assign to each topic an assortment of the individual tokens from the ‘vocabulary’ of the corpus. The LDA model assumes that the combination of topics within each document and the combination of tokens within each topic follows a Dirichlet probability distribution, which means that each document will contain just a few topics, and each topic will contain a small number of tokens.

In practice, the LDA model returns a group of the most frequent words for each topic. Each review in the corpus can be rated with a percentage for each topic, and the LDA model can also be used to classify new reviews by the same topic categories.  

After testing a variety of inputs, we determined that three topics generated the most interpretable results with the greatest separation between topics. We also adjusted the models to eliminate some words which appeared high in the output for all topics, in order to improve separation. While the meaning of each topic necessarily involves an element of human interpretation, we believe the three topics most closely correspond to complaints about price, service quality, and food quality.  


Generating Results on New Text

In order to classify and respond to new Yelp reviews, we wrote code to apply these models to a new input. The code uses the models to assign a primary complaint category and generate an appropriate response to the reviewer based on the complaint category. We then returned these responses to the reviewer via chatbot for demonstration purposes (see below).


Functions of the Shiny App (Check it out)

Interactive Map

In this map, we plot all of the restaurants with negative sentiment score reviews. Users can select the complaint type, and slider inputs can adjust the sentiment score, star rating, and the number of reviews. Notably, the clusters are not spread broadly across the country, because Yelp has only included reviews from 12 states in the dataset. Detailed restaurant information, including restaurant name, category, average star rating, and the number of reviews, is available by clicking on the location tag.


LDA Visualization

We divided negative reviews into three categories. By using this interactive graph, users can manually select each topic to view its most frequent terms.  The slider input allows the user to select either the terms that appear in the topic most frequently or the terms that are most distinctive in that topic vs. the other two topics.  This can help in assigning an interpretable name or “meaning” to each topic.


Complainteller Chatbot

As the chatbot is built based on LDA results, we classify complaints into three categories — price, food quality, and service quality. Once the chatbot receives a message, it runs the text through the NLP preprocessing and classification algorithms, ranks the percentage of possible complaint groups and then replies to the review immediately. For the demo video, we selected three-star reviews from NYY Steak (Yankee Stadium), which are new information to the chatbot and are viewed as a neutral comment without applying sentiment analysis. Our chatbot, Complainteller, successfully replies to those negative reviews with appropriate responses.

If you would like to talk to Complainteller, please feel free to register an account on Telegram and then you can start your conversation with @YelpReviews_bot.  Telegram is a free, non-profit cloud-based instant messaging service; bots are represented as Telegram accounts operated by an automated program. They can respond to messages, be invited into groups and be integrated into other programs.


Future Work

For NLP and LDA modeling, we could run more tests on removing different common words to improve the topic groups.  We could test more advanced sentiment score algorithms and models to improve the separation of complaint reviews.

The chatbot currently responds with a single reply to the most prominent topic. However, sometimes, customer complaints are mixed problems. We can further improve the chatbot to reply with an appropriate response incorporating multiple solutions. Additionally, the accuracy of the result depends on the volume of the text. Therefore, creating a common word database and tagging those words into different categories may improve the performance.


The post Real time Yelp reviews analysis and response solutions for restaurant owners appeared first on NYC Data Science Academy Blog.

To leave a comment for the author, please follow the link and comment on their blog: R – NYC Data Science Academy Blog. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)