单元挂钩 迁移施工中

This commit is contained in:
wangweiye7840 2024-04-16 12:36:22 +08:00
parent e871c1f619
commit dd753c09c5
4 changed files with 261 additions and 9 deletions

View File

@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file '单元挂钩.ui'
##
## Created by: Qt User Interface Compiler version 6.6.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QLabel, QLineEdit, QPushButton,
QRadioButton, QSizePolicy, QWidget)
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(400, 131)
self.label = QLabel(Form)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(20, 40, 54, 16))
self.radioButton_phase2 = QRadioButton(Form)
self.radioButton_phase2.setObjectName(u"radioButton_phase2")
self.radioButton_phase2.setGeometry(QRect(20, 90, 191, 20))
self.lineEdit_ids = QLineEdit(Form)
self.lineEdit_ids.setObjectName(u"lineEdit_ids")
self.lineEdit_ids.setGeometry(QRect(20, 60, 271, 20))
self.pushButton_exec = QPushButton(Form)
self.pushButton_exec.setObjectName(u"pushButton_exec")
self.pushButton_exec.setGeometry(QRect(310, 10, 71, 111))
font = QFont()
font.setBold(True)
self.pushButton_exec.setFont(font)
self.radioButton_phase1 = QRadioButton(Form)
self.radioButton_phase1.setObjectName(u"radioButton_phase1")
self.radioButton_phase1.setGeometry(QRect(20, 20, 141, 20))
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"\u5355\u5143\u6302\u94a9", None))
self.label.setText(QCoreApplication.translate("Form", u"\u9898\u53f7", None))
self.radioButton_phase2.setText(QCoreApplication.translate("Form", u"\u6536\u5f55\u5355\u5143\u4fe1\u606f\u81f3metadata\u9636\u6bb5", None))
self.lineEdit_ids.setPlaceholderText(QCoreApplication.translate("Form", u"\u8f93\u5165\u9898\u53f7, \u7559\u7a7a\u8868\u793a\u9488\u5bf9\u6240\u6709\u6ca1\u6709\u5bf9\u5e94\u5355\u5143\u7684\u9898\u76ee", None))
self.pushButton_exec.setText(QCoreApplication.translate("Form", u"\u8fd0\u884c", None))
self.radioButton_phase1.setText(QCoreApplication.translate("Form", u"\u81ea\u9002\u5e94\u8d4b\u4e88\u5355\u5143\u4fe1\u606f", None))
# retranslateUi

View File

@ -100,12 +100,48 @@ def pre_treating(string): #删除字符串中对比较无用的字符, 以供比
string = re.sub(r"[,\.:;?]","",string)
return string #返回处理后的字符串
def treat_dict(p_dict): #对整个题库字典中的内容部分进行预处理,删除无用字符
def findsame(id,same_list): #在same_list中寻找与id相同的题号
set1 = set([pair[1] for pair in same_list if pair[0] == id])
set2 = set([pair[0] for pair in same_list if pair[1] == id])
return sorted(list(set1 | set2))
def findsameinDB(id): #在数据库中寻找与id相同的题号
mydb = connect(hostname = "wwylss.synology.me", port = "13306", username="tikuuser", pwd="Kjmathds_2024", db = "tikutest")
mycursor = mydb.cursor()
sql = "SELECT SAME_ID FROM same WHERE ID = (%s);"
val = (id,)
mycursor.execute(sql,val)
set1 = set([ret[0] for ret in mycursor.fetchall()])
sql = "SELECT ID FROM same WHERE SAME_ID = (%s);"
mycursor.execute(sql,val)
set2 = set([ret[0] for ret in mycursor.fetchall()])
return sorted(list(set1 | set2))
def findrelatedinDB(id): #在数据库中寻找与id关联的题号
mydb = connect(hostname = "wwylss.synology.me", port = "13306", username="tikuuser", pwd="Kjmathds_2024", db = "tikutest")
mycursor = mydb.cursor()
sql = "SELECT RELATED_ID FROM related WHERE ID = (%s);"
val = (id,)
mycursor.execute(sql,val)
set1 = set([ret[0] for ret in mycursor.fetchall()])
sql = "SELECT ID FROM related WHERE RELATED_ID = (%s);"
mycursor.execute(sql,val)
set2 = set([ret[0] for ret in mycursor.fetchall()])
return sorted(list(set1 | set2))
def treat_dict(): #对整个题库字典中的内容部分进行预处理,删除无用字符
treated_dict = {}
mydb = connect(hostname = "wwylss.synology.me", port = "13306", username="tikuuser", pwd="Kjmathds_2024", db = "tikutest")
mycursor = mydb.cursor()
mycursor.execute("SELECT ID,content FROM problems;")
p_dict = {id:content for id,content in mycursor.fetchall()}
mycursor.execute("SELECT ID,SAME_ID FROM same;")
same_list = [(ret[0],ret[1]) for ret in mycursor.fetchall()]
for id in p_dict:
treated_dict[id] = {}
treated_dict[id]["content"] = pre_treating(p_dict[id]["content"])
treated_dict[id]["same"] = p_dict[id]["same"]
treated_dict[id]["content"] = pre_treating(p_dict[id])
treated_dict[id]["same"] = findsame(id,same_list).copy()
mydb.close()
return treated_dict #返回处理后的字典, 含内容字段及相同题目字段
def detectmaxsim(currentid,excludelist,adict): #检测与已知题目关联程度最大的题目(除外列表之外的部分)
@ -2149,17 +2185,25 @@ def generateAnswerList(string,anspreamble = "答案: \\textcolor{red}{"): #从La
return anslist
def unUnitted(adict): #返回adict中未赋单元的id列表
return [id for id in adict if not "单元" in "".join(adict[id]["tags"]) and not "暂无" in "".join(adict[id]["tags"])]
def unUnitted(idexp): #返回adict中未赋单元的id列表
idlist = generate_number_set(idexp)
mydb = connect(hostname = "wwylss.synology.me", port = "13306", username="tikuuser", pwd="Kjmathds_2024", db = "tikutest")
mycursor = mydb.cursor()
mycursor.execute("SELECT ID FROM tagcorresp WHERE tagname REGEXP '\\\\S*单元' or tagname = '暂无对应';")
unittedids = set([ret[0] for ret in mycursor.fetchall()])
ununittedids = sorted(list(set(idlist)-unittedids))
return ununittedids
def AutoAssignTagNotoLaTeX(newlist,adict): #根据题库中已有的单元挂钩和字符串相似比对, 给newlist中的每一个ID在可能的情况下自动适配单元号码, 可能有误, 需人工审核, 返回(LaTeX代码的body(从\begin{enumerate}到\end{enumerate}的部分),题号和单元号码的对应)组成的二元tuple
def AutoAssignTagNotoLaTeX(newlist): #根据题库中已有的单元挂钩和字符串相似比对, 给newlist中的每一个ID在可能的情况下自动适配单元号码, 可能有误, 需人工审核, 返回(LaTeX代码的body(从\begin{enumerate}到\end{enumerate}的部分),题号和单元号码的对应)组成的二元tuple
output = "\\begin{enumerate}\n\n"
tagrecord = ""
treateddict = treat_dict(adict)
print("正在生成简化题目的字典...")
treateddict = treat_dict()
print("简化题目字典生成完毕.")
for id in newlist:
samelist = adict[id]["same"]
relatedlist = adict[id]["related"]
samelist = findsameinDB(id)
relatedlist = findrelatedinDB(id)
if not samelist == []:
unittags = [i for i in adict[samelist[0]]["tags"] if "单元" in i]
elif not relatedlist == []:
@ -2464,5 +2508,17 @@ def ChooseIDsByUsageInterval(startdate,enddate,interval,classregex): #返回根
return (chosenproblems,cautionids,usedproblems) #返回根据条件选出的题号字典及对应小题, 需要留意的记录长度不一的题号, 以及所有使用过的题号
def getAllIDsExp(obsincluded = True): #获取题库中所有题目的id(含:,)字符串, 不含
mydb = connect(hostname = "wwylss.synology.me", port = "13306", username="tikuuser", pwd="Kjmathds_2024", db = "tikutest")
mycursor = mydb.cursor()
if obsincluded:
mycursor.execute("SELECT ID FROM problems;")
else:
mycursor.execute("SELECT ID FROM problems WHERE NOT content REGEXP 'OBS';")
idlist = [ret[0] for ret in mycursor.fetchall()]
return generate_exp(idlist)
if __name__ == "__main__":
print("数据库工具, import用.")

