# -*- coding: utf-8 -*-
"""
这是网上找的别人的python实现,用做参考,但是这个是用python2.7的,python3要想实现还要做一些更改
"""
import numpy as np
class productionSystem:
def __init__(self):
f=open("productions.txt","rb")
self.productions=[] # 保存的是全体名词
self.charicters=[] # 保存的是全体特征名词
self.myclass=(f.readline().strip("最终类别为:").strip()).split(",") # f.readline()是读取一行
'''
self.myclass= ['金钱豹', '虎', '长颈鹿', '斑马', '鸵鸟', '企鹅', '信天翁'] # 这个列表保存的是7个要识别的动物名称
'''
for line in f.readlines(): # 针对第一行之外的每行
if(line!='\n'):
line=line.replace("->"," ")
line=line.replace(","," ")
line=line.split() # 最终得到了一个每行名词的列表
self.productions.append(line) # self.productions保存的是全体名词
for ch in line: # 针对每行的每一个名词
if (ch not in self.myclass) and (ch not in self.charicters):
self.charicters.append(ch) # 把特征名词放进self.charicters这个列表中
def showProduction(self):
print()
def match(self,x):
for ch in self.productions[x][:-1]:
if(ch in self.work):
continue
else:
return False
return True
def classify(self,work):
used=np.zeros(len(self.productions))
counter=len(self.work)
while(True):
for i in range(len(self.productions)):
if(used[i]==0 and self.match(i)):
used[i]=1
self.work.append(self.productions[i][-1])
if(self.work[-1] in self.myclass):
return True
tmp=len(self.work)
if(tmp==counter):
return False
else:
counter=tmp
def dowork(self):
print("所有类别有:")
for t in self.myclass:
print('{:<10}'.format(t),end="")
print()
print("所有属性有:")
counter=0
for t in self.charicters:
counter+=1
if(counter%5==0):
print('{:<10}'.format(t))
else:
print('{:<10}'.format(t),end='')
self.work=input("\n请输入属性\n----属性之间用空格隔开----\n")
self.work=self.work.split()
if(self.classify(self.work)):
print("该物种为:"+self.work[-1])
else:
print("抱歉,未能识别出该物种\n")
def main():
p=productionSystem()
p.dowork()
if __name__=="__main__":
main()
动物专家系统(2)
最新推荐文章于 2024-07-28 15:46:11 发布