动物专家系统(2)

# -*- 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()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值