From ba573b4dce5352a8b633521354e0ef9fde9cc191 Mon Sep 17 00:00:00 2001 From: "weiye.wang" Date: Tue, 6 Feb 2024 22:21:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=88=96=E5=89=AA=E8=B4=B4=E6=9D=BF=E6=8F=90=E5=8F=96=E7=AD=94?= =?UTF-8?q?=E6=A1=88=20=E5=8A=9F=E8=83=BD=E5=B9=B6=E5=8A=A0=E5=85=A5?= =?UTF-8?q?=E9=9D=A2=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 工具v2/database_tools.py | 25 +++++++++++++++++++++++++ 工具v2/工具面板.py | 2 +- 工具v2/文件或剪贴板提取答案.py | 20 ++++++++++++++++++++ 工具v2/文本文件/config.json | 5 +++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 工具v2/文件或剪贴板提取答案.py diff --git a/工具v2/database_tools.py b/工具v2/database_tools.py index 82d9acf2..bb93b240 100644 --- a/工具v2/database_tools.py +++ b/工具v2/database_tools.py @@ -1803,6 +1803,31 @@ def getindex(string,pos = 2): para = string.split(".") return int(para[pos-1]) +def generateListOfIDandContent(string): #根据标准的讲义LaTeX源码字符串(可能含答案)生成一个list, 每一项是一个(id,id后内容)的tuple + return re.findall(r"\((\d{6})\)([\s\S]*?)(?:(?:\\item)|(?:\\end\{enumerate\}))",string) + +def CountEffectiveBraces(string): #在string中统计有效的(LaTeX的\{和\}不算作有效)的大括号的个数, 返回(左大括号个数, 右大括号个数) + string_ref = re.sub(r"\\[\{\}]"," ",string) + return (string_ref.count("{"),string_ref.count("}")) + +def generateAnswerTex(content,anspreamble = "答案: \\textcolor{red}{"): #在从anspreamble开始的字符串后找到答案字符串(anspreamble中的字符不算) + startpos = content.index(anspreamble) + len(anspreamble) - 1 + endpos = startpos + 1 + l,r = CountEffectiveBraces(content[startpos:endpos]) + while not l == r: + endpos += 1 + l,r = CountEffectiveBraces(content[startpos:endpos]) + return content[startpos+1:endpos-1].strip() + +def generateAnswerList(string,anspreamble = "答案: \\textcolor{red}{"): #从LaTeX源代码string中分析题号与对应的答案 + alist = generateListOfIDandContent(string) + anslist = [] + for a in alist: + id,content = a + if anspreamble in content: + anslist.append((id,generateAnswerTex(content,anspreamble))) + return anslist + if __name__ == "__main__": print("数据库工具, import用.") \ No newline at end of file diff --git a/工具v2/工具面板.py b/工具v2/工具面板.py index 958014c6..ec1e5bb7 100644 --- a/工具v2/工具面板.py +++ b/工具v2/工具面板.py @@ -92,7 +92,7 @@ MaintainenceMenu.add_command(label = "合并使用记录并排序", command = la MaintainenceMenu.add_separator() MaintainenceMenu.add_command(label = "统考数据导入", command = lambda: SetButton("统考数据导入",["统考数据导入.py"])) MaintainenceMenu.add_command(label = "手动统计结果导入", command = lambda: SetButton("手动统计结果导入",["临时文件/手动统计结果.txt"])) -MaintainenceMenu.add_command(label = "转换手打答案至metadata", command = lambda: SetButton("转换手打答案至metadata",["转换手打答案至metadata.py"])) +MaintainenceMenu.add_command(label = "文件或剪贴板提取答案", command = lambda: SetButton("文件或剪贴板提取答案",["文件或剪贴板提取答案.py"])) MaintainenceMenu.add_separator() MaintainenceMenu.add_command(label = "移除关联题号", command = lambda: SetButton("移除关联题号",["文本文件/metadata.txt"])) MaintainenceMenu.add_separator() diff --git a/工具v2/文件或剪贴板提取答案.py b/工具v2/文件或剪贴板提取答案.py new file mode 100644 index 00000000..bf76d736 --- /dev/null +++ b/工具v2/文件或剪贴板提取答案.py @@ -0,0 +1,20 @@ +from database_tools import * + +configjson = load_dict("文本文件/config.json")["文件或剪贴板提取答案.py"] +preamble = configjson["前缀"] + + +if configjson["来自剪贴板"] == True: + data = getCopy() +else: + data = ReadTextFile(configjson["文件地址"]) + +anslist = generateAnswerList(data,preamble) + +output = "ans\n\n" +for id,ans in anslist: + if not ans == "暂无答案": + output += f"{id}\n{ans}\n\n" + +SaveTextFile(output,"文本文件/metadata.txt") + diff --git a/工具v2/文本文件/config.json b/工具v2/文本文件/config.json index da319cd6..b0c46f0b 100644 --- a/工具v2/文本文件/config.json +++ b/工具v2/文本文件/config.json @@ -67,5 +67,10 @@ "备注": true, "届别": [] } + }, + "文件或剪贴板提取答案.py": { + "来自剪贴板": true, + "文件地址": "d:/temp/test5.tex", + "前缀": "答案: \\textcolor{red}{" } } \ No newline at end of file