Skip to content

Commit

Permalink
Merge pull request #98 from HE-Arc/mbu-86-unperfect-but-right-answer-…
Browse files Browse the repository at this point in the history
…also-accepted

use jellyfish to check is answer is close to right answer
  • Loading branch information
maelys-buhler authored Apr 10, 2024
2 parents 2d29665 + 108ba55 commit 1b0b39c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions api/masteriqapp/views/QuestionView.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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")
Expand All @@ -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"])
Expand All @@ -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)


Binary file modified api/requirements.txt
Binary file not shown.

0 comments on commit 1b0b39c

Please sign in to comment.