Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please Help: I can't seem to get this too work Assignment Requirements: A main() function that creates a chatbot then loops. Before the loop starts,

Please Help: I can't seem to get this too work image text in transcribedimage text in transcribedimage text in transcribed

Assignment Requirements:

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 the following methods:An __init__ method that has three file names as parameters and that defines three instance variables (written like self.blah)

greetings - a list of greetings read from the gr1eeting 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 parameters (besides self) 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 string split() method from class and the web.

Finally, loop over the list of words. For each word check 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, written before your class definition, that will be used by the methods in the Chatter class:

random_choice_from_list: takes a phrase_list parameter. to be used by the Chatter class. The function should generate a random index, access the given list at that index, and return that value. Refer to some of the ideas in the Dice lab for this.

make_list_from_file . Should have a filename parameter. This should read the file line by line, adding each line to a list, then returning that list.

convert_response_lines_into_dict: has 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. Return the dictionary you create.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. You should write at least 8 lines per file. The format of the files should be as follows:

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

The responses.txt 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.txt 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.

Run your program twice and have a short conversation with the chatbot. Copy the text of each conversation into an editor, then save the file as a pdf for submission.

A3chatbot - main.py A3chatbot - main.py A3chatbot : mair.py

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

Seven NoSQL Databases In A Week Get Up And Running With The Fundamentals And Functionalities Of Seven Of The Most Popular NoSQL Databases

Authors: Aaron Ploetz ,Devram Kandhare ,Sudarshan Kadambi ,Xun Wu

1st Edition

1787288862, 978-1787288867

More Books

Students also viewed these Databases questions

Question

How is data transmitted in parallel?

Answered: 1 week ago

Question

6. Describe why communication is vital to everyone

Answered: 1 week ago