Source code for calculations.calculate_cluster
"""This module processes questionnaire responses to recommend therapy clusters"""
[docs]
def process_all_responses(responses):
"""
Processes all responses and calculates scores for each cluster.
:param responses: List of response objects from the frontend.
:type responses: list
:return: Scores for each cluster.
:rtype: dict
"""
# Initialize scores for clusters
scores = {}
# Iterate through all responses
for response in responses:
for cluster in response["cluster_points"]:
scores[cluster] = scores.get(cluster, 0) + 1
return scores
[docs]
def calculate_cluster(scores):
"""
Determines the cluster with the highest score.
:param scores: Scores for each cluster.
:type scores: dict
:return: The cluster with the highest score.
:rtype: str
"""
# Add score tuples to a list for sorting
sorted_scores = []
for cluster, score in scores.items():
sorted_scores.append((cluster, score))
# Perform bubble sort to sort scores in descending order
n = len(sorted_scores)
for i in range(n):
for j in range(0, n - i - 1):
if sorted_scores[j][1] < sorted_scores[j + 1][1]:
sorted_scores[j], sorted_scores[j + 1] = sorted_scores[j + 1], sorted_scores[j]
# Get the key of the highest score
best_cluster = sorted_scores[0][0]
return best_cluster