Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your task is to implement a simple chatbot in Scheme (Racket ). A chatbot is a p

ID: 3816749 • Letter: Y

Question

Your task is to implement a simple chatbot in Scheme (Racket).

A chatbot is a program that can chat with a human. One of the earliest and most famous chatbots is Eliza (Links to an external site.).

Here's a sample chat that your program will carry out:

> (chat-with 'Name)

hi name

talk to me >>>how are you?
why do you ask?

talk to me >>>i am bored
i am bored too

talk to me >>>can you sing?
yes i can

talk to me >>>i like to sing too
good to know

talk to me >>>do you play the guitar?
no rula i do not play the guitar

talk to me >>>are you intelligent?
maybe

talk to me >>>what is the meaning of life?
what do you think?

talk to me >>>i think it is all about family and friends
tell me more about your friends rula

talk to me >>>they are great
that's nice

talk to me >>>will you help me with my work?
no rula i will not help you with your work

talk to me >>>tell me a joke
you tell me a joke

talk to me >>>why are you doing this?
why not?

talk to me >>>do you understand me?
yes i do

talk to me >>>i need to go get my umbrella
why do you need to go get your umbrella?

talk to me >>>because it is raining
is that the real reason?

talk to me >>>you drive me crazy
can you elaborate on that?

talk to me >>>bye
bye rula
have a great day!

Your program will generate a response based on the following rules:

no i (do|can|will|would) not __change person___

yes i (do|can|will|would)

why do you ask?

how would an answer to that help you?

what do you think?

why do you ask?

i don't know

i have no idea

i have no clue

maybe

The user name will be identified when the main function is called: (chat-with 'user-name)

The special topics in rule 2 will include the following: family, friend(s), mom, dad, brother, sister, girlfriend, boyfriend, children, son, daughter, child, wife, husband, home, dog, cat, pet. If more than one special topic is found in the input, your program will pick one randomly and ask the user about it. For example, the response to:

talk to me >>>i think it is all about family and friends

can be either:
tell me more about your friends rula

or:
tell me more about your family rula

In rule 1 and rule 8, when the chatbot repeats part of the user's input, the pronouns must be changed as follows:

I becomes you

am becomes are

my becomes your

your becomes my

me becomes you

you becomes me

talk to me >>>will you help me with my work?
no rula i will not help you with your work

talk to me >>>i want to understand your algorithm
why do you want to understand my algorithm?

talk to me >>>i think i am done
why do you think you are done?

talk to me >>>i have to understand you
why do you have to understand me?

For rule 10, you will use a short list of verbs that includes: tell, give, say.

STARTER CODE:

;;; starter code

;;; We'll use the random function implemented in Racket
;;; (random k) returns a random integer in the range 0 to k-1
(#%require (only racket/base random))

;;; some input and output helper functions

;;; prompt: prompt the user for input
;;; return the input as a list of symbols
(define (prompt)
(newline)
(display "talk to me >>>")
(read-line))

;;; read-line: read the user input till the eof character
;;; return the input as a list of symbols
(define (read-line)
(let ((next (read)))
(if (eof-object? next)
'()
(cons next (read-line)))))

;;; output: take a list such as '(how are you?) and display it
(define (output lst)
(newline)
(display (to-string lst))
(newline))

;;; to-string: convert a list such as '(how are you?)
;;; to the string "how are you?"
(define (to-string lst)   
(cond ((null? lst) "")
((eq? (length lst) 1) (symbol->string (car lst)))
(else (string-append (symbol->string (car lst))
" "
(to-string (cdr lst))))))


;;; main function
;;; usage: (chat-with 'your-name)

(define (chat-with name)
(output (list 'hi name))
(chat-loop name))

;;; chat loop
(define (chat-loop name)
(let ((input (prompt))) ; get the user input
(if (eqv? (car input) 'bye)
(begin
(output (list 'bye name))
(output (list 'have 'a 'great 'day!)))
(begin
   (reply input name)
(chat-loop name)))))


;;; your task is to fill in the code for the reply function
;;; to implement rules 1 through 11 with the required priority
;;; each non-trivial rule must be implemented in a separate function
;;; define any helper functions you need below
(define (reply input name)
(output (pick-random generic-response))) ; rule 11 has been implemented for you

;;; pick one random element from the list choices
(define (pick-random choices)
(list-ref choices (random (length choices))))

;;; generic responses for rule 11
(define generic-response '((that's nice)
(good to know)
(can you elaborate on that?)))

if the input is of the form: your program will generate a response (randomly chosen) 1 (do|can|will|would) you ________________?

no i (do|can|will|would) not __change person___

yes i (do|can|will|would)

2 _____(special topics)_______ tell me more about your (special topics)... 3 why __________? why not? 4 how __________?

why do you ask?

how would an answer to that help you?

5 what __________?

what do you think?

why do you ask?

6 _____________?

i don't know

i have no idea

i have no clue

maybe

7 ______because_______ is that the real reason? 8 i (need|think|have|want) ________ why do you (need|think|have|want) __change person_____? 9 i __________ (last word is not too) i __________ too 10 verb ________________ you verb ______________ 11 everything else good to know
that' s nice
can you elaborate on that?

Explanation / Answer

#pragma warning(disable: 4786)

#include <iostream>
#include <string>
#include <vector>
#include <ctime>

const int MAX_RESP = 3;

typedef std::vector<std::string> vstring;

vstring find_match(std::string input);
void copy(char *array[], vstring &v);


typedef struct {
char *input;
char *responses[MAX_RESP];
}record;

record arr[] = {
{"Enter name ",
{"My name is su.",
"WHY DO YOU WANT TO KNOW MY NAME?"}
},

{"hi",
{"hi there!",
"how are you?",
"hi!"}
},
  
{"how are you",
{"i am doing fine!",
"iam doing well and you?",
"why do you want to know how am i doing?"}
},


};

size_t narrSize = sizeof(arr)/sizeof(arr[0]);


int main() {
srand((unsigned) time(NULL));

std::string sInput = "";
std::string sResponse = "";

while(1) {
std::cout << ">";
std::getline(std::cin, sInput);
vstring responses = find_match(sInput);
if(sInput == "BYE") {
std::cout << "it was nice talking to you user, see you nexttime!" << std::endl;
break;
}
else if(responses.size() == 0) {
std::cout << "I'm not sure if i understand what you are talking abouT." << std::endl;
}
else {
int nSelection = rand() % MAX_RESP;
sResponse = responses[nSelection]; std::cout << sResponse << std::endl;
}
}

return 0;
}
  

vstring find_match(std::string input) {
vstring result;
for(int i = 0; i < narrSize; ++i) {
if(std::string(arr[i].input) == input) {
copy(arr[i].responses, result);
return result;
}
}
return result;
}

void copy(char *array[], vstring &v) {
for(int i = 0; i < MAX_RESP; ++i) {
v.push_back(array[i]);
}
}