Web Scraping and Sentiment Analysis for Yelp Review

Frank Wang
Posted on May 30, 2016

Contributed by Frank Wang. He  is currently in the NYC Data Science Academy 12 week full time Data Science Bootcamp program taking place between April 11th to July 1st, 2016. This post is based on his third class project - Web Scraping, due on the 6th week of the program.

Human language data is one of the most important data and most are free. Internet and social media time and age, people's opinions, reviews, and recommendations have become a valuable resource for political science and businesses. Thanks to modern technologies, we are now able to collect and analyze such data most efficiently. In this project, I scrape the Yelp review data and then applied sentiment analysis to classify documents based on their polarity: the attitude of the writer.

Yelp Web Scraping

Yelp review can be download for free. API and keys are required to download all the data. What I am interested are the review, rating score and author’s name. Beautifufsoup is used to scrape the web data and then information is extracted and save to data frame. Data for different category, such as restaurant and park, are save to different folder for analysis.

abc_kitchen

FIG.1 example of the ABC Kitchen Review

ABC_hist

FIG.2 Author information data (left) and the histogram of the review rating score

Sentiment Analysis

Textbold is first used to check the prediction accuracy. Although Texbold provide option to train the data using Naive Bayes, it is very slow. Therefore the pre-trained model is used for simplicity. FIG.3 shows the predicted score for ABC kitchen, which is scaled to 1-5 to compare the real score. The distribution is about symmetry with long tails in both ends. The predicted average score is 3.6, which is lower than the real score of 4.2. If we divided the score to two categories: positive and negative, the prediction accuracy is about 96%. The prediction accuracy is largely drop (<50%) when 1-5 rating are compared. The predictor is not generous comparing with human being: the 5 star is much lower. Different data set, such as park review, are tried. The overall conclusions are similar.

SA_ABC_kitchen_hist

SA_ABC_kitchen_5start

FIG.3 Predicted rating score for restaurant review

sa_cettral_park

sa_central_park5

FIG.4 Predicted rating score for park review

Training model using real review data

NLTK tool is used in this study. The review data is transformed to a list of dictionary: bag-of-words. A simple unigram model is used for the first try. We use Naïve Bayes to train our data. The data includes about 6000 restaurant reviews. It is important to randomly shuffle the data before use it. The training data and test data are 70% and 30%, respectively. The prediction accuracy on the test data is about 72% with this simple model. While the accuracy drop to 62% with 2000 data. Apparently more data is needed. Fig. 5 show the most informative words. It is interesting that some “positive” words, such as “greeting”, “politely” , can becomes negative in the most informative words. They are not errors. Since we use unigram model: the sentences are broken into bag of single words.  We will improve this use bigram model in the future.

ABC_word_cloud

FIG. 4 Word cloud of most informative words

Summary

Web review is scarped. Besides the review text, the pictures can have useful information. We download review photo for future study.

Preliminary sentiment study is done with simple model using Naive Bayes model. The prediction accuracy is about 72%, which is low due to the simple model. TextBlob package gives high accuracy about 96%. We will continue study using better model: word clean, bigram word-bag-model, tf-idf transform, word stemming and cross-validation.

About Author

Frank Wang

Frank Wang

Frank (Lanfa) Wang have worked in several research laboratories as a physicist. He has over a decade of experience in modeling and scientific computing and had access to the large supercomputer NERSC. He participated several national/international projects: Japanese...
View all posts by Frank Wang >

Related Articles

Leave a Comment

Avatar
Google April 12, 2020
Google Please take a look at the web pages we stick to, such as this a single, as it represents our picks from the web.
Avatar
Google April 4, 2020
Google Here are some links to websites that we link to due to the fact we believe they may be really worth visiting.
Avatar
hermes picotin handbags faux June 20, 2017
Im grateful for the blog article.Thanks Again. Will read on… hermes picotin handbags faux http://www.modetaschen.in/en/hermes-picotin-30-c18_20.html

View Posts by Categories


Our Recent Popular Posts


View Posts by Tags

#python #trainwithnycdsa 2019 airbnb Alex Baransky alumni Alumni Interview Alumni Reviews Alumni Spotlight alumni story Alumnus API Application artist aws beautiful soup Best Bootcamp Best Data Science 2019 Best Data Science Bootcamp Best Data Science Bootcamp 2020 Best Ranked Big Data Book Launch Book-Signing bootcamp Bootcamp Alumni Bootcamp Prep Bundles California Cancer Research capstone Career Career Day citibike clustering Coding Course Demo Course Report D3.js data Data Analyst data science Data Science Academy Data Science Bootcamp Data science jobs Data Science Reviews Data Scientist Data Scientist Jobs data visualization Deep Learning Demo Day Discount dplyr employer networking feature engineering Finance Financial Data Science Flask gbm Get Hired ggplot2 googleVis Hadoop higgs boson Hiring hiring partner events Hiring Partners Industry Experts Instructor Blog Instructor Interview Job Job Placement Jobs Jon Krohn JP Morgan Chase Kaggle Kickstarter lasso regression Lead Data Scienctist Lead Data Scientist leaflet linear regression Logistic Regression machine learning Maps matplotlib Medical Research Meet the team meetup Networking neural network Neural networks New Courses nlp NYC NYC Data Science nyc data science academy NYC Open Data NYCDSA NYCDSA Alumni Online Online Bootcamp Open Data painter pandas Part-time Portfolio Development prediction Prework Programming PwC python python machine learning python scrapy python web scraping python webscraping Python Workshop R R language R Programming R Shiny r studio R Visualization R Workshop R-bloggers random forest Ranking recommendation recommendation system regression Remote remote data science bootcamp Scrapy scrapy visualization seaborn Selenium sentiment analysis Shiny Shiny Dashboard Spark Special Special Summer Sports statistics streaming Student Interview Student Showcase SVM Switchup Tableau team TensorFlow Testimonial tf-idf Top Data Science Bootcamp twitter visualization web scraping Weekend Course What to expect word cloud word2vec XGBoost yelp