Answered step by step
Verified Expert Solution
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 Build
Build
Modularize the code in Assignment Build so that:
The controlling code is in elizarun The application starts from this file.
The function getcomplainttype together with the data that it needs, are in elizafunctions The data that it needs is in the form of lists as before.
Assignment Build
This part will prepare for Build
Thus far, the key words and complaint types are part of the codein the form of lists complainttype and keywords. 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 readcomplaintdata that you are required to implement precede the function definition with the two lines shown:
complainttypes
keywords
def readcomplaintdata:
Intent: Get complainttypes and keywords 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
complainttypes list of the phrases in ElizaData.txt describing all
complaint categories
keywords 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 readcomplaintdata:
readcomplaintdata # need to execute this here
printPriting complainttypes and keywords from runtimedata..."
printcomplainttypes
printkeywords
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 readcomplaintdata changes the value of variables defined outside it you have to tell that to Python within the function definition as follows:
global complainttypes, keywords
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:
# datasource: datasource represents 'ElizaData.txtlocal file within the program
# AND datasource which is like a cursor is at the beginning of the first unread line
# Postconditions for all read: Post and Post are valid for the data read so far
# lineread: lineread contents of most recently read line from datasource
You can fulfill all three of these objectives together rather than one at a time with a few lines of code. To fulfill the linereadlast of the three objective, I assigned
to linereadin 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 lineread.endswith
:
Assignment Build
Build
Now modularize the entire program further by actually using readcomplaintdata as follows:
The controlling code is in elizarun. The application starts from this file as before.
The function getcomplainttype is in elizafunctions as before
The variables complainttypes and keywords are defined in elizaruntimedata and collected there when read as before, and
The function readcomplaintdata which obtains complainttypes and keywords, is in the file elizaruntimedata as before.
Be sure that the following code follows directly after the definition of readcomplaintdata:
readcomplaintdata # need to execute this here
ie remove the print statement that followed this previouslyas required in Build you no longer need it The line above will actually do the reading of data.
Instead of giving values to complainttypes and keywords in elizarun as you did in previous assignments, place the following line at the beginning of elizafunctions:
from elizaruntimedata import complainttypes, keywords
This will automatically execute readcomplaintdata in elizaruntimedata, and complainttypes
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started