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 LISP!! Here\'s a sample chat that

ID: 3818025 • Letter: Y

Question

Your task is to implement a simple chatbot in LISP!!

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 re
import random


reflections = {
"am": "are",
"was": "were",
"i": "you",
"i'd": "you would",
"i've": "you have",
"i'll": "you will",
"my": "your",
"are": "am",
"you've": "I have",
"you'll": "I will",
"your": "my",
"yours": "mine",
"you": "me",
"me": "you"
}

psychobabble = [
[r'I need (.*)',
["Why do you need {0}?",
"Would it really help you to get {0}?",
"Are you sure you need {0}?"]],

[r'Why don'?t you ([^?]*)??',
["Do you really think I don't {0}?",
"Perhaps eventually I will {0}.",
"Do you really want me to {0}?"]],

[r'Why can'?t I ([^?]*)??',
["Do you think you should be able to {0}?",
"If you could {0}, what would you do?",
"I don't know -- why can't you {0}?",
"Have you really tried?"]],

[r'I can'?t (.*)',
["How do you know you can't {0}?",
"Perhaps you could {0} if you tried.",
"What would it take for you to {0}?"]],

[r'I am (.*)',
["Did you come to me because you are {0}?",
"How long have you been {0}?",
"How do you feel about being {0}?"]],

[r'I'?m (.*)',
["How does being {0} make you feel?",
"Do you enjoy being {0}?",
"Why do you tell me you're {0}?",
"Why do you think you're {0}?"]],

[r'Are you ([^?]*)??',
["Why does it matter whether I am {0}?",
"Would you prefer it if I were not {0}?",
"Perhaps you believe I am {0}.",
"I may be {0} -- what do you think?"]],

[r'What (.*)',
["Why do you ask?",
"How would an answer to that help you?",
"What do you think?"]],

[r'How (.*)',
["How do you suppose?",
"Perhaps you can answer your own question.",
"What is it you're really asking?"]],

[r'Because (.*)',
["Is that the real reason?",
"What other reasons come to mind?",
"Does that reason apply to anything else?",
"If {0}, what else must be true?"]],

[r'(.*) sorry (.*)',
["There are many times when no apology is needed.",
"What feelings do you have when you apologize?"]],

[r'Hello(.*)',
["Hello... I'm glad you could drop by today.",
"Hi there... how are you today?",
"Hello, how are you feeling today?"]],

[r'I think (.*)',
["Do you doubt {0}?",
"Do you really think so?",
"But you're not sure {0}?"]],

[r'(.*) friend (.*)',
["Tell me more about your friends.",
"When you think of a friend, what comes to mind?",
"Why don't you tell me about a childhood friend?"]],

[r'Yes',
["You seem quite sure.",
"OK, but can you elaborate a bit?"]],

[r'(.*) computer(.*)',
["Are you really talking about me?",
"Does it seem strange to talk to a computer?",
"How do computers make you feel?",
"Do you feel threatened by computers?"]],

[r'Is it (.*)',
["Do you think it is {0}?",
"Perhaps it's {0} -- what do you think?",
"If it were {0}, what would you do?",
"It could well be that {0}."]],

[r'It is (.*)',
["You seem very certain.",
"If I told you that it probably isn't {0}, what would you feel?"]],

[r'Can you ([^?]*)??',
["What makes you think I can't {0}?",
"If I could {0}, then what?",
"Why do you ask if I can {0}?"]],

[r'Can I ([^?]*)??',
["Perhaps you don't want to {0}.",
"Do you want to be able to {0}?",
"If you could {0}, would you?"]],

[r'You are (.*)',
["Why do you think I am {0}?",
"Does it please you to think that I'm {0}?",
"Perhaps you would like me to be {0}.",
"Perhaps you're really talking about yourself?"]],

[r'You'?re (.*)',
["Why do you say I am {0}?",
"Why do you think I am {0}?",
"Are we talking about you, or me?"]],

[r'I don'?t (.*)',
["Don't you really {0}?",
"Why don't you {0}?",
"Do you want to {0}?"]],

[r'I feel (.*)',
["Good, tell me more about these feelings.",
"Do you often feel {0}?",
"When do you usually feel {0}?",
"When you feel {0}, what do you do?"]],

[r'I have (.*)',
["Why do you tell me that you've {0}?",
"Have you really {0}?",
"Now that you have {0}, what will you do next?"]],

[r'I would (.*)',
["Could you explain why you would {0}?",
"Why would you {0}?",
"Who else knows that you would {0}?"]],

[r'Is there (.*)',
["Do you think there is {0}?",
"It's likely that there is {0}.",
"Would you like there to be {0}?"]],

[r'My (.*)',
["I see, your {0}.",
"Why do you say that your {0}?",
"When your {0}, how do you feel?"]],

[r'You (.*)',
["We should be discussing you, not me.",
"Why do you say that about me?",
"Why do you care whether I {0}?"]],

[r'Why (.*)',
["Why don't you tell me the reason why {0}?",
"Why do you think {0}?"]],

[r'I want (.*)',
["What would it mean to you if you got {0}?",
"Why do you want {0}?",
"What would you do if you got {0}?",
"If you got {0}, then what would you do?"]],

[r'(.*) mother(.*)',
["Tell me more about your mother.",
"What was your relationship with your mother like?",
"How do you feel about your mother?",
"How does this relate to your feelings today?",
"Good family relations are important."]],

[r'(.*) father(.*)',
["Tell me more about your father.",
"How did your father make you feel?",
"How do you feel about your father?",
"Does your relationship with your father relate to your feelings today?",
"Do you have trouble showing affection with your family?"]],

[r'(.*) child(.*)',
["Did you have close friends as a child?",
"What is your favorite childhood memory?",
"Do you remember any dreams or nightmares from childhood?",
"Did the other children sometimes tease you?",
"How do you think your childhood experiences relate to your feelings today?"]],

[r'(.*)?',
["Why do you ask that?",
"Please consider whether you can answer your own question.",
"Perhaps the answer lies within yourself?",
"Why don't you tell me?"]],

[r'quit',
["Thank you for talking with me.",
"Good-bye.",
"Thank you, that will be $150. Have a good day!"]],

[r'(.*)',
["Please tell me more.",
"Let's change focus a bit... Tell me about your family.",
"Can you elaborate on that?",
"Why do you say that {0}?",
"I see.",
"Very interesting.",
"{0}.",
"I see. And what does that tell you?",
"How does that make you feel?",
"How do you feel when you say that?"]]
]


