From 108ba55b5de6c49a0f5d82e7f3887b4254ca5c76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=BChler=20Ma=C3=ABlys?= Date: Wed, 10 Apr 2024 10:09:34 +0200 Subject: [PATCH] use jellyfish to check is answer is close to right answer --- api/masteriqapp/views/QuestionView.py | 27 ++++++++++++++++++-------- api/requirements.txt | Bin 458 -> 530 bytes 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/api/masteriqapp/views/QuestionView.py b/api/masteriqapp/views/QuestionView.py index 046c0bd..ca938b1 100644 --- a/api/masteriqapp/views/QuestionView.py +++ b/api/masteriqapp/views/QuestionView.py @@ -1,6 +1,6 @@ import io import random - +import jellyfish from rest_framework import viewsets from rest_framework.decorators import action from django.apps import apps @@ -15,12 +15,25 @@ masteriq = apps.get_app_config("masteriqapp") +def is_number(s): + try: + float(s) + return True + except ValueError: + return False + + def check_if_text_answer_is_correct(given_answer, right_answer): - if (right_answer.lower() == "yes" or right_answer.lower() == "true" or right_answer.lower() == "right") and (given_answer.lower() == "yes" or given_answer.lower() == "true" or given_answer.lower() == "right") : + if (right_answer.lower() == "yes" or right_answer.lower() == "true" or right_answer.lower() == "right") and ( + given_answer.lower() == "yes" or given_answer.lower() == "true" or given_answer.lower() == "right"): return True - if (right_answer.lower() == "no" or right_answer.lower() == "wrong" or right_answer.lower() == "false") and (given_answer.lower() == "no" or given_answer.lower() == "wrong" or given_answer.lower() == "false") : + if (right_answer.lower() == "no" or right_answer.lower() == "wrong" or right_answer.lower() == "false") and ( + given_answer.lower() == "no" or given_answer.lower() == "wrong" or given_answer.lower() == "false"): return True - if given_answer.lower() == right_answer.lower(): + if is_number(right_answer): + if given_answer == right_answer: + return True + elif jellyfish.damerau_levenshtein_distance(given_answer.lower(), right_answer.lower()) < 3: return True return False @@ -111,7 +124,7 @@ def answer_text(self, request): "answer_sent": request.data['answer']} del request.session['question'] del request.session['options_asked'] - #TODO: add points to user when connexion is implemented + # TODO: add points to user when connexion is implemented return Response(status=status.HTTP_200_OK, data=data_to_send) @action(detail=False, methods=["POST"], url_path="answer_option") @@ -134,7 +147,7 @@ def answer_options(self, request): "answer_sent": answer_sent.text} del request.session['question'] del request.session['options_asked'] - #TODO: add points to user when connexion is implemented + # TODO: add points to user when connexion is implemented return Response(status=status.HTTP_200_OK, data=data_to_send) @action(detail=False, methods=["GET"]) @@ -144,5 +157,3 @@ def options_asked(self, request): else: data_to_send = {"options_asked": request.session['options_asked']} return Response(status=status.HTTP_200_OK, data=data_to_send) - - diff --git a/api/requirements.txt b/api/requirements.txt index 5d7a11c6385db113edfad96b69000441b7e09b03..0dbaa5429797009b34c116d2fc235fca515937d3 100644 GIT binary patch delta 111 zcmX@bJc(t3&*U;joyl2@JQIEN%#64W%`3xmML-H8PP_!COd|L|um#7$q delta 45 zcmbQla*BC^&%_qJiP!8Vn=m>|Ucgv1v4Ur!kIv*gMk&5J23rP020aD?AU0$G0B$S{ A#{d8T