This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| result = requests.get("http://books.toscrape.com/catalogue/page-50.html") | |
| print("status code for page 50: " + str(result.status_code)) | |
| result = requests.get("http://books.toscrape.com/catalogue/page-51.html") | |
| print("status code for page 51: " + str(result.status_code)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pages_urls = [] | |
| new_page = "http://books.toscrape.com/catalogue/page-1.html" | |
| while requests.get(new_page).status_code == 200: | |
| pages_urls.append(new_page) | |
| new_page = pages_urls[-1].split("-")[0] + "-" + str(int(pages_urls[-1].split("-")[1].split(".")[0]) + 1) + ".html" | |
| print(str(len(pages_urls)) + " fetched URLs") | |
| print("Some examples:") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| booksURLs = [] | |
| for page in pages_urls: | |
| booksURLs.extend(getBooksURLs(page)) | |
| print(str(len(booksURLs)) + " fetched URLs") | |
| print("Some examples:") | |
| booksURLs[:5] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| names = [] | |
| prices = [] | |
| nb_in_stock = [] | |
| img_urls = [] | |
| categories = [] | |
| ratings = [] | |
| # scrape data for every book URL: this may take some time | |
| for url in booksURLs: | |
| soup = getAndParseURL(url) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| # read data | |
| reviews_df = pd.read_csv("../input/Hotel_Reviews.csv") | |
| # append the positive and negative text reviews | |
| reviews_df["review"] = reviews_df["Negative_Review"] + reviews_df["Positive_Review"] | |
| # create the label | |
| reviews_df["is_bad_review"] = reviews_df["Reviewer_Score"].apply(lambda x: 1 if x < 5 else 0) | |
| # select only relevant columns | |
| reviews_df = reviews_df[["review", "is_bad_review"]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| reviews_df = reviews_df.sample(frac = 0.1, replace = False, random_state=42) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # remove 'No Negative' or 'No Positive' from text | |
| reviews_df["review"] = reviews_df["review"].apply(lambda x: x.replace("No Negative", "").replace("No Positive", "")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # return the wordnet object value corresponding to the POS tag | |
| from nltk.corpus import wordnet | |
| def get_wordnet_pos(pos_tag): | |
| if pos_tag.startswith('J'): | |
| return wordnet.ADJ | |
| elif pos_tag.startswith('V'): | |
| return wordnet.VERB | |
| elif pos_tag.startswith('N'): | |
| return wordnet.NOUN |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # add sentiment anaylsis columns | |
| from nltk.sentiment.vader import SentimentIntensityAnalyzer | |
| sid = SentimentIntensityAnalyzer() | |
| reviews_df["sentiments"] = reviews_df["review"].apply(lambda x: sid.polarity_scores(x)) | |
| reviews_df = pd.concat([reviews_df.drop(['sentiments'], axis=1), reviews_df['sentiments'].apply(pd.Series)], axis=1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # add number of characters column | |
| reviews_df["nb_chars"] = reviews_df["review"].apply(lambda x: len(x)) | |
| # add number of words column | |
| reviews_df["nb_words"] = reviews_df["review"].apply(lambda x: len(x.split(" "))) |