We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
Magpie Chatbot Lab: Student Guide
Introduction
From Eliza in the 1960s to Siri and Watson today, the idea of talking to computers in natural language
has fascinated people. More and more, computer programs allow people to interact with them by typing
English sentences. The field of computer science that addresses how computers can understand human
Ianguage is called Natural Language Processing (NLP).
NLP isa field that attempts to have computers understand natural (ie, human) language. There are
many exciting breakthroughs in the field. While NLP is a complicated field, itis fairly easy to create a
simple program to respond to English sentences.
For this lab, you will explore some of the basics of NLP. As you explore this, you will work with a variety
of methods of the String class and practice using the if statement, You will trace a complicated
method to find words in user input
Activity 1: Introduction to the Magpie Clas
In this activity, you will work Magpie, with a simple implementation of a chatbot. You will see how
it works with some keywords and add keywords of your own.
Prepare
Have available:
+ the code for the Magpie
+ the code for the MagpieRunner
+ acomputer with your Java development tools
Start
Get toknow the Magpie class. Run it, using the instructions provided by your teacher.
How does it respond to:
+ My mother and I talked last night.
+ Isaid no!
+ The weather is nice.
+ Do you know my brother?Exploration
Look at the code. See how the if statement assigns a value to the response and returns that response.
‘The method getRandomResponse picks a response from a group of String objects.
‘Compare the responses from saying “I like my mother.” to “I like my Mother.” What happens? Why?
Alter the code
A) Fix the code so that it will treat uppercase characters the same as lower case characters when
determining responses.
B) Have it respond “Fell me more about your pets” when the statement contains the word “dog” or
“cat.” For example, a possible statement and response would be:
Statement: I like my cat Mittens,
Response: ell me more about your pets.
C) Have it respond favorably when it sees your name. Be sure to use appropriate pronouns!
For example, a possible statement and response would be:
Statement: Mr. Zendt is telling us about computers.
Response: He sounds pretty awesome.
D) Have the code check that the statement has at least one character. You can do this by using
the trim method to remove spaces from the beginning and end, and then checking the
length of the trimmed string, If there are no characters, the response should tell the user to
enter something. For example, a possible statement and response would be:
Statement:
Response: Say something, please.
E) Add two more noncommittal responses to the possible random responses.
F) Pick three more keywords, such as “no” and “brother” and edit the getResponse method to
respond to each of these.
G) What happens when more than one keyword appears in a string? Consider the string
“My mother has a dog but no cat.” Explain how to prioritize responses in the reply
method. (Put the explanation in comments // ).
Question
1. What happens when a keyword is included in another word? Consider statements like “I know
all the state capitals” and “I like vegetables smothered in cheese.” Explain the problem with the
responses to these statements,Activity 2: Responses that Transform Statements
As stated previously, single keywords are interesting, but better chatbots look for groups of words.
Statements like “I ike cats,” “I ike math class,” and “I like Spain” all have the form “Ilike something.”
‘The response could be “What do you like about something?” This activity will respond to groupings
of words.
Prepare
Have available:
+ the API for the Magpie class
+ the API for the String class
+ the code forthe Magpie
+ the code forthe MagpieRunner
+ acomputer with your Java development tools
Exploration
Get to know the revised Magpie class. Run it, using the instructions provided by your teacher.
How does it respond to:
+ want to build a robot
+ Iwant to understand French.
+ Doyoulike me?
+ You confuse me.
Look at the code. See how it handles “I want to” and you/me statements.
Alter the code
A) Have it respond to “I want something” statements with “Would you really be happy if you had
something?” In doing this, you need to be careful about where you place the check. Be sure you
understand why, For example:
Statement: I want a million dollars.
Response: Would you really be happy if you had a million dollars?
3B) Have it respond to statements of the form “I something you" with the restructuring “Why do you
something me?” For example
Statement: I like you.
Response: Why do you like me?
Find an example of when this structure does not work well. How can you improve it?
C) Have it respond to questions similar to a Magic 8 Ball. That means, if the user asks a question,
Magpie should respond with a random “Yes/No/Maybe/Try Again Later” answer. This is similar to the
generic responses, but instead are an answer to a question. Look for statements than end in “?” or start
with the words “Should” or “Am” in the first position:
Statement: Should I skip class and skip (hop) in the hallway?
Response: Outlook is cloudy, ask again later.
Statement: Am I'a good person
Response: Signs point to ‘yes’
Take a look at the ratio of types of responses on a Magic 8 Ball and try to replicate that. A real Magic 8
Ball has twenty possible replies, but instead, I recommend that you cut it down to ten replies for this
method.