49
工具v3/单元挂钩.py Normal file
View File

@ -0,0 +1,49 @@
from PySide6.QtWidgets import QWidget, QApplication, QFileDialog
from Ui_单元挂钩 import Ui_Form
from database_tools_2 import *
class MyWindow(QWidget,Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.bind()
def bind(self):
self.radioButton_phase1.setChecked(True)
self.pushButton_exec.clicked.connect(self.exec)
def exec(self):
# pro_dict = load_dict("../题库0.3/Problems.json")
allids = getAllIDsExp()
if self.radioButton_phase1.isChecked():
idexp = self.lineEdit_ids.text()
if idexp.strip() == "":
idlist = unUnitted(allids)
else:
idlist = generate_number_set(idexp)
latex_body,tagrec = AutoAssignTagNotoLaTeX(idlist,pro_dict)
latex_raw = ReadTextFile("模板文件/讲义模板.txt")
latex_data = StringSubstitute(r"<<[\s\S]*?待替换[\s\S]*?>>",latex_raw,("待检查单元",latex_body))
SaveTextFile(tagrec,"临时文件/单元对应.txt")
SaveTextFile(latex_data,"临时文件/待检查单元.tex")
if XeLaTeXCompile("临时文件","待检查单元.tex") == True:
print("处理成功.")
os.system("code 临时文件/单元对应.txt 临时文件/待检查单元.pdf")
else:
print("处理失败.")
elif self.radioButton_phase2.isChecked():
output = UnitRectoMetadataText(ReadTextFile("临时文件/单元对应.txt"))
SaveTextFile(output,"文本文件/metadata.txt")
os.system("code 文本文件/metadata.txt")
if __name__ == '__main__':
app = QApplication([])
windows = MyWindow()
windows.show()
app.exec()

89
工具v3/单元挂钩.ui Normal file
View File

@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>131</height>
</rect>
</property>
<property name="windowTitle">
<string>单元挂钩</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>20</x>
<y>40</y>
<width>54</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>题号</string>
</property>
</widget>
<widget class="QRadioButton" name="radioButton_phase2">
<property name="geometry">
<rect>
<x>20</x>
<y>90</y>
<width>191</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>收录单元信息至metadata阶段</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_ids">
<property name="geometry">
<rect>
<x>20</x>
<y>60</y>
<width>271</width>
<height>20</height>
</rect>
</property>
<property name="placeholderText">
<string>输入题号, 留空表示针对所有没有对应单元的题目</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_exec">
<property name="geometry">
<rect>
<x>310</x>
<y>10</y>
<width>71</width>
<height>111</height>
</rect>
</property>
<property name="font">
<font>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>运行</string>
</property>
</widget>
<widget class="QRadioButton" name="radioButton_phase1">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>141</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>自适应赋予单元信息</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>