一、基础知识库
有毛发 哺乳动物 - 有奶 哺乳动物 - 有羽毛 鸟 - 会飞 会下蛋 鸟 - 吃肉 食肉动物 - 有犬齿 有爪 眼盯前方 食肉动物 - 哺乳动物 有蹄 有蹄类动物 - 哺乳动物 反刍动物 有蹄类动物 - 哺乳动物 食肉动物 黄褐色 身上有暗斑点 金钱豹 * 哺乳动物 食肉动物 黄褐色 身上有黑色条纹 虎 * 有蹄类动物 长脖子 有长腿 身上有暗斑点 长颈鹿 * 有蹄类动物 身上有黑色条纹 斑马 * 鸟 长脖子 有长腿 不会飞 有黑白二色 鸵鸟 * 鸟 会游泳 不会飞 有黑白二色 企鹅 * 鸟 善飞 信天翁 *
最后一个字符为 - 表示结论为中间结果 为 * 表示为一种动物
二、QT界面 源码

# -*- 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", "修改规则库"))
三、后端处理 Python源码

# -*- 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_())