2014-10-18 80 views
0

我正在创建一个程序A.I.事实上,它通过一系列问题来了解动物之间的差异,并通过这些问题来确定动物之间的差异,这将决定动物头脑中拥有哪些属性。所以,我要的是保存动物选择的属性问题的方式,例如:如何根据属性问题区分动物

dog : does it have a wet nose? 
     does it have whiskers? 

所有这些问题,其回答“是”的属性,狗有...任何想法?此外裸记住,一些动物具有相同的属性,例如:

dog : does it have a wet nose? 
     does it have whiskers? 
cat : does it purr? 
     does it have whiskers? 

所以我希望程序能够问多个问题,并确定哪些动物是哪个。

回答

1

你可以做最简单的事情就是用字典,这将保持有例如该属性的动物属性和列表之间的关系:

attributes=[bite,scratch,purr,whiskers,...] 
dic={ 
whiskers:[cat,dog] 
purr:[cat] 
scratch:[cat] 
bite:[cat,dog,rat]} 
dose it scratch 

例如基于你的问题格式:

for each question: 
    for each word in question : 
     if word in attributes: 
     attributesFound.append(word) 
    animal=set(dic[attributesFound[0]]) 
    for each attribute in attributesFound: 
    animal=animal.intersection(dict[attribute ]) 

说,我们有这样的情况下

 does it purr? 
     does it have whiskers? 

的一个ttributesFound将是[呼噜声,晶须],动物将是[猫,狗]然后我们进入每个属性循环

for each attribute in [cat,dog] : 
    animal=set(animal.intersection(dict[attribute ]))//in the first iteration animal would still be [cat,dog]. 

//然而在第二次迭代组([猫,狗]交点([猫]))会给你一组只有一个元素,这将是猫