Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

180
LINES

< > BotCompany Repo | #30 // Eliza, your virtual psychiatrist

Lua code - Chat-bot

------------------------------------------------------------------------
-- Joseph Weizenbaum's classic Eliza ported to TinyBrain.de
-- Kein-Hong Man <khman@users.sf.net> 20060905
-- Stefan Reich <stefan.reich.maker.of.eye@gmail.com>
-- This program is hereby placed into PUBLIC DOMAIN
------------------------------------------------------------------------
-- HOW TO USE
-- * This program is for recreational purposes only. :-)
-- * ClassicEliza function will open a new buffer, then type away...
-- * Eliza recognizes one line of input only (if you are typing very
--   long lines, please consider seeing a real person.)
-- * Eliza will not interfere with other buffers or handlers, and is
--   compatible with extman.
------------------------------------------------------------------------
-- Original ELIZA paper:
--   ELIZA--A Computer Program For the Study of Natural Language
--   Communication Between Man and Machine,
--   Joseph Weizenbaum, 1966, Communications of the ACM Volume 9,
--   Number 1 (January 1966): 36-35.
--   URL: http://i5.nyu.edu/~mm64/x52.9265/january1966.html
------------------------------------------------------------------------
-- A copy of the original BASIC source of this Lua version of ELIZA can
-- be found at Josep Subirana's ELIZA download page.
------------------------------------------------------------------------

-- Redefine a Lua 5.0 function still used in this script
table.getn = function (t)
        if t.n then
            return t.n
        else
            local n = 0
            for i in pairs(t) do
                if type(i) == "number" then
                    n = math.max(n, i)
                end
            end
        return n
        end
    end

