Yikes, that’s a tough one! Basically, they learn to associate certain sounds (and actions) you make with specific responses. So the one they most often learn first is “No!” which is just a negative reinforcer — that is, it tells the animal you’re not happy. Of course, the word itself doesn’t carry much information but when used together with an angry shout and some form of punishment (i.e. sending your dog outside, for instance), the animal gets the picture.
So basically, by combining distinct words with reinforcers — either positive or negative — you can teach (certain) animals to recognize commands. This type of learning is called reinforcement learning and is important for virtually all species, including humans.
As far as I undestand, they learn in a similar way to very young children. They may not know what language is as such, but they start to associate certain noises you make and gestures with being rewarded for doing a certain thing. So because they want the reward (be it food or just a pat on the head) they learn to do the correct action. The process of learning in this way is called conditioning.
Comments