MooBot
A simple Eliza like chat bot.
- Code:
- local function MooBot(text)
local response = "" -- Our response
local user = string.upper(text) -- Message from user in upper case
local userOrig = user
-- randomly selected replies if no keywords
local randReplies = {
"WUT DUS DAT SUJEST 2 CHU?",
"AI SEH...",
"I IZ NAWT SHURE AI UNDERSTIND CHU FULLEH",
"CAYN CHU ELABIRITE IN DAT?",
"DAT IZ QUIETE INATESTIN'",
"DATS SO... PLZ CONTINUE...",
"AI UNDERSTIND.",
"WEL, WEL, GU IN..",
"WAI ARE CHU SAYIN' DAT?",
"PLZ EKSPLIN DA BICKGROND 2 DAT REMACK.",
"CULD CHU SAI DAT AGAN? EN EH DIFFIRNT WAI?",
}
-- keywords, replies
local replies = {
[" CAN YOU"] = "NU, BUTT CHU SHULD B ABLE 2",
[" DO YOU"] = "YESH, AI",
[" CAN I"] = "MAY B CHU DON'T WANT TO",
[" YOU ARE"] = "WUT MAKS CHU THANK AI AM",
[" YOU'RE"] = "WUT IZ CHUR REACTION 2 MEH BEIN'",
[" I DON'T"] = "WAI DUN'T CHU",
[" I FEEL"] = "WHY ARE CHU FEELIN'",
[" WHY DON'T YOU"] = "NU I WILL NOT",
[" WHY CAN'T I"] = "CHU CAYN",
[" ARE YOU"] = "WAI R CHU INTERESTED IF AH AM",
[" I CAN'T"] = "ORLY NAO? CHU CAN'T",
[" SEX"] = "EW, AI DU NUT WANT 2 KNEW ABUT",
[" I AM"] = "HAO LONG HAD CHU BEAN",
[" I'M"] = "WAI CHU BE TELLIN MEH THAT CHUR",
[" I WANT"] = "WAI DO CHU WNT",
[" WHAT"] = "WUT DO CHU THANK?",
[" HOW"] = "WUT ANSWER WOULD PLEASE YOU THE MOST",
[" WHO"] = "HOW OFTEN DU CHU THANK UF SICH QUESTIONS??",
[" WHERE"] = "WHY DID YOU THINK OF THAT?",
[" WHEN"] = "WHAT WOULD YOUR BEST FRIEND SAY TO THAT QUESTIONZ?",
[" WHY"] = "WAT IZ IT DAT CHU REELY WUNT 2 KNOW?",
[" PERHAPS"] = "CHU R NAWT VERY FERM ON DAT!",
[" DRINK"] = "BEEER TAIME?",
[" SORRY"] = "WAI R CHU APOLOGIZING",
[" DREAMS"] = "WAI DID CHU BRANG IP TEH SIBGECT OF DREAMZ?",
[" I LIKE"] = "IS IT GOOD THAT YOU LIKE",
[" MAYBE"] = "CHU SPELT MAY B WRONG",
[" NO"] = "DONUT B NEGITAIVE",
[" YOUR"] = "WAI ARE CHU CONCERNED ABUT MAI",
[" ALWAYS"] = "CAN YOU THINK OF A SPECIFIC EXAMPLE?",
[" THINK"] = "DO YOU DOUBT",
[" YES"] = "YUS? YUS.",
[" FRIEND"] = "WAI DU CHU BRANG UP FRIENDZ?",
[" COMPUTER"] = "WAI DU CHU MENTION COMPUTORZ",
[" AM I"] = "CHU R NAWT",
}
-- conjugate
local conjugate = {
[" I "] = "CHU",
[" ARE "] = "IZ",
[" WERE "] = "WAZ",
[" YOU "] = "MEH",
[" YOUR "] = "MAI",
[" I'VE "] = "YOU HAV",
[" I'M "] = "CHUR",
[" ME "] = "CHU",
[" AM I "] = "CHU'RE",
[" AM "] = "R",
}
-- random replies, no keyword
local function replyRandomly()
response = randReplies[math.random(table.getn(randReplies))].." "
end
-- find keyword, phrase
local function processInput()
for keyword, reply in pairs(replies) do
local d, e = string.find(user, keyword, 1, 1)
if d then
-- process keywords
response = response..reply.." "
-- If reply already ends in a "?", then we're done.
if string.byte(string.sub(reply, -1)) < 65 then -- "A" = 65, "?" = 63
return
end
local h = string.len(user) - (d + string.len(keyword))
if h > 0 then
user = string.sub(user, -h)
end
for cFrom, cTo in pairs(conjugate) do
local f, g = string.find(user, cFrom, 1, 1)
if f then
local j = string.sub(user, 1, f - 1).." "..cTo
local z = string.len(user) - (f - 1) - string.len(cTo)
response = response..j.." "
if z > 2 then
local l = string.sub(user, -(z - 2))
if not string.find(userOrig, l) then return end
end
if z > 2 then response = response..string.sub(user, -(z - 2)) end
if z < 2 then response = response end
return
end--if f
end--for
response = response..user
return
end--if d
end--for
-- If we haven't found a reply by now, pick from our random list
replyRandomly()
return
end
-- First word is "BYE"
if string.sub(user, 1, 3) == "BYE" then
response = "BAI."
return response
end
-- Trim off leading "BECAUSE"
if string.sub(user, 1, 7) == "BECAUSE" then
user = string.sub(user, 8)
end
user = " "..user.." "
processInput()
return response
end
function onMsgReceived(message, player)
if message ~= "" and not player:isRobot() then
bot:globalMsg(MooBot(message))
end
end
-------------------------------------------------------------------------------
-- This function is called once and should return the robot's name
function getName()
return("MooBot")
end
------------------------------------------------------------------------
-- setup
------------------------------------------------------------------------
function main()
globalMsg("MOOBOT 1.0 BOOTED")
globalMsg("MADE IN 2013")
globalMsg("PUBLIC EDITION")
subscribe(Event.MsgReceived)
unsubscribe(Event.Tick)
end
i got a rattlesnake gun.