Chat Bot
The Loebner Prize Competition is held every year to evaluate artificial intelligence chat programs using the Turing Test. In 1950, Alan Turing proposed a test to measure the intelligence of computers. In the Turing test, a subject has a text chat with either a person in another room or with a computer (the subject does not know which). There are no limits to what the subject can chat about, but when done chatting the subject must choose whether they think they were talking to a real person or to a computer. The computer is thought to be more intelligent if it is more frequently able to fool the human tester into labeling it as a human. (Whether the Turing test is actually a measure of intelligence is debatable, but one interesting thing to consider is, what if a computer was labeled as human more than an actual human?) One simple artificial intelligence chat algorithm is to just repeat what other people have said previously in the same context.
Details
Write a simple artificial intelligence chat program. The program should use a dictionary to associate phrases with responses. When a user types a phrase into a prompt the program should check if there is an entry for the phrase. If there is an entry it should print it, if there is not, then it should print a response for a random phrase in the dictionary. Note, the dictionary must be initialized with at least one phrase and response or else it will not be able to select a random phrase. To create a better bot, it should be initialized with a bunch of phrases and responses. After printing the response the program should repeat. The program should repeat a large fixed number of times and it should inform that user that pressing ctl-c will quit the program.
The chat will be very boring unless the program’s response dictionary is updated. The program should add the users input as a new response to the last phrase the program printed. That is, the program’s last output is the new key, and the user’s last input is the new value. The key should be converted to lowercase and stripped of all non-alphabetic characters to prevent capitalization and punctuation from affecting the look-up. If the key is already in the dictionary, the value should be overwritten with the new response.
Example
Welcome to chat bot! Press ctl-c to quit. you: hi bot: hello you: How are you? bot: Good, how are you? you: Open the pod bay doors, HAL. bot: I'm sorry, Dave. I'm afraid I can't do that.
Hint
This activity can be broken into three steps.
- Write a chat bot that can only respond by looking up responses in a dictionary of phrases and responses. Create an infinite while loop that repeatedly prompts the user for input and prints a response by looking it up in the dictionary. If the phrase entered by the user is not in the dictionary, the program should pick a random response from the dictionary.
- Update the dictionary every time the user enters input. This requires knowing what the user last entered and knowing what the bot last responded. Depending on the structure of your loop this may require a variable that is updated to store values from previous iterations of the loop. Also note that because the user begins the conversation it is not possible to update the dictionary after the user's first input.
- Finally, modify the strings that are used as keys in the dictionary so that they are all lowercase and contain only alphabetic characters. Since this will need to be done multiple times, a function would be very useful. Be careful when updating the dictionary to not put these stripped strings as values. It should still be possible for the chat bot to respond with capitals and punctuation.
Challenge
One problem with the chat function is that it forgets previous (and perhaps better responses) when it encounters the same phrase twice. A better chat program would store a list of responses in each dictionary entry. When looking up a phrase the function should choose randomly from all of the phrases stored in the list. An even better chat program would also store how frequently each of the phrases in a list has been encountered and weight the random selection to favor of the more frequently encountered phrases.