我正在开发一个程序,实际上是一个人工智能,它通过一系列问题来学习区分动物,这些问题将确定我们所考虑的动物所具有的属性。因此,我需要一种方法来存储动物及其相应的属性问题,例如:
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?
所以我希望程序能够通过问多个问题来确定哪个动物是哪个。
回答:
最简单的方法是使用字典,它将保持属性与具有该属性的动物列表之间的关系,例如:
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?
attributesFound 将会是 [purr,whiskers],animal 将会是 [cat,dog],然后我们进入每个属性的循环
for each attribute in [cat,dog] : animal=set(animal.intersection(dict[attribute ]))//在第一次迭代中,animal 仍然是 [cat,dog]。
// 然而在第二次迭代中,set([cat,dog]intersection([cat])) 将只留下一个元素,即 cat