Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment 1 1 Build 1 1 . Build 1 Modularize the code in Assignment 9 Build 3 so that: The controlling code is in eliza

Assignment 11 Build 1
1. Build 1
Modularize the code in Assignment 9 Build 3 so that:
The controlling code is in eliza300_5_run_1. The application starts from this file.
The function get_complaint_type(), together with the data that it needs, are in eliza300_5_functions_1. The data that it needs is in the form of lists as before.
Assignment 11 Build 2
This part will prepare for Build 2.
Thus far, the key words and complaint types are part of the codein the form of lists complaint_type and key_words. This is not particularly desirable because whenever new kinds of complaints and key words arise, a programmer must modify the source code. It would be far better to enter these in a text file and have the program read them at runtime. Here are the specifications for the function read_complaint_data() that you are required to implement (precede the function definition with the two lines shown):
complaint_types =[]
key_words =[]
def read_complaint_data():
'''
Intent: Get complaint_types and key_words from local ElizaData.txt
Precondition =========
ElizaData.txt is a local file consisting of paragraphs of the form
On first line: 'Key Words for '
On second line:
Example of ElizaData.txt:
Key Words for Depression
depress sad
Key Words for Human Relations
conflict argument mistreat
Postconditions =========
(1) complaint_types = list of the phrases in ElizaData.txt describing all
complaint categories
{2) key_words = list of lists of words in ElizaData.txt that may occur
within phrases that describe the corresponding complaint category
'''
Place the following code immediately after the definition of read_complaint_data():
read_complaint_data() # need to execute this here
print("Priting complaint_types and key_words from ...runtime_data...")
print(complaint_types)
print(key_words)
Below is an example, showing an ElizaData.txt file, followed by the output.
Key Words for Depression
depress sad down
Key Words for Human Relations
conflict argument mistreat quarrel
Key Words for Substance Abuse
drug alcohol drink cocaine opioid
Hints:
Since read_complaint_data() changes the value of variables defined outside it, you have to tell that to Python within the function definition as follows:
global complaint_types, key_words
The function readline() returns the next line. The returned string ends with
unless its the last line. Use this knowledge to read all the lines with a while loop.
Note the function endswith():
Here are the first three of the four objectives that I used to organize the program:
# --(data_source): data_source represents 'ElizaData.txt'(local file) within the program
# AND data_source (which is like a cursor) is at the beginning of the first unread line
# --(Postconditions for all read): Post(1) and Post(2) are valid for the data read so far
# --(line_read): line_read = contents of most recently read line from data_source
You can fulfill all three of these objectives together (rather than one at a time) with a few lines of code. To fulfill the line_read(last of the three) objective, I assigned '
' to line_readin other words, without actually reading from the file yet.
Your while loop starts with a readline() and then has to restore the above three objectives each time around since you want them to remain true. I used the following:
while line_read.endswith('
'):
Assignment 11 Build 3
1. Build 3
Now modularize the entire program further by actually using read_complaint_data(), as follows:
The controlling code is in eliza300_5_run. The application starts from this file as before.
The function get_complaint_type() is in eliza300_5_functions as before
The variables complaint_types and key_words are defined in eliza300_5_runtime_data and collected there when read as before, and
The function read_complaint_data(), which obtains complaint_types and key_words, is in the file eliza300_5_runtime_data as before.
Be sure that the following code follows directly after the definition of read_complaint_data():
read_complaint_data() # need to execute this here
(i.e., remove the print statement that followed this previouslyas required in Build 1you no longer need it). The line above will actually do the reading of data.
Instead of giving values to complaint_types and key_words in eliza300_5_run as you did in previous assignments, place the following line at the beginning of eliza300_5_functions:
from eliza300_5_runtime_data_3 import complaint_types, key_words
This will automatically execute read_complaint_data() in eliza300_5_runtime_data, and complaint_types

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions