Spaces:
Sleeping
Sleeping
| import json | |
| import random | |
| import re | |
| import os | |
| from pymongo import MongoClient | |
| import certifi | |
| from bson import ObjectId | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| CONNECTION_STRING = os.getenv("CONNECTION_STRING") | |
| DB_NAME = os.getenv("DB_NAME") | |
| COLLECTION_NAME = os.getenv("COLLECTION_NAME") | |
| def get_data(filepath): | |
| with open(filepath, "r") as f: | |
| data = json.load(f) | |
| return data | |
| def get_score(user_id): | |
| client = MongoClient(CONNECTION_STRING) | |
| db = client["sattvastha"] | |
| collection = db["completedassessments"] | |
| score = {} | |
| limit = 1 | |
| user_id_obj = user_id | |
| score = {} | |
| limit = 1 | |
| # user_id = ObjectId('69397eaa34ad3c5ebda7022a') | |
| doc = (collection.find({"userId": user_id_obj,"templateName": "Chitta Bhumi"}).sort("completedAt", -1).limit(limit)) | |
| try: | |
| latest_entry = doc.next() | |
| # print(latest_entry) | |
| except StopIteration: | |
| return ("No documents found for this user.") | |
| score = {} | |
| interpret = {"1. Never true for me":1,"2. Rarely true for me":2,"3. Sometimes true for me":3,"4. Often true for me":4,"5. Always true for me":5} | |
| for item in latest_entry["answers"]: | |
| # print(item["answer"]) | |
| score[item["questionId"]] = 0 | |
| for item in latest_entry["answers"]: | |
| try: | |
| score[item["questionId"]] += interpret[item["answer"]] | |
| except: | |
| print("something not found here") | |
| # print(score) | |
| groups = {"K":"Kshipta", "Mudha":"Mudha", "Vikashipta":"Vikshipta", "Ekagra":"Ekagra", "Nirodha":"Nirodha"} | |
| # groups = ["", "Mudha", "Vikshipta", "Ekagra", "Nirodha"] | |
| normalized = {} | |
| for g,name in groups.items(): | |
| group_keys = [f"{g}_{i}" for i in range(1,6)] | |
| total = sum(score[k] for k in group_keys) | |
| normalized[name] = total / 5 | |
| return normalized | |
| # doc = (collection.find({"userId": user_id_obj}).sort("completedAt", -1).limit(limit)) | |
| # docs = list(doc) | |
| # docs[0] | |
| # for i in range(len(docs[0]["answers"])): | |
| # score[docs[0]["answers"][i]["questionId"]] = 0 | |
| # for i in range(len(docs[0]["answers"])): | |
| # score[docs[0]["answers"][i]["questionId"]] +=1 | |
| # # print(score) | |
| # normalized_scores = {key: value / 5 for key, value in score.items()} | |
| # # print(normalized_scores) | |
| # # print("\n") | |
| # return normalized_scores | |
| # score = {} | |
| # for key in data.keys(): | |
| # print(f"Please enter a score between 1-5 for each of the questions under {key.upper()}, (1 = Never/Rarely True, 2 = Occasionally True, 3 = Sometimes True, 4 = Often True, 5 = Almost Always True)") | |
| # score[key] = 0 | |
| # for i in range(len(data[key])): | |
| # print(data[key][i]) | |
| # # user = int(input("How much do you resonate with it?")) | |
| # user = random.randint(1,5) | |
| # score[key] += user | |
| # # print("\n") | |
| # normalized_scores = {key: value / 5 for key, value in score.items()} | |
| # return normalized_scores | |
| def get_raw_interpretation(key): | |
| with open("interpretations.json", "r") as f: | |
| interpretations = json.load(f) | |
| return interpretations[key] | |
| def get_definitions(key): | |
| with open("definitions.json", "r") as f: | |
| definitions = json.load(f) | |
| return definitions[key] | |
| def get_curetasks(key): | |
| with open("task.json", "r") as f: | |
| task = json.load(f) | |
| return task[key] | |
| def extract_tasks(text): | |
| daily_pattern = re.compile( | |
| r"(daily[_\s]*tasks?\b.*?)(?=\bweekly[_\s]*tasks?\b|\Z)", | |
| re.IGNORECASE | re.DOTALL | |
| ) | |
| weekly_pattern = re.compile( | |
| r"(weekly[_\s]*tasks?\b.*)", | |
| re.IGNORECASE | re.DOTALL | |
| ) | |
| daily_match = daily_pattern.search(text) | |
| weekly_match = weekly_pattern.search(text) | |
| daily_tasks = daily_match.group(1).strip() if daily_match else None | |
| weekly_tasks = weekly_match.group(1).strip() if weekly_match else None | |
| return daily_tasks, weekly_tasks | |