Your task is to implement a simple chatbot in Scheme (Racket ). Here\'s a sample
ID: 3817895 • Letter: Y
Question
Your task is to implement a simple chatbot in Scheme (Racket).
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 name
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:
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?
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 name
or:
tell me more about your family name
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
import java.io.*;
import java.net.*;
public class MathServer
{
protected MathService mathService;
protected Socket socket
public void setMathService(MathService mathService)
{
this.mathService = mathService;
}
public void setSocket(Socket socket)
{ this.socket = socket; }
public void execute()
{
try
{
BufferedReader reader = new BufferedReader( new InputStreamReader(socket.getInputStream()));
// read the message from client and parse the execution
String line = reader.readLine(); double result = parseExecution(line);
// write the result back to the client
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(socket.getOutputStream()));
writer.write(“”+result); writer.newLine(); writer.flush();
// close the stream
reader.close(); writer.close();
}
catch (Exception e)
{ e.printStackTrace(); } }
// the predefined protocol for the math operation is
// operator:first value:second value
protected double parseExecution(String line)
throws IllegalArgumentException
{ double result = Double.MAX_VALUE;
String [] elements = line.split(“:”);
if (elements.length != 3)
throw new IllegalArgumentException(“parsing error!”);
double firstValue = 0; double secondValue = 0;
try
{
firstValue = Double.parseDouble(elements[1]);
secondValue = Double.parseDouble(elements[2]);
}
catch(Exception e)
{
throw new IllegalArgumentException(“Invalid arguments!”);
}
switch (elements[0].charAt(0))
{
case ‘+’: result = mathService.add(firstValue, secondValue);
break;
case ‘-’: result = mathService.sub(firstValue, secondValue);
break;
case ‘*’: result = mathService.mul(firstValue, secondValue);
break;
case ‘/’: result = mathService.div(firstValue, secondValue);
break;
default: throw new IllegalArgumentException(“Invalid math operation!”);
}
return result;
}
public static void main(String [] args)throws Exception
{
int port = 10000;
if (args.length == 1)
{
try
{
port = Integer.parseInt(args[0]);
}
catch(Exception e){ }
}
System.out.println(“Math Server is running...”);
// create a server socket and wait for client’s connection
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
// run a math server that talks to the client
MathServer mathServer = new MathServer();
mathServer.setMathService(new PlainMathService());
mathServer.setSocket(socket);
mathServer.execute();
System.out.println(“Math Server is closed...”);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.