Animals and the Tree of Knowledge

  1. What it Does
  2. The Data Structure
  3. The Code
  4. Ideas for further Development

What it Does

This little program builds a classification tree by interacting with the user. It starts off knowing only that a bird is an animal and bit by bit allows learns more as it interacts with you. Here is an example run. You would type everything that follows a '?'.

   >>> import animal
   >>> animal.main()
   
   Are you thinking of an animal? y
   Is it a bird? n
   What is the animals name? Dog
   What question would distinguish a Dog from a bird? Does it have wings
   If the animal were Dog the answer would be? n
   
   Are you thinking of an animal? y
   Does it have wings? y
   Is it a bird? n
   What is the animals name? Bat
   What question would distinguish a Bat from a bird? Does it have feathers
   If the animal were Bat the answer would be? n
   
   Are you thinking of an animal? y
   Does it have wings? y
   Does it have feathers? y
   Is it a bird? y
   
   Are you thinking of an animal? n
   >>>

The Data Structure

This is a schematic of the knowledge tree built from the above dialog.

The Code

The program is very simple. It starts with the top question and depending on the users yes or no response, chooses either the left or right fork down the tree. At the last element (the "leaf" node), it makes its guess. If the guess is wrong then it has the user input the name of a new animal and a question to distinguish the new animal from the guess. Then the tree is grown by one more node to accomodate the question and the new animal.

The code should be very straight forward. An object class is used for nodes in the tree. Each node contains a question plus pointers to left and right nodes. Leaf nodes simply contain the name of an animal and the value None in the pointer attributes.

The function "yes" is a convenience. It will recognize "Yes", "YES", "Y", "y", etc. all correctly, as well as similar variations of "No". Otherwise it keeps asking the question.

I first saw this program in very old fashioned Basic about 20 years ago. The code was much more complex requiring separate arrays of strings and pointers. I was starting my new job in Holland and took it along. It was something of a hit. After several people played with it for an hour or so, it had developed a nice biological hierarchy, half in English and half in Dutch, of everyone who worked in the office!

Ideas for further Development

Enable the program to save its knowledge tree between runs. You might use the pickle mechanism. Or write a function that saves all the user responses so that they can be played back as a script via the UserInput module.

Make a command to display the knowledge tree on the screen using indenting. You might need to consider recursion for this. Alternatively map the tree with a graphics package like TKinter.

Index