CPSC 231 Assignment 3
Chat231: Creating your own Chat Bot
Learning Objectives:
Through this assignment, students will practice coding in Python, apply concepts of functions,
dictionary, and lists (as well as the concept from before), and use Linux commands to test their
code.
Weight: 9% of final grade
Due: Nov 8, 2024 at 11:59pm.
Submission: one Python .py file, submitted on the D2L Dropbox. You may submit as many
times as you wish, and only the latest submission will be graded. Note that you will also need
to sign this page and submit it on D2L (you can take a screenshot or picture of the page).
Extensions: You may use your personal days to extend the deadline.
You have a total of 5 personal days for the entire semester. No penalties for using personal days.
An assignment will not be accepted if it is late and all personal days are already used, other than
in exceptional circumstances. You are responsible for ensuring that you keep track of days used.
To use a personal day, just submit your assignment late. Your TA will count the number of days
used based on your latest submission time: Less than 24 hours = 1 day, More than 24 hours but
less than 48 hours = 2 days, etc. You have a 5-minute grace period. An assignment submitted
less than 5 minutes late will not be counted as late (but if the submission time is 12:05am or
later, it will be counted as late).
Special circumstances beyond 5 personal days: You may submit a request form here.
Academic Integrity: This work must be completed individually. Please follow academic integrity
rules as discussed during lecture, including the AI policy. You may discuss your ideas in English
(not in Python) with other students as much as you like, but make sure that when you write your
code that you are writing your own code. A good rule of thumb is to never share your code with
anyone, except your instructor and TAs.
Please sign below to indicate that you’ve read and agreed to the rules above for assignment 3:
Your Signature: _______________
Date: _______________________
Copyright © 2024. Do not distribute outside of the CPSC 231 Fall 2024 class.
CPSC 231 Assignment 3
Chat231: Creating your own Chat Bot
Notes: You are not allowed to import anything for this assignment, except “import math” and
"import numpy". You are asked to demonstrate programming at the basic level, so you are not
allowed to use other imports.
The existing file [Link] imports your code in support_functions. This is how server uses all
the code you write in support_functions. You should not change anything in [Link].
You should not have any code that is outside a function. You should not have any global
variables and the word "global" should not appear anywhere in your code. All your code should
be defined inside a function.
Other than these, you are allowed to use any other Python coding structures even if they haven’t been
discussed in class. However, this assignment can be completed using only the knowledge taught in class.
Don’t use something if you don’t fully understand it.
Detailed Descriptions
Think ChatGPT is cool? Well, how about creating your own version of ChatGPT? As a computer
science major, you can’t just know how to use ChatGPT. You will need to learn how it works
behind the scenes so you can create your own chat bot!
Learning the neural networks that power ChatGPT is beyond the scope of this course. Let’s start
with this simpler assignment, and see just how difficult it is to create even a mini version of
ChatGPT, called Chat231, that can answer simple questions.
Step 1:
Create a new py file to work on. Name your file support_functions.py. If it is not called this
name, rename it. It must have this file name. This is the only file you will submit.
Step 2:
Download the new file, [Link], from the D2L assignment page. [Link] must be placed in
the same directory as your own support_functions.py file.
Step 3:
Do not add/change anything in [Link]. You will not submit this file.
Copyright © 2024. Do not distribute outside of the CPSC 231 Fall 2024 class.
Start writing all your code in support_functions.py. [Link] imports support_functions, so
whatever code you write in support_functions will be used by [Link].
Step 4: Your first job is to ensure the files are set up correctly. Run [Link] (do not run
support_functions.py). You should see the following:
Starting server on [Link]:8080.
The server Python code is creating a web server on your local computer. The server will keep
running until you stop the Python program (or until you crash it with some error in your code).
You don't need to understand the code in [Link] because you will not be changing it in any
way. As long as you write your functions correctly in support_functions.py, the server will work
as intended.
Now open a web browser (Chrome, Firefox, Edge, etc. but not Safari because Apple doesn’t like
to play well with others) and go to
[Link]
You should see:
(While keeping your Python code running) Click the button "Connect to server". If you see the
following message in your web browser, you've set up correctly.
Copyright © 2024. Do not distribute outside of the CPSC 231 Fall 2024 class.
This web page (called a client) is able to connect with the Python-based server running on your
computer. If you don't see the "Connected!" message, your server is not running correctly.
Function: get_greetings(name)
Step 5: Chat231 is complaining that you did not create the function get_greetings(), so your task
is to create a new custom function in your support_functions.py file, called
get_greetings(name). The function has the following:
Parameter: name, a string.
Returns: a string.
The function should return the following string:
"Greetings <name>! I am your virtual assistant Chat231."
Here <name> should be whatever the value of the name parameter is, if a name is provided by
the user. If the user does not provide a name, the return value should be the empty string.
As an example, if the function is called with the parameter "John": get_greetings("John")
then the returned string should be
"Greetings John! I am your virtual assistant Chat231."
Note that this string is a return value of the function. It is not an output, so you should not
print it. This is different than assignments 1 and 2 where you are printing the results to the
screen. You are not printing anything here.
The server in [Link] will call your function, get its return value, and then send this return value
to the web browser to be displayed.
Copyright © 2024. Do not distribute outside of the CPSC 231 Fall 2024 class.
Step 6:
To test your code, restart the running Python code [Link]. You must restart the server each
time you make a change to your Python code. You don’t need to reload the web page each
time you make a change.
Go to the web page, and type in any name, and click "Connect to Server". You should see the
following:
Function: full_word (sentence, word)
Step 7:
You will create the actual question and answer functionalities of Chat231. But first, you will need
to create a helper function that your other function needs to use.
Create a new custom function in your support_functions.py file, called full_word(sentence,
word).
The function has the following:
Parameters:
sentence, a string.
word, a string.
Returns: a Boolean
The Boolean return value is True if the word exists as a word in the sentence. False otherwise.
A word is defined to be existing as a word if it appears in the sentence, and is not a part of
another word. For example:
• In the sentence "oh hi you!", "hi" is a word, so the function should return True.
• In the sentence, "high up in the sky", the word "hi" does not exist as a word since it is a
part of "high", so the function should return False.
This is similar to your exercise 3 function fullword, except that now your function has to work in
the following way:
Copyright © 2024. Do not distribute outside of the CPSC 231 Fall 2024 class.
• The sentence and the word can be any strings, containing any letters, numbers, or
symbols. You can only make one assumption about the strings: they will not contain a
word from a non-Latin-alphabet language. (So the strings can contain words in English,
French, Latin, etc. but not Chinese, Russian, etc.)
• You can assume that in a sentence, any spaces or punctuations (including any other
symbols) separate two words. Numbers do not separate a word. For example,
"Chat,231" are two words. "Chat231" is one word.
• However, the string value of the parameter word is considered one word, regardless of
whether it contains spaces or other symbols in it. For example, if word is “!amaha atu!”,
then “!amaha atu!” is defined to be one single word. The space and the ! in it are
considered to be a part of this word (this rule overrides the previous rule).
More examples:
Given parameter values when function is called: Function should Reason
return:
full_word("oh hi you!", "hi") True hi is a word
full_word("high up in the sky!", "hi") False hi is a part of high
full_word("Hi chat231!", "Chat231") True Chat231 is a word
full_word("Hi chat,231!", "Chat,231") True Chat,231 is a word since
comma is now considered a
part of this word
full_word("Hi chat,231!", "Chat") True Chat is a word. The comma
separates Chat and 231.
full_word("Hi chat231!", "Chat") False Chat is a part of Chat231
This function is case insensitive – meaning any combination of upper/lower case words are
accepted as valid, so "HI" counts as appearing in "hi you." Note: this is a different requirement
than previous assignments. You are building a chatbot, after all, and people don’t follow proper
grammar when chatting.
Copyright © 2024. Do not distribute outside of the CPSC 231 Fall 2024 class.
Function: get_answer (database, sentence)
Step 8:
Create a new custom function in your support_functions.py file, called get_answer(database,
sentence). The function has the following:
Parameters:
database, a dictionary
sentence, a string.
Returns: a string.
database will be assigned by the caller to be question keywords and their corresponding
answers in a dictionary. Here is one example of what it might look like (you can’t assume this is
the only value for the dictionary, since the dictionary, as a parameter, can be assigned any
arbitrary value by whoever is calling this function):
{
"name": "I am Chat231. My nickname is Riley.",
"textbook": "The textbook we use is called The Python Workbook 2nd Edition.",
"final exam": "The final exam will be on December 19th. Check D2L for details."
The basic idea is:
Your function will scan the sentence for any keywords that appear in the dictionary. If a keyword
is found, the corresponding answer in the dictionary is returned.
For example, if the user types in the sentence “What’s your name?” , your function checks this
sentence for the keywords ("name", "textbook", "final exam") in the dictionary. If it sees the
word “name” in the sentence, it assumes the user is asking about name, and returns the answer
“I am Chat231. My nickname is Riley.”
Now, your get_answer function must call your own full_word function at some point to make
sure the keyword is, in fact, a word and not a part of another word.
For example, if the user types in “What’s your namesake?” , your function checks this sentence,
calls full_word on the keyword “name”. The function full_word returns False, telling you that
“name” is not a word in the sentence. You keep checking other keywords. If no other keywords
appear in the sentence, Chat231 can’t answer the question, and this function should return
"Sorry, I do not understand your question."
Copyright © 2024. Do not distribute outside of the CPSC 231 Fall 2024 class.
See more examples below. In these examples, we assume the dictionary example above is given
to the function.
Given parameter values when function is called: Function should return:
get_answer(database, "Name!?") "I am Chat231. …" (the full sentence)
get_answer(database, "What Textbook, do we "The textbook we use is …"
use?")
get_answer(database, "What’s on final exam.") "The final exam will be on December …"
get_answer(database, "Ha ha ha!") "Sorry, I do not understand your ..."
However, when testing your code, the database can be any dictionary. It is a parameter, so you
don't know ahead of time what it is. As stated above, strings can contain any words in English,
French, Latin, etc. so your code should work with any dictionary that contains these. If your code
only works with one particular dictionary, it will fail all the test cases in the auto-grader.
Everything here is case insensitive – any upper/lower case combination of words should be
recognized as words.
If multiple keywords appear in the sentence, choose any one keyword and return its answer.
Copyright © 2024. Do not distribute outside of the CPSC 231 Fall 2024 class.
Test cases
Your program will be tested on some test cases using an auto-grader. The result will help
determine your grade on the assignment. To help you test and fix your problems, an auto-
grader on Linux is provided to you with 5 test cases, so you can test your code yourself before
you submit. Testing your code is important, therefore you will lose points on this assignment if
you have not tested your code using our auto-grader at least once.
To test your code using our auto-grader:
Step 1: Your Python file needs to be on a lab machine. The auto-grader only runs on our Linux
machines.
If your Python file is on your own computer, you will need to copy your Python file to a Linux
machine in the CPSC labs.
• If you are at home, you will need to remotely transfer your file to a CPSC server
machine. You could use scp. (SCP instructions)
Step 2: If you are sitting in front of a lab machine, you can open a terminal on the lab machine. If
you are sitting at home, you will need to use ssh to connect to a CPSC server machine. (SSH
instructions)
Step 3: In your Terminal or Bash window, run the auto-grader by typing
/home/profs/richard.zhao1/231/a3/autograde support_functions.py
You should see the results of the auto-grader.
The auto-grader will record which students have used it at least once, but it will not record any
results. The code you submit on D2L is the only code your TA will grade. When submitting your
code, only submit support_functions.py .
The auto-grader will delete all the code that are outside a function and will delete any line that
contains the word "global".
Your grade will be determined in the following table:
Grade Letter Grade Guidelines
Point
4 A All test cases passed or with only one failed test
3.7 A- Two failed tests
3.3 B+ Three failed tests
3 B Four failed tests
2.7 B- Five failed tests
Copyright © 2024. Do not distribute outside of the CPSC 231 Fall 2024 class.
2.3 C+ Six failed tests
2 C Seven failed tests
1.7 C- Eight failed tests
1.3 D+ Nine failed tests
1 D More than nine failed tests
0 F No submission, or code does not run due to syntax errors, or late submission with no possible
extension
Your submitted code should be clearly documented with comments. Comments are lines that
start with # . This is the only form of comments we will accept.
Comments need to include: your name, and for each part of your code segments, description of
what your code segment does. Comments should be so detailed so that if someone reads the
comments without reading the code, they can understand what the code is doing.
At the end of the code, it should cite any sources you have used to complete this assignment.
Code without proper comments will get a letter grade deduction (e.g. B becomes B-).
If there are no record of the student using the auto-grader to test their code at least once
before they submit, there will be a letter grade deduction (e.g. B becomes B-).
You will receive feedback from your TA on D2L two weeks after the assignment deadline.
Copyright © 2024. Do not distribute outside of the CPSC 231 Fall 2024 class.