赞
踩
- from tkinter import *
- import copy
-
- def find_data(process_data_list):
- # 依次进行循环查找并对过程排序
- #print(data_process_list)
- #print(data_result_list)
- #print(dict_input)
- for p, data_process in enumerate(data_process_list):
- # 用于判断此过程是否成立
- count = 0
- #print(process_data_list)
- #print(data_process)
- for i in process_data_list:
- if i in data_process:
- #print(i+'1')
- count += 1
- # 过程成立则数值相同,可以进入下一步
- if count == len(data_process):
- # 此过程中结果是否为最终结果,不是将此过程结果加入到过程中
- if data_result_list[p] not in result_list:
- # 弹出过程和此过程结果,因为此过程已经进行过,此结果存入需要查找的过程中
- result = data_result_list.pop(p)
- process = data_process_list.pop(p)
- #ins1(result,process)
- # 判断结果是否已经存在过程中,存在则重新寻找,不存在则加入过程,并将其存入最终结果
- if result not in process_data_list:
- dict_input[','.join(process)] = result
- end_result = find_data(process_data_list + [result])
- if end_result == 1:
- return 1
- else:
- return 0
- # 存在则直接寻找
- else:
- end_result = find_data(process_data_list)
- if end_result == 1:
- return 1
- else:
- return 0
- # 找到最终结果,取出结果后返回
- else:
- process = data_process_list.pop(p)
- dict_input[','.join(process)] = data_result_list[p]
- return 1
- return 0
- def ini1(data_process_list, data_result_list):
- Lstbox1.delete(0, END)
- for item in data_process_list:
- Lstbox1.insert(END, item)
- Lstbox2.delete(0, END)
- for item in data_result_list:
- Lstbox2.insert(END, item)
-
-
- def clear():
- Lstbox1.delete(0, END)
- Lstbox2.delete(0, END)
- txt.delete(1.0,END)
-
-
- def ins1(x,y):
- Lstbox1.insert(Lstbox1.size(),x)
- Lstbox2.insert(Lstbox2.size(),y)
-
- def ins():
- if entry.get() != '':
- if Lstbox1.curselection() == ():
- Lstbox1.insert(Lstbox1.size(), entry.get())
- else:
- Lstbox1.insert(Lstbox1.curselection(), entry.get())
- s = entry.get()
- global data_process_list
- data_process_list.append(s)
-
-
- def ins2():
- if entry.get() != '':
- if Lstbox2.curselection() == ():
- Lstbox2.insert(Lstbox2.size(), entry.get())
- # s = entry.get()
- # t = Lstbox2.size()
- # global data_result_list
- # data_result_list[t] = s
- else:
- Lstbox2.insert(Lstbox2.curselection(), entry.get())
- # s = entry.get()
- # t = Lstbox2.curselection()
- # global data_result_list
- # data_result_list[t] = s
- s = entry.get()
- global data_result_list
- data_result_list.append(s)
-
-
- def updt():
- if entry.get() != '' and Lstbox1.curselection() != ():
- selected = Lstbox1.curselection()[0]
- Lstbox1.delete(selected)
- Lstbox1.insert(selected, entry.get())
- s1 = entry.get()
- global data_process_list
- data_process_list[selected] = s1
- s = '你已经成功修改'
- gettt(s)
-
- def updt1():
- if entry.get() != '' and Lstbox2.curselection() != ():
- selected = Lstbox2.curselection()[0]
- Lstbox2.delete(selected)
- Lstbox2.insert(selected, entry.get())
- s1 = entry.get()
- global data_result_list
- data_result_list[selected]=s1
- s = '你已经成功修改'
- gettt(s)
-
-
-
- def delt():
- if Lstbox1.curselection() != ():
- t = list(Lstbox1.curselection())
- Lstbox1.delete(Lstbox1.curselection())
- Lstbox2.delete(t)
- t1 = Lstbox1.curselection()
- global data_process_list
- del data_process_list[t1]
- global data_result_list
- del data_result_list[t1]
- s='你已经成功删除'
- gettt(s)
-
- def gettt(s):
- txt.delete(1.0,END)
- txt.insert(END, s)
-
-
- def plus():
- term=entry.get()
- #print(term)
- if term=='0':
- entry.delete(0,'end')
- #print(list_data)
- global data_result_list
- tdataq_result_list=data_result_list.copy()
- global dict_input
- dict_input.clear()
- global data_process_list
- tdata_process_list=data_process_list.copy()
- end_result = find_data(list_data)
- data_result_list=tdataq_result_list
- data_process_list=tdata_process_list
- print(data_result_list)
- print(dict_input)
- print(end_result)
- if end_result == 1:
- s='推理:'+'\n'
- s3='综合数据库:'
- s3 = s3 + ' '.join(list_data) + ' '
- # 将结果进行打印
- for i in dict_input.keys():
-
- if dict_input[i] not in result_list:
- s3 = s3 + dict_input[i] + ' '
- s1=i+'-->'+dict_input[i]+'\n'
- s=s+s1
- #print(s)
- #print(f"{i}->{dict_input[i]}")
- # 得到最终结果即输出所识别动物
- if dict_input[i] in result_list:
- s2='所识别的动物为'+dict_input[i]+'\n'
- s=s+s2
- s=s3+'\n'+s
- gettt(s)
- else:
- # 将结果进行打印
- s3 = '综合数据库:'
- s3 = s3 + ' '.join(list_data) + ' '
-
- for i in dict_input.keys():
- s1='推理:'
-
- if dict_input[i] not in result_list:
- s3 = s3 + dict_input[i] + ' '
-
- s=i+'->'+dict_input[i]+'\n'+'并未得出最终结论,请继续补充事实'
- s=s3+'\n'+s1+s
- #print(s)
- gettt(s)
-
-
- if term:
- if database[term] not in list_data:
- list_data.append(database[term])
- else:
- s='输入为空'
- gettt(s)
- entry.delete(0,'end')
- # 打印前提条件
- s='前提条件为:'
- s1=' '.join(list_data) + '\n'
- s1=s+s1
- gettt(s1)
- # 进行递归查找,直到找到最终结果,返回1则找到最终结果
-
-
- # 用于储存中间过程
- global data_process_list
- data_process_list = []
- # 用于存储过程对应的结果
- global data_result_list
- data_result_list = []
- # 存储用于查询的数据
- global list_data
- list_data = []
- # 用于存储输出结果
- global dict_input
- dict_input = {}
- #做一个联系
- dict1={}
- # 规则库
- txt = '''有毛发,是哺乳动物
- 有奶,是哺乳动物
- 有羽毛,是鸟
- 会飞,会下蛋,是鸟
- 吃肉,是肉食动物
- 有犬齿,有爪,眼盯前方,是食肉动物
- 是哺乳动物,有蹄,是蹄类动物
- 是哺乳动物,是咀嚼反刍动物,是蹄类动物
- 是哺乳动物,是肉食动物,是黄褐色,身上有暗斑点,金钱豹
- 是哺乳动物,是肉食动物,是黄褐色,身上有黑色条纹,虎
- 是蹄类动物,有长脖子,有长腿,身上有暗斑点,长颈鹿
- 是蹄类动物,身上有黑色条纹,斑马
- 是鸟,有长脖子,有长腿,不会飞,有黑白二色,鸵鸟
- 是鸟,会游泳,不会飞,有黑白二色,企鹅
- 是鸟,善飞,信天翁'''
- # 将数据预处理
- datas = txt.split('\n')
- #print(datas)
- for data in datas:
- data = data.split(',')
- data_process_list.append(data[:-1])
- #print(data_process_list)
- data_result_list.append(data[-1].replace('\n', ''))
- # print(data_result_list)
- result_list = ['信天翁', '鸵鸟', '斑马', '长颈鹿', '虎', '金钱豹', '企鹅']
- # 数据库对应的过程
- database = {'有毛发': '有毛发', '有奶': '有奶', '有羽毛': '有羽毛', '会飞': '会飞', '会下蛋': '会下蛋', '吃肉': '吃肉', '有犬齿': '有犬齿',
- '有爪': '有爪', '眼盯前方': '眼盯前方', '有蹄': '有蹄', '是咀嚼反刍动物': '是咀嚼反刍动物', '是黄褐色': '是黄褐色', '身上有暗斑点': '身上有暗斑点', '身上有黑色条纹': '身上有黑色条纹',
- '有长脖子': '有长脖子', '有长腿': '有长腿', '不会飞': '不会飞', '会游泳': '会游泳', '有黑白二色': '有黑白二色', '善飞': '善飞', '是哺乳动物': '是哺乳动物',
- '是鸟': '是鸟', '是食肉动物': '是食肉动物', '是蹄类动物': '是蹄类动物', '金钱豹': '金钱豹', '虎': '虎', '长颈鹿': '长颈鹿', '斑马': '斑马',
- '鸵鸟': '鸵鸟', '企鹅': '企鹅', '信天翁': '信天翁'}
-
-
- root = Tk()
- root.title('夏雨花费脑筋的动物专家识别系统')
- root.geometry('900x600')
-
- frame1 = Frame(root, relief=RAISED)
- frame1.place(relx=0.0)
-
- frame3 = Frame(root, relief=RAISED)
- frame3.place(relx=0.2)
-
- frame4 = Frame(root, relief=RAISED)
- frame4.place(relx=0.4,relheight=0.4,width=300)
-
- frame2 = Frame(root, relief=GROOVE)
- frame2.place(relx=0.8)
-
- Lstbox1 = Listbox(frame1)
- Lstbox1.pack()
-
- Lstbox2 = Listbox(frame3)
- Lstbox2.pack()
-
- txt=Text(frame4)
- txt.pack()
-
- entry = Entry(frame2)
- entry.pack()
-
- btn1 = Button(frame2, text='初始化', command=lambda: ini1(data_process_list, data_result_list))
- btn1.pack(fill=X)
-
- btn2 = Button(frame2, text='添加', command=ins)
- btn2.pack(fill=X)
-
- btn3 = Button(frame2, text='添加2', command=ins2) # 添加和插入功能实质上是一样的
- btn3.pack(fill=X)
-
- btn4 = Button(frame2, text='修改', command=updt)
- btn4.pack(fill=X)
-
- btn9 = Button(frame2, text='修改2', command=updt1)
- btn9.pack(fill=X)
-
- btn5 = Button(frame2, text='删除', command=delt)
- btn5.pack(fill=X)
-
- btn6 = Button(frame2, text='清空', command=clear)
- btn6.pack(fill=X)
-
- btn7 = Button(frame2, text='查找', command=lambda:find_data(list_data))
- btn7.pack(fill=X)
-
- btn8 = Button(frame2, text='加入', command=plus)
- btn8.pack(fill=X)
- # 最终结果列表
-
-
-
-
- root.mainloop()
其实也是参照了其他博主的代码,然后改了一些,添加了一些自己的东西
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。