<jupyter_start><jupyter_text>Final Project 2 - NoSQL Database for AirBnB<jupyter_code>import pandas as pd
import json
from bson import json_util
from pymongo import MongoClient
client = MongoClient()
db = client.airbnb<jupyter_output><empty_output><jupyter_text>Answering The Questions Using Twitter<jupyter_code>#. What are people saying about me (somebody)?
#Ans. Fetch the Reviews for each of the matching Listing_id between Reviews and Property_Listings Collections.

cursor = db.Reviews.aggregate([ { "$lookup" :  {  "from": "Property_listings",  "localField": "listing_id",  "foreignField": "listing_id",  "as":"match_reviews"   } }])
i=0
for review in cursor:
    if i > 5:
        break
    else:
        print(review['listing_id'], review['comments'])
        print("----------")
        i+=1
    
#xi. Should I add and picture or url to my post?
#Ans. Check for likes/retweets of posts with and without URL.
#     IF count is of posts' likes/retweets with URL > count of posts' likes/retweets without URL
#     THEN
#     URL is should be metioned in post
#     ELSE
#     No need to add URL to post. 
c1 = db.TweetDetils.aggregate([ {"$match":{"urls":{"$size":0}}}, {"$group" : {"_id":"null", "score":{"$sum":"$retweet_cnt"}}},  {"$sort":{"score" : -1}}])
cnt_url=0
cnt_no_url = 0
for rec in c1:
    cnt_url = rec["score"]

c2 = db.TweetDetils.aggregate([ {"$match":{"urls":{"$size":1}}}, {"$group" : {"_id":"null", "score":{"$sum":"$retweet_cnt"}}},  {"$sort":{"score" : -1}}])
for rec in c2:
    cnt_no_url = rec["score"]
    
if cnt_url >= cnt_no_url:
    print("Should Contain URL")
else:
    print("Should not contain url")

#ii. How viral are my posts?
#Ans. Get the sum of all the fav_cnt and group by users order by fav_cnt desc;
#     The user with most counts has the most viral posts.
cursor = db.TweetDetils.aggregate([ {"$group" : {"_id":"$user_name", "score":{"$sum":"$fav_cnt"}}},  {"$sort":{"score" : -1}},{"$limit":5}])
for rec in cursor:
    print(rec["_id"], rec["score"])
#iii. How much influence to my posts have?
#Ans. Get the sum of all the retweeted tweets and group by users  
#     The users with most counts is most influencial
cursor = db.TweetDetils.aggregate([ {"$group" : {"_id":"$user_name", "score":{"$sum":"$retweet_cnt"}}},  {"$sort":{"score" : -1}},{"$limit":5}])
for rec in cursor:
    print(rec["_id"], rec["score"])<jupyter_output>PAKO0699 13682
arianaisquaking 6841
Theus_Antonny 6841
intoyougabi 6841
theesztikocsis 6841<jupyter_text>Answering The Questions Using Facebook<jupyter_code>#vii What topics are trending in my domain?
#Ans Top 5 popular hashtag within my domain will tell us the topics
#which are trending
 db.facebook_tags.aggregate([ 
{"$unwind":"$hashtags"},
{"$group" : {"_id":"$hashtags", "score":{"$sum":1}}},
{"$sort":{"score" : -1}},{"$limit":5} ])
 
{ "_id" : "airbnb", "score" : 1899965 }
{ "_id" : "airbnbhost", "score" : 101419}
{ "_id" : "airbnbphoto", "score" : 99810 }
{ "_id" : "airbnblife", "score" : 60267 }
{ "_id" : "airbnbexperience", "score" : 47198}
#vi Who should I be following?
#Popular Pages with the most followers,this can suggest who should i be following in my domain 
db.pagedata.aggregate( [
   { $group: { _id: "$page", total_fan_count: { $sum: "$fan_count" } } },
   { $match: { total_fan_count: { $gte: 100 } } }
] )
:
{
  "_id" : "airbnb",
  "total_fan_count" : 12362251
}
#xii Whats my reach?
#Ans The calculation of my reach , I am going to measure using fan count. 
#The fan count of various pages of airbnb.
db.pagedata.aggregate( [
  { $match: { $or: [ { fan_count: { $gt: 70, $lt:900 } }, { fan_count: { $gte: 1000 } } ] } },
  { $group: { _id: null, count: { $sum: 1 } } }
] );
{ "_id" : null, "count" : 5 }
#viii What Keywords and Hashtags should I add to my post?
#Ans The most poplular tags but during a time duration.
db.facebook_tags.aggregate([  
{"$match": {"feed_data_created_time": {"$gt": new ISODate('2017-12-25')}}},
{"$unwind":"$hashtags"},
{"$group" : {"_id":"$hashtags", "score":{"$sum":1}}}, 
{"$sort":{"score" : -1}},
{"$limit":3} ])
{ "_id" : "airbnb", "score" : 8965 }
{ "_id" : "airbnbexperience", "score" : 1998}
{ "_id" : "airbnblife", "score" : 676 }<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18617905485632866, 'fraction_numerical': 0.02825932082640703, 'mean_word_length': 4.534822601839685, 'pattern_counts': {'":': 43, '<': 12, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '551715', 'license': 'MIT', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1535, 'n_words': 447, 'path': 'Project2/NoSQL_MongoDB_Airbnb_Project.ipynb', 'repo_name': 'chandni895/Airbnb-Database', 'size': '4211'}