From 9afc956d01fcf70070100ae230bb9c102289e05a Mon Sep 17 00:00:00 2001 From: Albert Tan Date: Tue, 7 Mar 2023 20:19:27 +0800 Subject: v1.2 --- review_it.py | 63 +++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/review_it.py b/review_it.py index 7d70e11..1db5d38 100644 --- a/review_it.py +++ b/review_it.py @@ -1,11 +1,12 @@ ''' Updated: -- More flexibility when judging whether answer is correct - -To be updated: - Better ways to check syntax - Let user judge whether answer is correct - Zero / single / plural in final output + +To be updated: +- Learn answers +- Make orders not matter - Remove unnecessary spaces or line breaks in input file - Better ways to handle each line - Answer judged correct if contains keyword @@ -24,33 +25,33 @@ try: def check_string(line): - return bool(re.match("[!#-~] [\w\d\s]*", line)) + return bool(re.match("([-~] [\w\d\s\?\.\"\(\)\[\]\{\}!,;'/]*\Z)|(! version: \d+\.?\d*\s?\Z)|(# [\w\d\s\?\.\"\(\)\[\]\{\}!,;'/]*: [\w\d\s\?\.\"\(\)\[\]\{\}!,;'/]*\Z)", line)) def compare_string(answer, response): - diff = difflib.ndiff(answer, response) + diff = list(difflib.ndiff(answer, response)) diff_count = 0 for line in diff: if line[0] != " ": diff_count += 1 - return 1 - (diff_count / len(response)) + return round(1 - (diff_count / len(diff)), 4)*100 file_name = input("Enter file name (with postfix) >> ") if file_name == "": - file_name = "test_1.1.rvwt" + file_name = "test_1.2.rvwt" with open(file_name) as f: data = f.readlines() count_prompts = 0 count_correct = 0 - correct_param = 0.66 + correct_param = 0.8 incorrect = [] if len(data) == 0: raise Exception("Error: file empty") - - if data.pop(0).split(".")[0] != "! version: 1": + + if data.pop(0).split(".")[0].replace("\n", "") != "! version: 1": raise Exception("Error: file version not provided or does not match") for line_index in range(len(data)): @@ -68,8 +69,6 @@ try: elif line[0] == "#": count_prompts += 1 line = line[2:].split(":") - if len(line) != 2: - raise Exception(f"Error: invalid syntax at line {line_index+2} of file '{file_name}'") prompt = line[0] answer = line[1][1:-1] response = input(prompt + ">> ") @@ -77,29 +76,49 @@ try: print("Correct") count_correct += 1 elif compare_string(response.lower(), answer.lower()) >= correct_param: - print(f"Probably correct: answer is '{answer}', similarity {round(compare_string(response.lower(), answer.lower()), 4)*100}%") - count_correct += 1 + print(f"Probably correct: answer is '{answer}', similarity {round(compare_string(response.lower(), answer.lower()), 4)}%") + judge = input("Is your answer correct? [y/n] ").lower() + if judge == 'n': + incorrect.append((prompt, answer)) + else: + count_correct += 1 + if judge != "y": + print("Warning: invalid input, answer judged as correct") else: - print(f"Incorrect: answer is '{answer}'") - incorrect.append((prompt, answer)) + print(f"Probably incorrect: answer is '{answer}', similarity {round(compare_string(response.lower(), answer.lower()), 4)}%") + judge = input("Is your answer correct? [y/n] ").lower() + if judge == 'y': + count_correct += 1 + else: + incorrect.append((prompt, answer)) + if judge != "n": + print("Warning: invalid input, answer judged as incorrect") else: continue - - print(f"\n\nOut of {count_prompts} questions, {count_correct} were answered correctly, which is {round(count_correct / count_prompts, 4)*100}%. ") + if count_prompts <= 1: + print(f"\n\nOut of {count_prompts} question, ", end="") + else: + print(f"\n\nOut of {count_prompts} questions, ", end="") + if count_correct <= 1: + print(f"{count_correct} was answered correctly, ", end="") + else: + print(f"{count_correct} were answered correctly, ", end="") + print(f"which is {round(count_correct / count_prompts, 4)*100}%. ") if count_prompts == count_correct: print("Congratulations for finishing perfectly! ") else: - print(f"The questions with incorrect responses are listed below: \n") + if count_prompts - count_correct <= 1: + print(f"The question with incorrect response is listed below: \n") + else: + print(f"The questions with incorrect responses are listed below: \n") for line in incorrect: (prompt, answer) = line print(prompt + "-> " + answer) except Exception as e: - print("\n" + str(e)) + print("\nException: " + str(e)) except KeyboardInterrupt: print("\nProcess quitted") -except BaseException as e: - print("\nException: " + str(e)) -- cgit v1.2.3