Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In PyCharm, start a new project for A3. In that project add a new Python file called Chatter.py. Write your assignment code in that Chatter.py

In PyCharm, start a new project for A3. In that project add a new Python file called Chatter.py. Write your assignment code in that Chatter.py file.

The basic idea is that a person types some text. The computer loops through each word in the typed text. If it recognizes a word, it adds a set response for the word to a list of possible responses. After checking all the typed text, the computer randomly chooses one of the set responses from the words it recognized.

Assignment Requirements

There are 3 main parts to the assignment:

A main function that creates a chatbot then loops.

Before the loop starts, a randomly chosen greeting from the computer should be printed.

Then, in the loop, input should be requested from the user and a response from the chatbot should be printed. If the person types "stop", the loop should be stopped, a final goodbye message printed, and the program ended.

A Chatter class should be defined. The Chatter class needs:An __init__ method that has three file names as parameters and that defines three instance variables (self attributes)

greetings - a list of greetings read from the greeting filename parameter. This list should be created by calling the make_list_from_file function described below.

keyword_and_response - a dictionary where the key is a word and the value is a response sentence. For example, an entry in the dictionary might be {"angry": "Tell me more about what is making you angry"} so that if the person used the word angry in their sentence, the computer could respond somewhat appropriately. This dictionary should be made using the convert_response_lines_into_dict function described below.

default_responses - a list of vague, default sentences that can be used when the person's text doesn't have any words in the keyword and response dictionary, so the computer doesn't have a response to use. This should also be made by calling the make_list_from_file function.

A greet method with no parameter that randomly returns one of the phrases from greetings. It should use the random_choice_from_list function described below and the greetings list stored in self.

A respond method with a human_text parameter. This method takes in some text from the user as a parameter and returns a sentence the computer should say. This is the most complicated method, so read this over and think about what it is doing before coding. This method should:

Make all the text in human_text lowercase. Review from class and the web the lower() method on a string.

Remove all the punctuation from the text. Online materials on this are a little varied and some are out of date. You need to "import string" at the top of your file, and then remove punctuation from the lowered-case text. For a string variable text, use the command

text = text.translate(str.maketrans('', '', string.punctuation))

and it will remove all the punctuation characters it knows about.

Then, turn the cleaned up string into a list of words. Review the split method from class and the web.

Finally, loop over the list of words. For each word see if it is a key in the keyword_and_response instance variable. The "in" keyword is helpful here - you can see if a key is "in" a dictionary. If the word is a key in the dictionary, add the key's response value to a list of potential responses. If no keywords are found after searching all the words, then make the list of potential responses be the list of default responses. Then randomly return one of the potential responses using the random_choice_from_list function described below.

Some helper functions that will be used by the methods in the Chatter class:

Write a function at the top of the code called random_choice_from_list to be used by the Chatter class. This function should have a parameter to hold a list of phrases. The function should generate a random number to pick one of those phrases and return it. Refer to some of the ideas in the Dice lab.

Write a function at the top of the code called make_list_from_file with a filename parameter. This should follow the example of the lab chatbot to open the file, to add each line as a new element in a list, then finally returning the list.

Write a function at the top of the code called convert_response_lines_into_dict with a list parameter. The list is assumed to be a list of strings, where each string holds a word, followed by a comma, followed by a response. An example line would be: angry,Why are you angry? Note that there is not a space around the comma. This function should take each line, split it on the comma, and then make the first word be a key in a dict and the text after the comma be the value for the key. You should do this with a loop and a split and code to add to a dict. There are mysterious approaches on the internet I do not want you to use.

In the descriptions above for the Chatter class, when parameters of methods are being discussed, the self parameter is assumed in the description of methods and instance variables.

You will need to make three short text files with the words and sentences the Chatter bot will use. In PyCharm, you can right-click on the project name in the left Project view, and make a new->File. These files should end in .txt as they are text files. The file names should match up with the file names used to make the Chatter bot in main. The format of the files should be as follows:

The greetings file should be a few sentence, each on their own line, with each sentence being some appropriate way of starting the conversation.

The responses file should be a few lines. Each line should start with a word, all in lower case, that is a word the Chatter bot will know to look for in user text, followed by a comma, followed by an appropriate response for that word. The description of the convert_response_lines_into_dict above has an example of such a line.

The default sentences file should contain a few sentences, each on their own line, with generic leading comments and questions that help the conversation continue when the Chatter bot doesn't recognize any of the words the user says.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 9 For Linux UNIX And Windows Advanced Database Administration Certification Certification Study Guide

Authors: Roger E. Sanders, Dwaine R Snow

1st Edition

1583470808, 978-1583470800

More Books

Students also viewed these Databases questions

Question

Main Reasons for Forming Relationships

Answered: 1 week ago

Question

2. How will the team select a leader?

Answered: 1 week ago

Question

3. What may be the goal of the team?

Answered: 1 week ago