# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
# Import Statements
import os
import csv
import re
from numpy import *
import sys
from datetime import datetime

# Setting path to my data files and storing path for each file in the files variable
path = 'C:/Users/sandy/PycharmProjects/DataManagement/hotels'
files = [i for i in os.listdir(path) if os.path.isfile(os.path.join(path,i)) and \
         'hotel_' in i]

# for each file, read its contents, and store the necessary data into specific variables
for file in files:
    filename = file
    g = open(path + "/"+filename, "r",newline="\n",errors = 'ignore')
    d = g.read()
    l = ''
    for i in range(1,16):
        review = d.split("<Author>")[i]
        author = review.split("<Content>")[0].replace("\r\n","")
        content = review.split("<Content>")[1].split("<Date>")[0].replace("\r\n","")
        date = review.split("<Date>")[1].split("\r\n")[0].replace("\r\n","")
        date = datetime.strptime(date, '%b %d, %Y')
        date = date.strftime('%Y-%m-%d')
        overall = review.split("<Overall>")[1].split("<Value>")[0].replace("\r\n","")
        value = review.split("<Value>")[1].split("<Rooms>")[0].replace("\r\n","")
        rooms = review.split("<Rooms>")[1].split("<Location>")[0].replace("\r\n","")
        location = review.split("<Location>")[1].split("<Cleanliness>")[0].replace("\r\n","")
        cleanliness = review.split("<Cleanliness>")[1].split("<Check in / front desk>")[0].replace("\r\n","")
        checkin = review.split("<Check in / front desk>")[1].split("<Service>")[0].replace("\r\n","")
        service = review.split("<Service>")[1].split("<Business service>")[0].replace("\r\n","")
        bservice = review.split("<Business service>")[1].replace("\r\n","")
        hotel_id = filename.split("_")[1].split(".dat")[0]

#         Concatenate the variables into the form of mongodb insert statements
        string = "db.hotels.insert([{ " + "\"Hotel_id\" : " + hotel_id + ",\"Author\" : \"" + author + "\" , \"Date\" : new Date(\"" + date + "\" ), \"Overall\" : "+ overall
        string1 = string + ", \"Value\" : " + value + ", \"Room\" : " + rooms + ", \"Location\" : " + location + ", \"Cleanliness\" : " + cleanliness
        insert = string1 + ", \"Checkin/frontdesk\" : " + checkin + ", \"Service\" : " + service + ", \"BService\" : " + bservice  + ",\"Reviews\" : \"" + content +  "\",\"SentimentRate\" : 0.427, \"Sentiment\" : \"Positive\"}])"
        l = l + insert + "\n\n\n"
#     make a file with same name as original data file and write the mongo db insert statements, and repeating this for all the data files.
    filewrite = open( "C:/Users/sandy/PycharmProjects/DataManagement/inserts/"+ str(filename.split("_")[1].split(".dat")[0]) + "_insert.txt","w") 
    filewrite.write(l) 
    filewrite.close()

StarcoderdataJupyterScriptsDedupFiltered