-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_connection_mongo_solution.py
178 lines (147 loc) · 5.24 KB
/
db_connection_mongo_solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#-------------------------------------------------------------------------
# AUTHOR: your name
# FILENAME: title of the source file
# SPECIFICATION: description of the program
# FOR: CS 4250- Assignment #2
# TIME SPENT: how long it took you to complete the assignment
#-----------------------------------------------------------*/
#IMPORTANT NOTE: DO NOT USE ANY ADVANCED PYTHON LIBRARY TO COMPLETE THIS CODE SUCH AS numpy OR pandas. You have to work here only with
# standard arrays
#importing some Python libraries
# --> add your Python code here
import pprint
import pymongo
import sys, traceback
import datetime
def remove_punctuation(input):
input = str(input).strip()
char_remove = ['.','?','!',',',';',':','-','(',')','[',']','{','}','\'','"']
for char in char_remove:
input = input.replace(char, '')
return input
def count_term(term_list, chk_term):
count = 0
for elem in term_list:
if elem == chk_term:
count += 1
return count
def createTermList(docText_cleansing):
found_terms = docText_cleansing.lower().split(' ')
print(found_terms)
# Convert the list to a set
rm_set = set(found_terms)
# Convert the set back to a list
distinct_terms = list(rm_set)
print(distinct_terms)
term_list = []
for term in distinct_terms:
term_list.append({"term": str(term).lower(), "num_char": len(term), "num_term": count_term(found_terms, term)})
print(term_list)
return term_list
def connectDataBase():
# Create a database connection object using pymongo
# --> add your Python code here
try:
client = pymongo.MongoClient(host="localhost", port=27017)
print("---Client---")
print(client)
db = client.library
print("---DB---")
print(db)
print("---Current DB List---")
print(client.list_database_names())
return db
except Exception as error:
traceback.print_exc()
print("Database not connected successfully")
def createDocument(col, docId, docText, docTitle, docDate, docCat):
print("Create Docs")
print(docId, docText, docTitle, docDate, docCat)
docText_cleansing = remove_punctuation(docText)
numChar = len(docText_cleansing.replace(' ', ''))
if docDate == '':
create_date = datetime.datetime.now()
else:
create_date = datetime.datetime.strptime(docDate, "%Y-%m-%d")
doc = {
"doc_no": int(docId),
"title": str(docTitle),
"text": str(docText),
"num_char": numChar,
"created_at": create_date,
"category": docCat,
"terms": createTermList(docText_cleansing)
}
# create a dictionary to count how many times each term appears in the document.
# Use space " " as the delimiter character for terms and remember to lowercase them.
# --> see the method "createTermList"
# create a list of dictionaries to include term objects.
# --> see the method "createTermList"
#Producing a final document as a dictionary including all the required document fields
# --> see the method "createTermList"
# Insert the document
# --> add your Python code here
result = col.insert_one(doc)
print(result.inserted_id)
print("Doc has been Created...")
def deleteDocument(col, docId):
print("Delete Docs")
# Delete the document from the database
# --> add your Python code here
find_doc = col.find_one({"doc_no": int(docId)})
print(find_doc)
_id = find_doc["_id"]
print(_id)
col.delete_one({"_id": _id})
print("Doc Deleted...")
def updateDocument(col, docId, docText, docTitle, docDate, docCat):
# Delete the document
# --> add your Python code here
print("Delete Docs")
deleteDocument(col, docId)
# Create the document with the same id
# --> add your Python code here
print("Recreate Docs")
createDocument(col, docId, docText, docTitle, docDate, docCat)
print("Doc Updated...")
def getIndex(col):
print("Get Indexes")
pipeline = [
{
'$unwind': {
'path': '$terms'
}
}, {
'$group': {
'_id': [
'$title', '$terms.term'
],
'cnt': {
'$sum': '$terms.num_term'
}
}
}, {
'$sort': {
'terms.term': 1
}
}
]
docs = col.aggregate(pipeline)
print(docs)
index_dict = {}
tmp = ''
for data in docs:
title = data['_id'][0]
term = data['_id'][1]
num_term = data['cnt']
if tmp != term:
value_str = title+':'+str(num_term)
tmp = term
else:
value_str += ', '+title + ':' + str(num_term)
index_dict.update({term: value_str})
return index_dict
# Query the database to return the documents where each term occurs with their corresponding count. Output example:
# {'baseball':'Exercise:1','summer':'Exercise:1,California:1,Arizona:1','months':'Exercise:1,Discovery:3'}
# ...
# --> add your Python code here