有毛髮 哺乳動物 - 有奶 哺乳動物 - 有羽毛 鳥 - 會飛 會下蛋 鳥 - 吃肉 食肉動物 - 有犬齒 有爪 眼盯前方 食肉動物 - 哺乳動物 有蹄 有蹄類動物 - 哺乳動物 反芻動物 有蹄類動物 - 哺乳動物 食肉動物 黃褐色 身上有暗斑點 金錢豹 * 哺乳動物 食肉動物 黃褐色 身上有黑色條紋 虎 * 有蹄類動物 長脖子 有長腿 身上有暗斑點 長頸鹿 * 有蹄類動物 身上有黑色條紋 斑馬 * 鳥 長脖子 有長腿 不會飛 有黑白二色 鴕鳥 * 鳥 會游泳 不會飛 有黑白二色 企鵝 * 鳥 善飛 信天翁 *
最後一個字符爲 - 表示結論爲中間結果 爲 * 表示爲一種動物後端
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file '動物識別專家系統.ui' # # Created by: PyQt5 UI code generator 5.9.2 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtGui import QFont class Ui_Animals(object): def setupUi(self, Animals): Animals.setObjectName("Animals") Animals.resize(1127, 710) Animals.setAutoFillBackground(True) self.TL = QtWidgets.QTextEdit(Animals) self.TL.setGeometry(QtCore.QRect(670, 200, 251, 211)) self.TL.setObjectName("TL") self.input = QtWidgets.QTextEdit(Animals) self.input.setGeometry(QtCore.QRect(240, 100, 151, 321)) self.input.setAutoFillBackground(False) self.input.setObjectName("input") self.result = QtWidgets.QTextEdit(Animals) self.result.setGeometry(QtCore.QRect(670, 100, 251, 51)) self.result.setObjectName("result") self.result.setReadOnly(True) self.input_lable = QtWidgets.QLabel(Animals) self.input_lable.setGeometry(QtCore.QRect(100, 80, 141, 41)) self.input_lable.setObjectName("input_lable") self.input_lable.setFont(QFont("Roman times", 10, QFont.Bold)) self.TL_label = QtWidgets.QLabel(Animals) self.TL_label.setGeometry(QtCore.QRect(750, 150, 101, 61)) self.TL_label.setObjectName("TL_label") self.TL_label.setFont(QFont("Roman times", 10, QFont.Bold)) self.result_label = QtWidgets.QLabel(Animals) self.result_label.setGeometry(QtCore.QRect(750, 70, 111, 31)) self.result_label.setObjectName("result_label") self.result_label.setFont(QFont("Roman times", 10, QFont.Bold)) self.scrollArea = QtWidgets.QScrollArea(Animals) self.scrollArea.setGeometry(QtCore.QRect(90, 120, 141, 20)) self.scrollArea.setWidgetResizable(True) self.scrollArea.setObjectName("scrollArea") self.scrollAreaWidgetContents = QtWidgets.QWidget() self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 139, 18)) self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents") self.comboBox = QtWidgets.QComboBox(self.scrollAreaWidgetContents) self.comboBox.setGeometry(QtCore.QRect(0, 0, 141, 21)) self.comboBox.setObjectName("comboBox") self.scrollArea.setWidget(self.scrollAreaWidgetContents) self.pushButton = QtWidgets.QPushButton(Animals) self.pushButton.setGeometry(QtCore.QRect(500, 240, 93, 28)) self.pushButton.setObjectName("pushButton") self.pushButton.setFont(QFont("Roman times", 10, QFont.Bold)) self.checkBox = QtWidgets.QCheckBox(Animals) self.checkBox.setGeometry(QtCore.QRect(500, 190, 91, 19)) self.checkBox.setObjectName("checkBox") self.checkBox.setFont(QFont("Roman times", 10, QFont.Bold)) self.pushButton_2 = QtWidgets.QPushButton(Animals) self.pushButton_2.setGeometry(QtCore.QRect(10, 120, 61, 21)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_2.setFont(QFont("Roman times", 10, QFont.Bold)) self.pushButton_3 = QtWidgets.QPushButton(Animals) self.pushButton_3.setGeometry(QtCore.QRect(500, 300, 91, 31)) self.pushButton_3.setObjectName("pushButton_3") self.pushButton_3.setFont(QFont("Roman times", 10, QFont.Bold)) self.retranslateUi(Animals) self.pushButton.clicked.connect(Animals.test) self.comboBox.activated['int'].connect(Animals.selectChange) self.checkBox.stateChanged['int'].connect(Animals.checkChange) self.pushButton_2.clicked.connect(Animals.selectInit) self.pushButton_3.clicked.connect(Animals.rules) QtCore.QMetaObject.connectSlotsByName(Animals) def retranslateUi(self, Animals): _translate = QtCore.QCoreApplication.translate Animals.setWindowTitle(_translate("Animals", "Form")) self.input_lable.setText(_translate("Animals", "請輸入已知事實")) self.TL_label.setText(_translate("Animals", "推理過程")) self.result_label.setText(_translate("Animals", "專家分析結果")) self.pushButton.setText(_translate("Animals", "推理")) self.checkBox.setText(_translate("Animals", "反向推理")) self.pushButton_2.setText(_translate("Animals", "初始化")) self.pushButton_3.setText(_translate("Animals", "修改規則庫"))
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'animal.py' # # Created by: PyQt5 UI code generator 5.9.2 # # WARNING! All changes made in this file will be lost! from 動物識別專家系統 import Ui_Animals from PyQt5 import QtWidgets from PyQt5 import QtGui import sys import os import tkinter from tkinter import messagebox def IsEvidence(x): for i in mywindow.fact: if x == i[-2]: return False return True def getData(x): data = [] for i in mywindow.fact: tr = [] if x == i[-2]: for j in range(0, len(i) - 1): tr.append(i[j]) data.append(tr) return data def backs(data): c = 0 flag = False for i in data: d = "if " for s in range(0, len(i)): if s == len(i)-2: d = d + str(i[s]) + " then " else: d = d + str(i[s]) + " " window.TL.append(d) for j in range(0, len(i) - 1): if (IsEvidence(i[j])): root = tkinter.Tk() root.withdraw() a= messagebox.askquestion("提示", i[j]+"嗎") #print(i[j] + "嗎?") #r = input() print(a) if a == "yes": c = c + 1 else: temp = getData(i[j]) if (backs(temp)): c = c + 1 if c >= i.__len__() - 1: flag = True print(i[-1]) print("驗證成功") break else: flag = False print(i[-1]) print("驗證失敗") if (flag): return True else: return False class mywindow(QtWidgets.QWidget,Ui_Animals): fact = [] conditions = set("") res = set("") def __init__(self): super(mywindow, self).__init__() f = open("rules.txt", "r") for line in f: ls = line.strip('\n').split(" ") mywindow.fact.append(ls) f.close() for i in mywindow.fact: for j in range(0,len(i)-2): mywindow.conditions.add(i[j]) mywindow.res.add(i[-2]) self.setupUi(self) def resizeEvent(self, event): palette = QtGui.QPalette() pix = QtGui.QPixmap('images/3.jpg') pix = pix.scaled(self.width(), self.height()) palette.setBrush(QtGui.QPalette.Background, QtGui.QBrush(pix)) self.setPalette(palette) def test(self): if self.checkBox.isChecked():#逆向推理 i = self.comboBox.currentIndex() s = self.comboBox.itemText(i) print(s) data=getData(s) print(data) if (backs(data)): root = tkinter.Tk() root.withdraw() a = messagebox.showinfo("提示", "該動物是" + data[0][-1]) self.result.setText("專家分析該動物是"+data[0][-1]) else: root = tkinter.Tk() root.withdraw() self.result.setText("專家分析該動物不是" + data[0][-1]) a = messagebox.showinfo("提示", "該動物不是" + data[0][-1]) else: #正向推理 s= self.input.toPlainText() tl ="" description = s.split("\n") print("des") print(description) line = 0 for i in mywindow.fact: same = 0 for j in range(0, len(i)): if j >= len(i) - 2: break for k in range(0, len(description)): if i[j] == description[k]: same = same + 1 break if k == len(description): break if same == i.__len__() - 2: print("same=i") line = 1 if i[-1] == "*": # 是結論 d = "if " for s in range(0,len(i)-1) : if s == len(i)-3: d=d+str(i[s])+" then " else: d=d+str(i[s])+" " tl = tl + d + "\n" self.TL.setText(tl) self.result.setText("專家分析該動物是"+i[-2]) print(i[-2]) else: line = 1 d = "if " for s in range(0, len(i) - 1): if s == len(i) - 3: d = d + str(i[s]) + " then " else: d = d + str(i[s]) + " " tl = tl + d +"\n" self.TL.setText(tl) self.result.setText("專家也不知道具體是什麼動物,大機率推測是"+i[-2]) # print(i[-1]) description.append(i[-2]) if line ==0: self.result.setText("專家也不知道具體是什麼動物") def selectInit(self): mywindow.fact.clear() mywindow.conditions.clear() mywindow.res.clear() f = open("rules.txt", "r") for line in f: ls = line.strip('\n').split(" ") mywindow.fact.append(ls) f.close() for i in mywindow.fact: for j in range(0, len(i) - 2): mywindow.conditions.add(i[j]) mywindow.res.add(i[-2]) self.comboBox.clear() self.input.clear() self.result.clear() self.TL.clear() if(self.checkBox.isChecked()): for x in mywindow.res: self.comboBox.addItem(str(x)) else: for x in mywindow.conditions: self.comboBox.addItem(str(x)) def selectChange(self): if self.checkBox.isChecked(): self.input.clear() i = self.comboBox.currentIndex() s = self.comboBox.itemText(i) self.input.append(s) else: i = self.comboBox.currentIndex() s = self.comboBox.itemText(i) self.input.append(s) def checkChange(self): self.comboBox.clear() if self.checkBox.isChecked(): for x in mywindow.res: self.comboBox.addItem(str(x)) else: for x in mywindow.conditions: self.comboBox.addItem(str(x)) def rules(self): os.startfile('rules.txt') app = QtWidgets.QApplication(sys.argv) window = mywindow() window.show() sys.exit(app.exec_())