Assignment 1: Python Warm-up and Chatbot
CSE 415: Introduction to Artificial Intelligence
The University of Washington, Seattle, Winter 2020
The reading for this assignment is Python as a Second Language.
Due Wednesday, January 15 at 23:59 PM.
 
Overview:
Part A of this assignment consists of several smallish Python exercises. If you are new to Python, do the associated reading. Otherwise, this is mainly a refresher with a little problem-solving thrown in.
Part B asks you to design a Turing test that takes your major field of study (e.g., Information Science, Electrical and Computer Engineering, etc.) into consideration. You'll write a short essay describing this design.
Part C is an opportunity not only to practice your Python skills, but also to get creative and produce an agent with "character." This part will complement Part B and help you to think about the challenges required to pass a Turing test in your major field.

You should turn in three files: (1) a file named a1.py of function definitions, (2) a file named report.pdf containing not only your essay from Part B, but also a transcript of a session with your chatbot. (3) a file containing the Python code for your conversational agent (named as described in the instructions). Starter code is available here: a1_starter.tar.

Purposes:
  • Help you get set up for Python programming.
  • Get practice working with Python lists, dictionaries, and a few utility functions.
  • Stimulate you to think about how AI might be relevant to your own major field and what some of the challenges might be in achieving a "pass" on some kind of Turing test relevant to your field.

 
Part A. Defining Functions (20 points).
 
Complete the definition of the following functions...

def is_multiple_of_3(n):
  "Return True if n is a multiple of 3; False otherwise."
  pass

def next_prime(m):
  '''Return an integer p that is prime, and such that
  p > m, and there does not exist any n, with n > m
  and n < p such that n is prime. In other words, return
  the next prime number after m.'''
  pass

import wordscraper
url = "http://courses.cs.washington.edu/courses/cse415/20wi/desc.html"
def empirical_probabilities(url):
  '''Return a dictionary whose keys are words in a reference vocabulary,
  and whose values are PROBABILITIES of those words, based on the
  number of occurrences on the webpage at the given URL.'''
  pass

This third function, empirical_probabilities, can take advantage of the functionality provided in the starter code file wordscraper.py. This includes functions for downloading a web page, making a dictionary of all the words on the page, counting the occurrences of each word, and also for setting up a reference vocabulary. A reference vocabulary is a pre-chosen set of words of interest. When using a reference vocabulary, all other words occuring on a page will not contribute to the final counts. Using a technique called "Laplace smoothing", we will initialize the count of each reference vocabulary word to 1, rather than 0. This is often done in Bayesian inference to avoid statistical problems arising from small sample sizes. Your main job is to call these functions and finally create a dictionary that gives a probability value for each word. Consult and try out the file wordscraper.py to get more familiar with these functions.

The above exercises will be graded with a combination of autograder and manual grading. Exercise 1 is worth 4 points, Exercise 2 is worth 6 points, and Exercise 3 is worth 10 points.

A simple "sanity-check" testing program is available in the starter code archive that might help you realize that you have a spelling error in a function name or that your functions do not return values of the proper type: This program is a1_fn_sig_checker.py. Note that this file is not an autograder, and it does NOT test whether all your functions compute the correct values.

Part B. Your Turing Test (15 points).
 
Think about a possible useful Turing test in your specific field such as organic chemistry, electrical engineering, mathematics, etc. Then write a 1-2 page paper that answers the following questions.
  1. Tell what your major field of study is. Then Explain what a person typically does in a job within your field.
  2. Explain what parts of this job can already be done by computer.
  3. Explain what seems to be BEYOND the state of the art -- something in your field that a computer cannot yet do but might be able to do soon.
  4. Describe how a good Turing test would be set up in your field:
    1. Who should be the judges?
    2. What kinds of questions would be fair game for the test?
    3. What affordances would be particularly important in your field? (Understanding human speech? Understanding a particular notation such as chemical formulas? Machine vision? Ability to perform logical deductions? Knowledge of particular types of facts such as chemical element properties?)
  5. Is there any particular "must-have" feature for a computer to exhibit in order to be considered "intelligent" within your field?
  6. What do you see as the likely progress of AI in your field in the next 10 years?
  7. Based on your experience designing an agent related to your major field, in Part C, what is one significant challenge in creating an agent that can pass your Turing test?