def reflect(fragment):
tokens = fragment.lower().split()
for i, token in enumerate(tokens):
if token in reflections:
tokens[i] = reflections[token]
return ' '.join(tokens)


def analyze(statement):
for pattern, responses in psychobabble:
match = re.match(pattern, statement.rstrip(".!"))
if match:
response = random.choice(responses)
return response.format(*[reflect(g) for g in match.groups()])


def main():
print "Hello. How are you feeling today?"

while True:
statement = raw_input("> ")
print analyze(statement)

if statement == "quit":
break


if __name__ == "__main__":
main()

def main():
print "Hello. How are you feeling today?"

while True:
statement = raw_input("> ")
print analyze(statement)

if statement == "quit":
break

def main():
print "Hello. How are you feeling today?"

while True:
statement = raw_input("> ")
print analyze(statement)

if statement == "quit":
break

def main():
print "Hello. How are you feeling today?"

while True:
statement = raw_input("> ")
print analyze(statement)

if statement == "quit":
break

CONNECTING ELIZA TO IRC

pip install irc

Copy this code into a file called elizabot.py

import sys
import irc.bot
import irc.strings
from eliza import analyze


class ElizaBot(irc.bot.SingleServerIRCBot):
def __init__(self, channel, nickname, server, port=6667):
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.channel = channel

def on_welcome(self, connection, event):
connection.join(self.channel)

def on_pubmsg(self, connection, event):
args = event.arguments[0].split(":", 1)
if len(args) > 1 and irc.strings.lower(args[0]) == irc.strings.lower(self.connection.get_nickname()):
connection.privmsg(self.channel, "{0}: {1}".format(event.source.nick, analyze(args[1]).strip()))
return


def main():
if len(sys.argv) != 4:
print "Usage: testbot <server[:port]> <channel> <nickname>"
sys.exit(1)

server_port = sys.argv[1].split(":", 1)
server = server_port[0]
if len(server_port) == 2:
try:
port = int(server_port[1])
except ValueError:
print "Error: bad port."
sys.exit(1)
else:
port = 6667

channel = sys.argv[2]
nickname = sys.argv[3]

bot = ElizaBot(channel, nickname, server, port)
bot.start()


if __name__ == "__main__":
main()

Let’s go through it. The SingleServerIRCBot class gives us some hooks we can use to respond to server events. We can make the bot join the given channel automatically by overriding theon_welcome method.

def on_welcome(self, connection, event):
connection.join(self.channel)

def on_pubmsg(self, connection, event):
args = event.arguments[0].split(":", 1)
if len(args) > 1 and irc.strings.lower(args[0]) == irc.strings.lower(self.connection.get_nickname()):
connection.privmsg(self.channel, "{0}: {1}".format(event.source.nick, analyze(args[1]).strip()))
return

python elizabot.py chat.freenode.net #ElizaBot ElizaBot