-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
105 lines (78 loc) · 2.97 KB
/
db.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
from pymongo import MongoClient
from dotenv import load_dotenv
import os
import json
import ai
import re
load_dotenv()
client = MongoClient(os.getenv("mongo"))
db = client["trace"]
collection = db["comments"]
def insert(data):
collection.insert_one(data)
def getTeachers():
return collection.distinct("teacherID")
def selectTeacher(id):
teachers = collection.find({"teacherID": id})
all_good_comments = []
all_bad_comments = []
name = ""
for teacher in teachers:
for c in teacher["comments"]["good_comments"]:
all_good_comments.append(c)
for c in teacher["comments"]["bad_comments"]:
all_bad_comments.append(c)
name = teacher["teacherName"]
data = {
'teacherID': id,
'teacherName': name,
'all_good_comments': all_good_comments,
'all_bad_comments': all_bad_comments,
'good_summary': '',
'bad_summary': ''
}
x = db["allComments"]
x.insert_one(data)
def selectTeacherAllCommentsAndSummarize(id):
x = db["allComments"]
comments = x.find_one({"teacherID": id})
goodCommentsString = ""
badCommentsString = ""
for y in comments["all_good_comments"]:
goodCommentsString += y + ", "
for y in comments["all_bad_comments"]:
badCommentsString += y + ", "
goodWords = ai.summarize("positive", goodCommentsString)
badWords = ai.summarize("critical", badCommentsString)
x.update_one({"teacherID": id}, {"$set": {"good_summary": goodWords}})
x.update_one({"teacherID": id}, {"$set": {"bad_summary": badWords}})
def _buildJSON(comments):
if comments:
comments.pop("_id", None)
json_document = json.dumps(comments, default=str, indent=4)
return json_document
else:
return json.dumps({}, default=str, indent=4)
# methods for usage in the API
def apiSelectTeacher(id):
x = db["allComments"]
comments = x.find_one({"teacherID": id})
return _buildJSON(comments)
def searchTeacher(q):
q = re.escape(q)
x = db["allComments"]
query = {"teacherName": {"$regex": q, "$options": "i"}} # Case-insensitive regex
search = x.find(query).limit(10)
results = []
for result in search:
results.append({"id": result["teacherID"], "name": result["teacherName"], "goodCount": len(result["all_good_comments"]), "badCount": len(result["all_bad_comments"]), "url": "/teacher/" + str(result["teacherID"])})
return results
def getRandomFrontend(count:int):
x = db["allComments"]
data = x.aggregate([{"$sample": {"size": count}}])
formatData = []
for d in data:
goodL = len(d["all_good_comments"])
badL = len(d["all_bad_comments"])
formatData.append({"teacherName": d["teacherName"], "url": "/teacher/" + str(d["teacherID"]), "description": f"{str(goodL)} positive comments, {str(badL)} critical comments"})
return formatData