-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.py
349 lines (309 loc) · 14.5 KB
/
MainWindow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import sys
from PyQt5.QtWidgets import QAbstractItemView, QHeaderView, QTableWidget
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def __init__(self):
self.centralwidget = None
self.tableWidget = None
self.pushButton = None
self.pushButton_2 = None
self.pushButton_3 = None
self.plainTextEdit = None
self.label = None
self.label_2 = None
self.label_3 = None
self.label_4 = None
self.left_list = None
self.right_list = None
def setupUi(self, MainWindow):
rd = open('RD.txt')
lines = rd.readlines()
row_count = len(lines)
MainWindow.setObjectName("MainWindow")
MainWindow.resize(615, 450)
self.centralwidget = QtWidgets.QWidget(MainWindow)
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
# ---------------------------------------------------------------------
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
# self.tableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
self.tableWidget.setGeometry(QtCore.QRect(30, 40, 256, 192))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(2)
# 字体
font = QtGui.QFont("song", 10) # 'Arial'
self.tableWidget.setFont(font)
self.tableWidget.horizontalHeader().setFont(font)
# 单元格大小
self.tableWidget.horizontalHeader().setDefaultSectionSize(30) # 设置默认宽度
self.tableWidget.verticalHeader().setDefaultSectionSize(30) # 设置一行默认高度
self.tableWidget.setRowCount(row_count)
self.tableWidget.setHorizontalHeaderLabels(['规则', '操作'])
self.tableWidget.verticalHeader().setVisible(False)
self.tableWidget.setColumnWidth(0, 160)
self.tableWidget.setColumnWidth(1, 80)
# self.tableWidget.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeToContents)
# self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
# self.tableWidget.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeToContents)
self.item_text_set()
# self.tableWidget.setVerticalHeaderLabels()
for i in range(row_count):
self.tableWidget.setCellWidget(i, 1, self.buttonForRow()) # 在单元格中加入删除按钮
# ---------------------------------------------------------------------
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(60, 240, 93, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.add_rule)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(170, 240, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(60, 300, 93, 28))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_2.clicked.connect(self.save_rule)
# ---------------------------------------------------------------------
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setGeometry(QtCore.QRect(30, 330, 561, 87))
self.plainTextEdit.setObjectName("plainTextEdit")
# ---------------------------------------------------------------------
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 10, 72, 15))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(310, 10, 72, 15))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(470, 10, 72, 15))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(160, 305, 72, 15))
self.label_4.setObjectName("label_4")
# ---------------------------------------------------------------------
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
# ---------------------------------------------------------------------
self.left_list = QtWidgets.QListWidget(self.centralwidget)
self.left_list.setObjectName("left_list")
self.left_list.setGeometry(QtCore.QRect(310, 40, 131, 230))
self.left_list.setStyleSheet("background-color: '#FFFFFF'")
self.left_list.setDragEnabled(True)
self.left_list.setDragDropOverwriteMode(False)
self.left_list.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.left_list.setDefaultDropAction(QtCore.Qt.MoveAction)
# ---------------------------------------------------------------------
self.right_list = QtWidgets.QListWidget(self.centralwidget)
self.right_list.setObjectName("right_list")
self.right_list.setGeometry(QtCore.QRect(460, 40, 131, 230))
self.right_list.setStyleSheet("background-color: '#FFFFFF'")
self.right_list.setAcceptDrops(True)
self.right_list.setDefaultDropAction(QtCore.Qt.MoveAction)
# ---------------------------------------------------------------------
list_test = self.left_list_data_get()
self.left_list.addItems(list_test)
# --------------------------------------------------------------------
self.right_list.clicked.connect(self.right_item_clicked)
self.right_list.doubleClicked.connect(self.right_item_double_clicked)
self.left_list.clicked.connect(self.left_item_clicked)
self.left_list.doubleClicked.connect(self.left_item_double_clicked)
self.pushButton_3.clicked.connect(self.inference)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def get_rules(self):
# 获取规则库 并将结论和前提分开存储
RD = open("RD.txt", 'r')
P = []
Q = []
for line in RD:
line = line.strip('\n') # 删除开头或是结尾的回车字符
if line == '': # 处理空行 跳过
continue
line = line.split(' ') # 切片
Q.append(line[line.__len__() - 1]) # 分开存储
del (line[line.__len__() - 1])
P.append(line)
RD.close() # 关闭文件
return P, Q
# 判断list中所有元素是否都在集合set中
def ListInSet(self, db, li):
is_in = False
for i in li:
if i not in db:
continue
else:
is_in = True
return is_in
def the_best_match_on_first(self, process_txt, data_li):
final_process = ''
final_process_list = []
if '\n' in process_txt:
process_list = process_txt.split('\n')
search_item = data_li
mx = -float('inf')
for item in process_list:
curr = sum(search_item[k] == item[k] for k in item)
if curr > mx:
match = item
mx = curr
print(match)
def inference(self):
# 推理
data = self.right_list_data_get()
li = set(data) # 将综合数据库以集合的形式存放
[P, Q] = self.get_rules() # 获取规则库
self.process = '' # 用于存储推理过程
self.animal = '' # 存储结论
# 下面开始正式推理
flag = 0
for premise in P: # 对前提条件进行遍历
if self.ListInSet(premise, li):
# 能够找到一个前提条件全部存在于数据库
# li.add(Q[P.index(premise)]) # 把结论放入综合数据库
self.animal = Q[P.index(premise)] # 更新结论
self.process += "%s --> %s\n" % (premise, Q[P.index(premise)])
flag = 1 # 至少有一个能够推出来的结论
if flag == 1:
# flga!=0说明有结论生成
# 显示出推理过程
self.plainTextEdit.setPlainText(self.process)
# # 显示出结论
# self.lineEdit_2.setText(self.animal)
else:
# 一个结论也推不出来,询问用户是否进行补充
self.plainTextEdit.setPlainText('未找到合适的结果')
def left_list_data_get(self):
"""左侧列表填充推导规则关键词语"""
rd = open('RD.txt', 'r')
lines = rd.readlines()
rd_list = []
for line in lines:
tmp_rds = line.split(' ')
for rd_txt in tmp_rds:
if rd_txt == tmp_rds[(len(tmp_rds)-1)]:
break
else:
if rd_txt not in rd_list:
rd_list.append(rd_txt)
return rd_list
def left_item_clicked(self):
item = self.left_list.currentItem()
print("left_item_clicked: ", str(item.text()))
def left_item_double_clicked(self):
print("left_item_double_clicked: ", end='')
item = self.left_list.currentItem()
print(str(item.text()))
self.right_list.addItem(str(item.text()))
def right_item_clicked(self):
item = self.right_list.currentItem()
print("right_item_clicked: ", str(item.text()))
def right_item_double_clicked(self):
# print("\nright_item_Changed——")
# print("currentRow: ", self.right_list.currentRow())
# print("currentItem: ", self.right_list.currentItem().text())
selected_row = self.right_list.currentRow()
item = self.right_list.takeItem(selected_row)
del item
def right_list_data_get(self):
"""右侧列表内容获取"""
tmp_list = []
# 获取listwidget中条目数
count = self.right_list.count()
# 遍历listwidget中的内容
for i in range(count):
if self.right_list.item(i).text() not in tmp_list:
tmp_list.append(self.right_list.item(i).text())
print(tmp_list)
return tmp_list
def buttonForRow(self):
widget = QtWidgets.QWidget()
# # 修改
# self.updateBtn = QtWidgets.QPushButton('修改')
# self.updateBtn.setStyleSheet(''' text-align : center;
# background-color : NavajoWhite;
# height : 30px;
# border-style: outset;
# font : 13px ''')
# 删除
self.deleteBtn = QtWidgets.QPushButton('删除')
self.deleteBtn.setStyleSheet(''' text-align : center;
background-color : LightCoral;
height : 30px;
border-style: outset;
font : 13px; ''')
self.deleteBtn.clicked.connect(self.delete_rule)
hLayout = QtWidgets.QHBoxLayout()
# hLayout.addWidget(self.updateBtn)
hLayout.addWidget(self.deleteBtn)
hLayout.setContentsMargins(5, 2, 5, 2)
widget.setLayout(hLayout)
return widget
def add_rule(self):
rows = self.tableWidget.rowCount()
self.tableWidget.insertRow(rows)
self.tableWidget.setCellWidget(rows, 1, self.buttonForRow()) # 在单元格中加入删除按钮
self.tableWidget.setCurrentCell(rows, 0)
def delete_rule(self):
row = self.tableWidget.currentRow()
self.tableWidget.removeRow(row)
def save_rule(self):
rd = open('RD.txt', 'w')
rd.truncate()
rd_list = []
rows = self.tableWidget.rowCount()
for row in range(rows):
rd_line = self.tableWidget.item(row, 0).text()
print(rd_line)
rd.write(rd_line + '\n')
rd.close()
list_test = self.left_list_data_get()
self.left_list.clear()
self.left_list.addItems(list_test)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "添加"))
self.pushButton_2.setText(_translate("MainWindow", "保存"))
self.pushButton_3.setText(_translate("MainWindow", "推论"))
self.label.setText(_translate("MainWindow", "规则库"))
self.label_2.setText(_translate("MainWindow", "待选关键字"))
self.label_3.setText(_translate("MainWindow", "已选关键字"))
self.label_4.setText(_translate("MainWindow", "推论过程"))
def item_text_set(self):
rd = open('RD.txt', 'r')
lines = rd.readlines()
i = 0
for line in lines: # 将规则库放入显示框
print(line.replace('\n', ''))
line = line.replace('\n', '')
rd_item = QtWidgets.QTableWidgetItem(line)
self.tableWidget.setItem(i, 0, rd_item)
i = i + 1
def get_rule(self):
rd = open('RD.txt', 'r')
lines = rd.readlines()
i = 0
for line in lines: # 将规则库放入显示框
print(line.replace('\n', ''))
line = line.replace('\n', '')
rd_item = QtWidgets.QTableWidgetItem(line)
self.tableWidget.setItem(i, 0, rd_item)
i = i + 1
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv) # 创建一个QApplication,即将开发的软件app
MainWindow = QtWidgets.QMainWindow() # QMainWindow装载需要的组件
ui = Ui_MainWindow()
ui.setupUi(MainWindow) # 执行类中的setupUi方法
MainWindow.show()
sys.exit(app.exec_()) # exit()或点击按钮退出app