Your essay should cite at least two references such as newspaper articles or wikipedia articles.
Part C. Conversational Agent (15 points).
 
Using the provided starter code, create a conversational agent that simulates a human character in a dialog. Your program should have some definable profession, such as mechanical engineer, information scientist, musician, etc. This profession should be related to your major field of study.

Besides being able to carry on a conversation with a human user, it should be able to join into conversations with the agents created by any member of the class. This should be easy to achieve, because you will simply provide a set of rules and other strings and instantiate the chatbot class provided in the starter code.

To create your agent, make a new Python file. The name should use the scheme YourUWNetID_agent.py, which is explained in more detail later. This file should import the starter-code module chatbot.

You can see how to create a sample agent by examining the code in chatbot.py near the end of the file, right after the line that reads as

if __name__=="__main__":
Thus your file would look something like this:
'''Filename of your agent.
CSE 415, Winter 2020, Assignment 1
Your name 
'''
import chatbot

... code defining rules etc.
InfoSciAgent = chatbot.chatbot(my_rules, you_me, "Info-Expert", my_intro_string)

if __name__=="__main__":
  InfoSciAgent.chat()
The last part of your file starts a chat session with the user, but only if your agent is being run by itself in a session, and not if it is being imported as agent among others in an automatic dialog, etc.

There should be at least 10 rules in your agent's rule list. Here is an example rule with a 2-line comment that describes it:

(r"you like (.*)", ["Why do you like $1$?", "Hey, I like you!"])
  # The above rule catches user inputs for the form "I like hot sauce."
  # There are two response patterns.
Each rule is a tuple with two components. The first component is a "raw string" that gives a "regular expression". For information on raw strings, see this page. For background on regular expressions in general, see this Wikipedia article. For more information on regular expressions in Python, see this documentation. The second component of each rule is a list of response patterns. A response pattern may contain a code such as $1$ which means to replace the code by whatever input text matched the first "group" according to the regular expression in the first component of the rule. A group is specified in the regular expression using parentheses. For example, "(.*)" in the given rule is a group whose subpattern ".*" will match zero or more characters of the input string. Your agent should be ready to be used as a module in another program that runs your agent with another agent in a dialog. The interface will consist of three functions that are already provided for you in the chatbot class of the starter code: one called introduce(), one called respond(theInput), and one called agentName().
 
The introduce function returns a string representing a message that tells the name of the agent, what the agent represents and the name and UWNetID of you the programmer. For example, it might return a string containing:
My name is Rusty Sales, and I sell junky cars.  I was designed by
Jenny Chrysler, a business major in college. If you don't like the way
I deal, contact her at jchrysler@uw.edu.  How can I help you?
The respond function is already implemented for you. However, it needs a list of rules to use when it is deciding how to reply to each input. You should look at the sample rules to see what format is required, and then develop your own rules.
 
The agentName function returns a string that should be a short nickname for your agent. This will be useful in printing out a prompt-like identifier when showing lines of a conversation among different agents. For example, the function might return Rusty for example above.
 

The conversations your agent generates (even with a user who inputs reasonable things) do not have to make sense or be consistent. That is beyond what we expect in this assignment. However, they should mostly seem reasonable in the small context of the user's previous line of input, and some of your agent's responses should seem related to the "profession" chosen for the agent. Thus there should be some relationship between your design for your agent and your design for the Turing test of Part B.

Name your agent's Python file in the following way, so that we can keep track of the different agents: YourUWNetID_agent.py, where YourUWNetID represents your UWNetID code (i.e., your email user name within the u.washington.edu domain. For one thing, this will guarantee that each of our agents is implemented in a file with a unique name. It will also give the graders an easy way to find your agent within a group, if needed.

Provide a comment in the code for each of your production rules.

Turn-In Instructions Turn in your files, named according to the instructions above, at our course's Canvas website.
Updates and Corrections
 
The line of the spec starting "InfoSciAgent = " was modified so that instead of chatbot.you_me it uses simply you_me.

The part of the comment in Part A, Ex. 3 that was '"biased" counts' was updated to "PROBABILITIES".

This is Version 1.03, last updated Jan. 12 at 12:49 PM. If needed, additional updates and corrections will be posted here, and/or in ED.