<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 From Command Line - Import CSV file into MongoDB
mongoimport --db airbnb --type csv --file listings_31Mar.csv --headerline -c Rawdata (From Data Preparation Part 1)
mongoimport --db airbnb --type csv --file reviews_27Mar.csv -c reviews (From Data Preparation Part 3)
Step1:  Connect Python to MongoDB
Step2:  Retrieve from Database
Database named as "airbnb"
Step3:  Retrieve Tables from Database
Step4:  Store data in a pandas dataframe for further analysis
Step5:  Sentiment Analysis
Step6:  1. Calculate AFINN Score
Step7:  2. Split into positive and negative reviews
Step8:  3. Create TFIDF Corpus
Step9:  Negative Reviews
Step10:  Positive Reviews
<ASSISTANT_TASK:>
Python Code:
import pymongo
from pymongo import MongoClient
Explanation: From Command Line - Import CSV file into MongoDB
mongoimport --db airbnb --type csv --file listings_31Mar.csv --headerline -c Rawdata (From Data Preparation Part 1)
mongoimport --db airbnb --type csv --file reviews_27Mar.csv -c reviews (From Data Preparation Part 3)
End of explanation
client = MongoClient('mongodb://localhost:27017/')
Explanation: Connect Python to MongoDB
End of explanation
db = client.airbnb
Explanation: Retrieve from Database
Database named as "airbnb"
End of explanation
listings = db.Rawdata
reviews = db.reviews
Explanation: Retrieve Tables from Database
End of explanation
import pandas as pd
listings_df = pd.DataFrame(list(listings.find()))
reviews_df = pd.DataFrame(list(reviews.find()))
listings_df.head()
reviews_df.head()
Explanation: Store data in a pandas dataframe for further analysis
End of explanation
from afinn import Afinn
from textblob import TextBlob
Explanation: Sentiment Analysis
End of explanation
afinn = Afinn()
reviews_df['afinn'] = ""
for i in range(0, len(reviews_df)):
    if pd.isnull(reviews_df.eng_comments[i]):
        reviews_df.loc[i, 'afinn'] = "NA"
    else:
        reviews_df.loc[i, 'afinn'] = afinn.score(reviews_df.loc[i, "eng_comments"])
