From e3a4762a175161a61ee818260e39e16ccabadd99 Mon Sep 17 00:00:00 2001 From: p38654902 Date: Thu, 31 Oct 2024 18:22:45 +0800 Subject: [PATCH] ADD file via upload --- My_project/knowledge_api.py | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 My_project/knowledge_api.py diff --git a/My_project/knowledge_api.py b/My_project/knowledge_api.py new file mode 100644 index 0000000..f3be388 --- /dev/null +++ b/My_project/knowledge_api.py @@ -0,0 +1,55 @@ +import re +import json + + +# 加载测试数据 +def load_test_data(file_path): + test_data = [] + try: + with open(file_path, 'r', encoding='utf-8') as file: + for line in file: + test_data.append(json.loads(line.strip())) + except Exception as e: + print(f"加载测试数据时发生错误: {e}") + return test_data + + +# 保存提交结果 +def save_submissions_to_jsonl(submissions, output_file_path): + try: + with open(output_file_path, 'w', encoding='utf-8') as file: + for submission in submissions: + json.dump(submission, file, ensure_ascii=False) + file.write('\n') + print(f"提交的结果已保存到 {output_file_path}") + except Exception as e: + print(f"保存提交结果时发生错误: {e}") + + +# 检测答案中的选项 +def detect_choice_in_answer(answer_text): + possible_choices = ['A', 'B', 'C', 'D'] + + for choice in possible_choices: + if re.search(rf'(?= 3: + code_block = parts[1].strip() # 提取第一个 ``` 和第二个 ``` 之间的部分 + + # 如果代码块中包含 "python\n",则删除它 + if "python\n" in code_block: + code_block = code_block.replace("python\n", "") + + return code_block + else: + return "" # 如果没有找到成对的 ```,返回空字符串 \ No newline at end of file