The code uses place holders with numbered indexes and String
“format” Method to specify the format of the response and then
create the response. Make sure you understand these ideas by going
through the code. Focus on how the response pattern is specified in
list “eliza_engine” and formatted response is generated using
“format” method inside the “regex_replace” function. • The
parameters to “format” method uses the idea of unpacking operator
(*) in Python applied to collection data types such as lists and
tuples. Make sure you understand this idea as well. 3. At current
situation, the “regex_replace” function returns combination of “You
Said: ” and the input, as the response for inputs shown in the
following table.
1)
Add a rule to the list “eliza_engine” so that the corresponding
output given in the table will be returned for such string
Input | Output |
Well, my boyfriend made me come here. | WHY DID YOUR BOYFRIEND MADE YOU COME HERE? |
Well, my friend made me run fast. | WHY DID YOUR FRIEND MADE YOU RUN FAST? |
Well, my parents made me go there. | WHY DID YOUR PARENTS MADE YOU GO there? |
Well, my boyfriend made him go away | You Said: WELL, MY BOYFRIEND MADE HIM GO AWAY. |
my friend made me run fast. | You Said: MY FRIEND MADE ME RUN FAST. |
2) you should be comfortable with searching using regular
expression in Python and the ideas of using “re.search” method,
accessing capture groups in search results, using format function
and place holders to format strings, using unpack operator in
Python and writing regular expressions with capture groups.
import re
def simple_eliza_demo(user_input):
uip = user_input.upper()
eliza_engine = [ [“^.* I’M (DEPRESSED|SAD) .*”, “I AM SORRY TO HEAR
YOU ARE {0}”, “true” ] ,
[“^.* I AM (DEPRESSED|SAD) .*”, “WHY DO YOU THINK YOU ARE {0}”,
“true” ],
[“^.* ALWAYS .*”, “CAN YOU THINK OF A SPECIFIC EXAMPLE?”, “false”
],
[“^.* ALL .*”, “IN WHAT WAY?”, “false”] ]
for rule in eliza_engine:
response = regex_replace(uip, rule[0], rule[1], rule[2])
if(response):
return response
return “You Said: ” + uip
def regex_replace(text, pattern, response, add_match):
result = re.search(pattern, text);
if(result):
if(add_match == ‘true’):
return response.format(*result.groups());
return response;
inputs = [ “Hello! I’M depressed and tired.”,
“Hello! I AM depressed and tired.”,
“Hello! I’M sad and tired.”,
“Hello! I AM tired and sad.”,
“Hello! I’M tired and sleepy.”,
“Men are all alike.”,
“They aere always bugging us about something or other.”,
“Well, my boyfriend made me come here.”,
“He says I’m depressed much of the time.” ]
for inp in inputs:
print(“Input: ” + inp)
print(“Response: ” + simple_eliza_demo(inp))
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more