reviews_df.head(10)
Explanation: 1. Calculate AFINN Score
End of explanation
negative = reviews_df[reviews_df['afinn'] < 0]
positive = reviews_df[reviews_df['afinn'] > 0]
Explanation: 2. Split into positive and negative reviews
End of explanation
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
hosts = list(listings_df['host_name'])
hosts = [x.lower() for x in hosts if type(x) is str]
stopwords = ['a', 'able', 'about', 'above', 'abroad', 'according', 'accordingly', 'across', 'actually', 'adj', 'after', 'afterwards', 'again', 'against', 'ago', 'ahead', "ain't", 'all', 'allow', 'allows', 'almost', 'alone', 'along', 'alongside', 'already', 'also', 'although', 'always', 'am', 'amid', 'amidst', 'among', 'amongst', 'an', 'and', 'another', 'any', 'anybody', 'anyhow', 'anyone', 'anything', 'anyway', 'anyways', 'anywhere', 'apart', 'appear', 'appreciate', 'appropriate', 'are', "aren't", 'around', 'as', "a's", 'aside', 'ask', 'asking', 'associated', 'at', 'available', 'away', 'awfully', 'b', 'back', 'backward', 'backwards', 'be', 'became', 'because', 'become', 'becomes', 'becoming', 'been', 'before', 'beforehand', 'begin', 'behind', 'being', 'believe', 'below', 'beside', 'besides', 'best', 'better', 'between', 'beyond', 'both', 'brief', 'but', 'by', 'c', 'came', 'can', 'cannot', 'cant', "can't", 'caption', 'cause', 'causes', 'certain', 'certainly', 'changes', 'clearly', "c'mon", 'co', 'co.', 'com', 'come', 'comes', 'concerning', 'consequently', 'consider', 'considering', 'contain', 'containing', 'contains', 'corresponding', 'could', "couldn't", 'course', "c's", 'currently', 'd', 'dare', "daren't", 'definitely', 'described', 'despite', 'did', "didn't", 'different', 'directly', 'do', 'does', "doesn't", 'doing', 'done', "don't", 'down', 'downwards', 'during', 'e', 'each', 'edu', 'eg', 'eight', 'eighty', 'either', 'else', 'elsewhere', 'end', 'ending', 'enough', 'entirely', 'especially', 'et', 'etc', 'even', 'ever', 'evermore', 'every', 'everybody', 'everyone', 'everything', 'everywhere', 'ex', 'exactly', 'example', 'except', 'f', 'fairly', 'farther', 'few', 'fewer', 'fifth', 'first', 'five', 'followed', 'following', 'follows', 'for', 'forever', 'former', 'formerly', 'forth', 'forward', 'found', 'four', 'from', 'further', 'furthermore', 'g', 'get', 'gets', 'getting', 'given', 'gives', 'go', 'goes', 'going', 'gone', 'got', 'gotten', 'greetings', 'h', 'had', "hadn't", 'half', 'happens', 'hardly', 'has', "hasn't", 'have', "haven't", 'having', 'he', "he'd", "he'll", 'hello', 'help', 'hence', 'her', 'here', 'hereafter', 'hereby', 'herein', "here's", 'hereupon', 'hers', 'herself', "he's", 'hi', 'him', 'himself', 'his', 'hither', 'hopefully', 'how', 'howbeit', 'however', 'hundred', 'i', "i'd", 'ie', 'if', 'ignored', "i'll", "i'm", 'immediate', 'in', 'inasmuch', 'inc', 'inc.', 'indeed', 'indicate', 'indicated', 'indicates', 'inner', 'inside', 'insofar', 'instead', 'into', 'inward', 'is', "isn't", 'it', "it'd", "it'll", 'its', "it's", 'itself', "i've", 'j', 'just', 'k', 'keep', 'keeps', 'kept', 'know', 'known', 'knows', 'l', 'last', 'lately', 'later', 'latter', 'latterly', 'least', 'less', 'lest', 'let', "let's", 'like', 'liked', 'likely', 'likewise', 'little', 'look', 'looking', 'looks', 'low', 'lower', 'ltd', 'm', 'made', 'mainly', 'make', 'makes', 'many', 'may', 'maybe', "mayn't", 'me', 'mean', 'meantime', 'meanwhile', 'merely', 'might', "mightn't", 'mine', 'minus', 'miss', 'more', 'moreover', 'most', 'mostly', 'mr', 'mrs', 'much', 'must', "mustn't", 'my', 'myself', 'n', 'name', 'namely', 'nd', 'nearly', 'necessary', 'need', "needn't", 'needs', 'neither', 'never', 'neverf', 'neverless', 'nevertheless', 'next', 'nine', 'ninety', 'no', 'nobody', 'non', 'none', 'nonetheless', 'noone', 'no-one', 'nor', 'normally', 'not', 'nothing', 'notwithstanding', 'novel', 'now', 'nowhere', 'o', 'obviously', 'of', 'off', 'often', 'oh', 'ok', 'okay', 'old', 'on', 'once', 'one', 'ones', "one's", 'only', 'onto', 'opposite', 'or', 'other', 'others', 'otherwise', 'ought', "oughtn't", 'our', 'ours', 'ourselves', 'out', 'outside', 'over', 'overall', 'own', 'p', 'particular', 'particularly', 'past', 'per', 'perhaps', 'placed', 'please', 'plus', 'possible', 'presumably', 'probably', 'provided', 'provides', 'q', 'que', 'quite', 'qv', 'r', 'rather', 'rd', 're', 'really', 'reasonably', 'recent', 'recently', 'regarding', 'regardless', 'regards', 'relatively', 'respectively', 'right', 'round', 's', 'said', 'same', 'saw', 'say', 'saying', 'says', 'second', 'secondly', 'see', 'seeing', 'seem', 'seemed', 'seeming', 'seems', 'seen', 'self', 'selves', 'sensible', 'sent', 'serious', 'seriously', 'seven', 'several', 'shall', "shan't", 'she', "she'd", "she'll", "she's", 'should', "shouldn't", 'since', 'six', 'so', 'some', 'somebody', 'someday', 'somehow', 'someone', 'something', 'sometime', 'sometimes', 'somewhat', 'somewhere', 'soon', 'sorry', 'specified', 'specify', 'specifying', 'still', 'sub', 'such', 'sup', 'sure', 't', 'take', 'taken', 'taking', 'tell', 'tends', 'th', 'than', 'thank', 'thanks', 'thanx', 'that', "that'll", 'thats', "that's", "that've", 'the', 'their', 'theirs', 'them', 'themselves', 'then', 'thence', 'there', 'thereafter', 'thereby', "there'd", 'therefore', 'therein', "there'll", "there're", 'theres', "there's", 'thereupon', "there've", 'these', 'they', "they'd", "they'll", "they're", "they've", 'thing', 'things', 'think', 'third', 'thirty', 'this', 'thorough', 'thoroughly', 'those', 'though', 'three', 'through', 'throughout', 'thru', 'thus', 'till', 'to', 'together', 'too', 'took', 'toward', 'towards', 'tried', 'tries', 'truly', 'try', 'trying', "t's", 'twice', 'two', 'u', 'un', 'under', 'underneath', 'undoing', 'unfortunately', 'unless', 'unlike', 'unlikely', 'until', 'unto', 'up', 'upon', 'upwards', 'us', 'use', 'used', 'useful', 'uses', 'using', 'usually', 'v', 'value', 'various', 'versus', 'very', 'via', 'viz', 'vs', 'w', 'want', 'wants', 'was', "wasn't", 'way', 'we', "we'd", 'welcome', 'well', "we'll", 'went', 'were', "we're", "weren't", "we've", 'what', 'whatever', "what'll", "what's", "what've", 'when', 'whence', 'whenever', 'where', 'whereafter', 'whereas', 'whereby', 'wherein', "where's", 'whereupon', 'wherever', 'whether', 'which', 'whichever', 'while', 'whilst', 'whither', 'who', "who'd", 'whoever', 'whole', "who'll", 'whom', 'whomever', "who's", 'whose', 'why', 'will', 'willing', 'wish', 'with', 'within', 'without', 'wonder', "won't", 'would', "wouldn't", 'x', 'y', 'yes', 'yet', 'you', "you'd", "you'll", 'your', "you're", 'yours', 'yourself', 'yourselves', "you've", 'z', 'zero']
stopwords += ['apartment', 'appartment', 'apartments', 'house', 'room', 'rooms', 'airbnb', 'barcelona', 'accommodation', 'accomodation', 'mention', 'flat', 'review', 'rental', 'brother', 'lover', 'date', 'visit', 'face']
stopwords += hosts
Explanation: 3. Create TFIDF Corpus
End of explanation
vect_neg = TfidfVectorizer(min_df=50, max_df=0.8, token_pattern=r'\b[A-Za-z]{3,}\b', stop_words=stopwords)
X_neg = vect_neg.fit_transform(negative['eng_comments'])
X_df_neg = pd.DataFrame(X_neg.toarray(), index=negative['listing_id'], columns=vect_neg.get_feature_names())
X_df_neg1 = pd.concat([X_df_neg, pd.DataFrame(negative.filter(['listing_id', 'date'])).set_index(negative['listing_id'])], axis = 1, join_axes=[X_df_neg.index])
negative_df = pd.DataFrame(X_df_neg1.set_index(['listing_id', 'date']).stack(), columns = ['tfidf'])
negative_df.index.names = ['listing_id', 'date', 'word']
negative_df = negative_df[negative_df.tfidf != 0]
negative_df.head(20)
#negative_df.to_csv("negative_tfidf.csv")
Explanation: Negative Reviews
End of explanation
test = positive['eng_comments'][0:50000]
vect_pos = TfidfVectorizer(min_df=50, max_df=0.8, token_pattern=r'\b[A-Za-z]{3,}\b', stop_words=stopwords)
X_pos = vect_pos.fit_transform(positive['eng_comments'])
X_df_pos = pd.DataFrame(X_pos.toarray(), index=positive['listing_id'], columns=vect_pos.get_feature_names())
X_df_pos1 = pd.concat([X_df_pos, pd.DataFrame(positive.filter(['listing_id', 'date'])).set_index(positive['listing_id'])], axis = 1, join_axes=[X_df_pos.index])
positive_df = pd.DataFrame(X_df_pos1.set_index(['listing_id', 'date']).stack(), columns = ['tfidf'])
positive_df.index.names = ['listing_id', 'date', 'word']
positive_df = positive_df[positive_df.tfidf != 0]
positive_df.head(20)
#positive_df.to_csv("positive_tfidf.csv")
Explanation: Positive Reviews
End of explanation
<END_TASK>