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/工具v4/批量收录新题.py

122 lines
4.7 KiB
Python

from PySide6.QtWidgets import QWidget, QApplication, QFileDialog
from Ui_批量收录新题 import Ui_Form
from database_tools_2 import *
class MyWindow_bdsl(QWidget,Ui_Form):
def __init__(self,database_name):
super().__init__()
self.database_name = database_name
self.setupUi(self)
self.bind()
def setdbname(self,string):
self.database_name = string
# print(self.database_name)
def bind(self):
self.suffix_checked = True
self.checkBox_suffix.toggled.connect(self.togglesuffix)
self.pushButton_step1.clicked.connect(self.execstep1)
self.pushButton_step2.clicked.connect(self.execstep2)
self.pushButton_commit.clicked.connect(self.commitchange)
def togglesuffix(self):
self.suffix_checked = self.checkBox_suffix.isChecked()
if self.suffix_checked:
self.lineEdit_suffix.setEnabled(True)
else:
self.lineEdit_suffix.setDisabled(True)
def execstep1(self):
colors = ["green","orange","blue"]
templatepath = "./模板文件/讲义模板.txt"
mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = self.database_name)
mycursor = mydb.cursor()
mycursor.execute("SELECT ID,content FROM problems;")
ret_list = mycursor.fetchall()
pro_dict = {i:j for i,j in ret_list}
treated_dict = {i:pre_treating(pro_dict[i]) for i in pro_dict.keys()}
mydb.close()
data_raw = self.plainTextEdit_raw.toPlainText()
if data_raw.strip().startswith("\\item"):
data_raw = "\\begin{enumerate}\n"+data_raw
if data_raw.count("\\begin{enumerate}") == data_raw.count("\\end{enumerate}") + 1:
data_raw = data_raw + "\n\\end{enumerate}"
problems_raw = re.findall(r"\\begin\{enumerate\}([\s\S]*?)\\end\{enumerate\}",data_raw)
data = "\n".join([item.strip() for item in problems_raw])
problems = [(d.strip()) for d in data.split("\\item") if not d.strip() == ""]
output = "使用说明:\\\\\n rep??????表示使用已有题号??????,\\\\\n s??????表示和??????相同,\\\\\n r??????表示和??????相关\n\n\\begin{enumerate}\n\n"
for p in problems:
p_treated = pre_treating(p)
t = stringmaxsim(p_treated,treated_dict,5)
psrstring = ""
for id,simrate in t:
if simrate == 1:
psrstring = "rep"+id+","
else:
psrstring += id + ","
if psrstring[-1] == ",":
psrstring = psrstring[:-1]
output += f"\\item [{psrstring}] {p}\n\n"
count = 0
for id,simrate in t:
if simrate > 0.5:
colors = get_color(simrate*2-1)
output += "\n\\definecolor{mycolor}{rgb}"+colors
output += "\n\\begin{tcolorbox}"+f"[colback = mycolor, opacityback = 0.25, colframe = orange!10!white, breakable]\n"
output += f"{simrate:.3f} \\ {id}\n\n"
content = pro_dict[id]
output += f"{content}\n"
output += "\\end{tcolorbox}\n"
count += 1
output += "\n\n"
output += "\n\\end{enumerate}\n\n"
texraw = ReadTextFile(templatepath)
texdata = StringSubstitute(r"<<[\s\S]*?待替换[\s\S]*?>>",texraw,("新题比对"+f"{GetDate()}",output))
self.plainTextEdit_compared.setPlainText(texdata)
def execstep2(self):
starting_id = self.lineEdit_startingid.text().strip().zfill(6)
raworigin = self.lineEdit_origin.text().strip()
data = self.plainTextEdit_compared.toPlainText()
editor = self.lineEdit_editor.text().strip()
if self.checkBox_suffix.isChecked():
Indexed = True
else:
Indexed = False
idlistpath = "文本文件/新题收录列表.txt"
problems = GenerateProblemListFromString2024(data)
# pro_dict = load_dict("../题库0.3/Problems.json")
nextspareid = NextSpareID(starting_id,self.database_name)
self.db = connect(hostname = db_host, port= db_port, username= db_user, pwd = db_pwd, db= self.database_name)
self.cursor = self.db.cursor()
idlist = AddProblemstoDict2024nocommit(nextspareid,raworigin,problems,editor,Indexed,self.cursor)
AppendTextFile(f"{GetDate()}-{GetTime()}\n{generate_exp(idlist)}",idlistpath)
os.system(f"code {idlistpath}")
def commitchange(self):
self.db.commit()
self.db.close()
print("已提交至数据库")
if __name__ == '__main__':
app = QApplication([])
windows = MyWindow_bdsl("tikutest")
windows.show()
app.exec()