I was planning my trip to Amsterdam in January and was looking through hostels in Hostel World filtering for different features and amenities. One amenity that I thought I would definitely need was free wifi if I wanted to do some programming from the hostel and also just because life demands it in general. While there’s a ton of hostels that offer free wifi, I’ve definitely been at the end of the stick where the quality of wifi has been unmentionably bad. This probably goes for hotels as well as hostels, but generally hostels are cheaper and offer less in the way of complementary services.
That got me thinking about creating an interesting application that could judge the quality of wifi in reviews. Randomly I decided to spin up a new idea for a scraping/api for Hostel World where I could actually find the reviews that mention wifi and other amenities that would be useful. Instead of meticulously scanning through hundreds of reviews, I could just scrape the reviews, parse out keywords, and assign sentiment scores to each review.
Eventually I made it into a Twitter Bot:
@racketracer Hostel noise: 77.0/100. Positive: 15 Negative: 5 “even with the bar downstairs i never had any noise issues in my room”
— Jay Feng (@HotelReviewsBot) November 18, 2015
Heh, I am replying to myself. Try it out yourselves! Mention @HostelReviewBot and link a hostel from Hostelworld and include the word wifi, breakfast, noise, bathroom, or shower.
The positive and negative refers to the number of positive and negative sentiment reviews respectively. The quote is picked from being an overall average of common words mentioned when scraping the site. Overall there’s too much information that can’t really get stuffed into 140 characters which is quite a sham. I should learn how to create a quick flask api. Maybe that’s for later.
Let’s go through a quick tutorial of some python stuff.
The idea is to first create a list of amenities that we would like to track from each hostel. I can think of five pretty important things that a hostel or a hotel should have that aren’t rated on Hostel World or another review site like Tripadvisor. Amenities is a dictionary of key value stores where the values are chained synonyms that could be used in text to describe our amenities. That way if someone mentions or misspells “wifi” with “wi fi” or “wi-fi” or just another definition like “internet”, we can track their opinion. I welcome any more ideas that could be things to track that right now can be ambiguous or require reading-reviews-effort.
I’m using the requests library to grab the html from Hostel World. Here I’ve picked the Black Swan hostel in Barcelona as an example and taken the url from their reviews site.
lxml is a useful package for taking an html page and transforming it into an xml tree to then use xpath to select elements. By grabbing the total number of reviews from the original base url, we can find out how many pages we need to scrape if the review count is less than 500. Else we can just go from page 1 to 25 because Hostel World lists 20 reviews per page.
Each review comes with a rating from 1-100 that we want to grab so that we can get a rating for each review. We can also use this later to subset the hotel reviews by our specific keyword (in this case “wifi”) and then see what the average rating of those reviews are. Chances are that if there’s a general average of lower reviews when the keyword “wifi” is in the review, the hostel probably doesn’t do well in that amenity. I used pandas to store the data for ease of use.
This should be an example of what the dataframe would look like: (Actually I already subsetted out the non-related reviews)
Natural Language Processing
There’s a couple different interesting ways we can approach text analysis when looking at reviews. I haven’t read the entire NLTK tutorial guide yet so the only ways I can think of analyzing these reviews would be to use a couple of methods that I might do if I were thinking about doing the whole situation manually.
- Averaging the subsetted reviews: As mentioned before, we can subset the reviews by the reviews that mentioned the key word and take their average rating and compare it against the overall average.
- N-grams: Another possibility to find common two word or three word phrases. The problem for the reviews is that there may not be enough reviews to extract these phrases. Also we chained the keyword “wifi” so theoretically we could replace each keyword we find with “wifi” in it to see if there are repeated n-grams, but with maybe 10-20 reviews actually describing the wifi, it might not be enough to be anything significant.
- Common words: An alternative to getting n-grams is to try finding common words in the reviews. Essentially these would be uni-grams so technically n-grams but we can also filter out the non-important words that don’t really contribute to the conversation when selecting these uni-grams.
- Sentiment analysis: Sentiment analysis is always a bit tricky when done programmatically because it means we then need to curate our tokenization and dictionary with specific keywords for our purpose. If we were to individually read each review that contained “wifi” in it, we could probably get a sense of how each reviewer felt about the wifi. We would then tally up the number of good reviews and bad reviews in our head and get a sense of how we feel about it. But programmatically it’s always harder to just assign sentiment without going into huge amounts of customization for the product at hand. For example, “too gimmicky for them to need a “like” on Facebook to use their wifi”. We understand that as being a small negative, though overall that really doesn’t even describe the wifi as being good quality or not. A machine could interpret that as being a great review though if it sees the keyword “like” and “wifi” in the same sentence and doesn’t understand the process of Facebook “likes” as gimmicked advertisements. In conclusion, I decided using a package called TextBlob, which is a wrapper around NLTK and effectively gives general sentiment scores on sentences and phrases. It’s a pretty good solution for general purpose projects and you don’t have to go into too much manual positive and negative sentiment dictionary searching.
Okay, let’s just try extracting the key phrases first. In each review, we have to first find the sentence or phrase where our keyword of “wifi” was mentioned. To do this, we’ll first need to split up the review into chunks of phrases with specific delimiters as commas, periods, etc.. After that, we’ll check if “wifi” exists in in the list of phrases and then if it does we’ll parse it out into it’s own column called “wifi” in the dataframe. I used the apply method here and passed in the function get_key_sentence which checks if the key exists and if it doesn’t just returns null.
Awesome so now we got a dataframe that we can subset to get just the reviews that contain “wifi” in them and isolate the individual sentences and phrases with wifi in it. Let’s apply TextBlob to each one of the phrases to get numerical sentiments.
Cool so we got sentiment analysis pretty easily. Note that this is probably the hackiest and easiest way to do it when a library wrapper is already packaged around NLTK. It really works for all of us when we are okay with 80% of it being correct and can deal with a possible completely off 20%. The next step is grabbing common uni-gram words.
Here nltk is grabbing stopwords which are essentially words that should be parsed out that are words like “is, the, i, etc…”. We want to count occurrences for all words besides stopwords and we do that by passing in a dictionary and tallying counts (Note NLTK has it’s own library for this but I didn’t figure it out in time). Since dictionaries are pass by reference in Python, we can pass in the value of word_freq and it’ll update the dictionary without having to return the actual value. Then we’ll implement the collections library which gives us an easy way to grab the top three common words.
This humongous dictionary called summary is an object of analysis that spans from the most positive phrase, most negative phrase, positive and negative sentiments, and rating averages. Ultimately it’s hard to store all of the information in a concise way to return it to the twitter bot. Ultimately it should be pretty clear what each averaged metric is doing, but in the end I only used a couple of the metrics as Twitter really only has 140 characters maxed out. For common_phrase I looked took the top three common words from word_freq and looked for the reviews that had the most out of the three.
The rest of the code is up on github. I’ll be sure to upload my Twitter API code up as well when it’s totally bug free. There’s a lot of things that can be improved as I hope that I can add in concurrent requests (though not to re-invent scrapy) and add in caching of previous hotels. There’s also a lot to be improved in natural language processing as I’ve just touched the surface with some trivial things. As the knowledge of NLTK grows I am going to want to implement more customized features as there’s a ton of hidden gems in a huge wealth of information like hostel and hotel reviews. The only real problem is laziness.
Please add comments and feedback.