This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mathdeptv2/工具/批量收录题目.py

107 lines
3.5 KiB
Python

#修改起始id,出处,文件名
starting_id = 22106
raworigin = ""
filename = r"C:\Users\wangweiye\Documents\wwy sync\临时工作区\自拟题目15.tex"
editor = "20230620\t王伟叶"
indexed = True
IndexDescription = "试题"
import os,re,json
#从enumerate环境的字符串生成题目列表
def GenerateProblemListFromString(data):
try:
data = re.findall(r"\\begin\{document\}([\s\S]*?)\\end\{document\}",problems_string)[0]
except:
pass
data = re.sub(r"\n{2,}","\n",data)
data = re.sub(r"\\item",r"\\enditem\\item",data)
data = re.sub(r"\\end\{enumerate\}",r"\\enditem",data)
ProblemList_raw = [p.strip() for p in re.findall(r"\\item([\s\S]*?)\\enditem",data)]
ProblemsList = []
for p in ProblemList_raw:
startpos = data.index(p)
tempdata = data[:startpos]
suflist = re.findall(r"\n(\%\s{0,1}[\S]+)\n",tempdata)
if len(suflist) > 0:
suffix = suflist[-1].replace("%","").strip()
else:
suffix = ""
p_strip = re.sub(r"\n(\%[\S]+)$","",p).strip()
ProblemsList.append((p_strip,suffix))
return ProblemsList
# 创建新的空题目
def CreateEmptyProblem(problem):
NewProblem = problem.copy()
for field in NewProblem:
if type(NewProblem[field]) == str:
NewProblem[field] = ""
elif type(NewProblem[field]) == list:
NewProblem[field] = []
elif type(NewProblem[field]) == int or type(NewProblem[field]) == float:
NewProblem[field] = -1
return NewProblem
# 创建新题目
def CreateNewProblem(id,content,origin,dict,editor):
NewProblem = CreateEmptyProblem(dict["000001"])
NewProblem["id"] = str(id).zfill(6)
NewProblem["content"] = content
NewProblem["origin"] = origin
NewProblem["edit"] = [editor]
return NewProblem
duplicate_flag = False
with open(r"../题库0.3/Problems.json","r",encoding = "utf8") as f:
database = f.read()
pro_dict = json.loads(database)
with open(filename,"r",encoding = "utf8") as f:
problems_string = f.read()
problems = GenerateProblemListFromString(problems_string)
id = starting_id
leading_suffix = problems[0][1]
for p_and_suffix in problems:
p = p_and_suffix[0]
suffix = p_and_suffix[1]
if not leading_suffix == suffix:
starting_id = id
leading_suffix = suffix
pid = str(id).zfill(6)
if pid in pro_dict:
duplicate_flag = True
if indexed == False:
origin = raworigin + suffix
else:
origin = raworigin + suffix + IndexDescription + str(id- starting_id+1)
NewProblem = CreateNewProblem(id = pid, content = p, origin = origin, dict = pro_dict,editor = editor)
if "blank" in NewProblem["content"]:
NewProblem["genre"] = "填空题"
elif "bracket" in NewProblem["content"]:
NewProblem["genre"] = "选择题"
else:
NewProblem["genre"] = "解答题"
NewProblem["space"] = "4em"
print("添加题号"+pid+", "+"来源: " + origin)
pro_dict[pid] = NewProblem
id += 1
#按id排序生成字典
sorted_dict_id = sorted(pro_dict)
sorted_dict = {}
for id in sorted_dict_id:
sorted_dict[id] = pro_dict[id]
#将排序后的字典转为json
if not duplicate_flag:
new_database = json.dumps(sorted_dict,indent = 4,ensure_ascii=False)
#写入json数据库文件
with open(r"../题库0.3/Problems.json","w",encoding = "utf8") as f:
f.write(new_database)
else:
print("题号有重复, 请检查.\n"*5+'\n'+"未录入新题目\n"*5)