------------------------------------------------------------------------
-- Eliza main routine, processes user input
-- * Input is case insensitive. No punctuation except apostrophes,
--   as in: don't you're i'm i've you've.
------------------------------------------------------------------------
local function Eliza(text)
  local response = ""
  local user = string.upper(text)
  local userOrig = user

  -- randomly selected replies if no keywords
  local randReplies = {
    "WHAT DOES THAT SUGGEST TO YOU?",
    "I SEE...",
    "I'M NOT SURE I UNDERSTAND YOU FULLY.",
    "CAN YOU ELABORATE ON THAT?",
    "THAT IS QUITE INTERESTING!",
    "THAT'S SO... PLEASE CONTINUE...",
    "I UNDERSTAND...",
    "WELL, WELL... DO GO ON",
    "WHY ARE YOU SAYING THAT?",
    "PLEASE EXPLAIN THE BACKGROUND TO THAT REMARK...",
    "COULD YOU SAY THAT AGAIN, IN A DIFFERENT WAY?",
  }

  -- keywords, replies
  local replies = {
    [" CAN YOU"] = "PERHAPS YOU WOULD LIKE TO BE ABLE TO",
    [" DO YOU"] = "YES, I",
    [" CAN I"] = "PERHAPS YOU DON'T WANT TO BE ABLE TO",
    [" YOU ARE"] = "WHAT MAKES YOU THINK I AM",
    [" YOU'RE"] = "WHAT IS YOUR REACTION TO ME BEING",
    [" I DON'T"] = "WHY DON'T YOU",
    [" I FEEL"] = "TELL ME MORE ABOUT FEELING",
    [" WHY DON'T YOU"] = "WHY WOULD YOU WANT ME TO",
    [" WHY CAN'T I"] = "WHAT MAKES YOU THINK YOU SHOULD BE ABLE TO",
    [" ARE YOU"] = "WHY ARE YOU INTERESTED IN WHETHER OR NOT I AM",
    [" I CAN'T"] = "HOW DO YOU KNOW YOU CAN'T",
    [" SEX"] = "I FEEL YOU SHOULD DISCUSS THIS WITH A HUMAN.",
    [" I AM"] = "HOW LONG HAVE YOU BEEN",
    [" I'M"] = "WHY ARE YOU TELLING ME YOU'RE",
    [" I WANT"] = "WHY DO YOU WANT",
    [" WHAT"] = "WHAT DO YOU THINK?",
    [" HOW"] = "WHAT ANSWER WOULD PLEASE YOU THE MOST?",
    [" WHO"] = "HOW OFTEN DO YOU THINK OF SUCH QUESTIONS?",
    [" WHERE"] = "WHY DID YOU THINK OF THAT?",
    [" WHEN"] = "WHAT WOULD YOUR BEST FRIEND SAY TO THAT QUESTION?",
    [" WHY"] = "WHAT IS IT THAT YOU REALLY WANT TO KNOW?",
    [" PERHAPS"] = "YOU'RE NOT VERY FIRM ON THAT!",
    [" DRINK"] = "MODERATION IN ALL THINGS SHOULD BE THE RULE.",
    [" SORRY"] = "WHY ARE YOU APOLOGIZING?",
    [" DREAMS"] = "WHY DID YOU BRING UP THE SUBJECT OF DREAMS?",
    [" I LIKE"] = "IS IT GOOD THAT YOU LIKE",
    [" MAYBE"] = "AREN'T YOU BEING A BIT TENTATIVE?",
    [" NO"] = "WHY ARE YOU BEING NEGATIVE?",
    [" YOUR"] = "WHY ARE YOU CONCERNED ABOUT MY",
    [" ALWAYS"] = "CAN YOU THINK OF A SPECIFIC EXAMPLE?",
    [" THINK"] = "DO YOU DOUBT",
    [" YES"] = "YOU SEEM QUITE CERTAIN. WHY IS THIS SO?",
    [" FRIEND"] = "WHY DO YOU BRING UP THE SUBJECT OF FRIENDS?",
    [" COMPUTER"] = "WHY DO YOU MENTION COMPUTERS?",
    [" AM I"] = "YOU ARE",
  }

  -- conjugate
  local conjugate = {
    [" I "] = "YOU",
    [" ARE "] = "AM",
    [" WERE "] = "WAS",
    [" YOU "] = "ME",
    [" YOUR "] = "MY",
    [" I'VE "] = "YOU'VE",
    [" I'M "] = "YOU'RE",
    [" ME "] = "YOU",
    [" AM I "] = "YOU ARE",
    [" AM "] = "ARE",
  }

  -- random replies, no keyword
  local function replyRandomly()
    response = randReplies[math.random(table.getn(randReplies))].."\n"
  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 string.byte(string.sub(reply, -1)) < 65 then -- "A"
          response = response.."\n"; 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.."\n"
            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)).."\n" end
            if z < 2 then response = response.."\n" end
            return
          end--if f
        end--for
        response = response..user.."\n"
        return
      end--if d
    end--for
    replyRandomly()
    return
  end

  -- main()
  -- accept user input
  if string.sub(user, 1, 3) == "BYE" then
    response = "BYE, BYE FOR NOW.\nSEE YOU AGAIN SOME TIME.\n"
    return response
  end
  if string.sub(user, 1, 7) == "BECAUSE" then
    user = string.sub(user, 8)
  end
  user = " "..user.." "
  -- process input, print reply
  processInput()
  return response
end

if input == '' then
  return "I'm Eliza, your virtual psychiatrist. Please tell me about your worries."
else
  return Eliza(input):gsub('\n+$', '')
end

chat to bot  test run  test run with input  download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Image recognition results

Recognizer Recognition Result Visualize Recalc
#308 6799 [visualize]

Snippet ID: #30
Snippet name: Eliza, your virtual psychiatrist
Eternal ID of this version: #30/1
Text MD5: 55d5d748317052f65c6b70894666bbb4
Author: stefan
Category: bots
Type: Lua code - Chat-bot
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2013-12-21 12:49:00
Source code size: 6799 bytes / 180 lines
Pitched / IR pitched: Yes / Yes
Views / Downloads: 1427911 / 2454
Referenced in: [show references]