ELIZA

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

ELIZA

Post by BasicCoder2 »

This is my version of the old Eliza program that came with my first computer, a TRS80.
You type bye to exit the program. You can add extra keywords and their replies as a block of data statements and then change the value of
the constant KEYWORD_COUNT to the number of data blocks.

Code: Select all

'===============================================
'                READ DATA
'===============================================
const KEYWORD_COUNT = 41 'change with each addition to data

dim as string conj1(1 to 13),conj2(1 to 13)
for i as integer = 1 to 13
    read conj1(i),conj2(i)
next i

dim as string  keyWords(0 to KEYWORD_COUNT)       'key word
dim as integer replyCount(0 to KEYWORD_COUNT)     'number of replies for this key word
dim as string  replies(0 to KEYWORD_COUNT,0 to 9) 'up to 10 replies in each of the 50 reply groups

for i as integer = 0 to KEYWORD_COUNT
    read keyWords(i)
'    print keyWords(i)
    read replyCount(i)
'    print "replyCount =";replyCount(i)
    for j as integer = 0 to replyCount(i)-1
        read replies(i,j)
'        print replies(i,j)
    next j
'    print
'    sleep
next i

'sleep
'end

'===============================================

function removeExcessSpaces(s as string) as string
    dim as integer ii = 1
    while ii < len(s)
        while mid(s,ii,1) = " " and mid(s,ii+1,1)=" "
            s = left(s,ii) + right(s,len(s)-ii-1)
        wend
        ii = ii + 1
    wend
    return s
end function

'================================================
function removeChar(s as string,char as string) as string
    dim as string newString = ""
    for i as integer = 1 to len(s)
        if mid(s,i,1)<> char then
            newString = newString + mid(s,i,1)
        end if
    next i
    return newString    
end function

'====================================================

function swapWords(s as string,w1 as string,w2 as string) as string
    for p as integer = 1 to len(s)
        if mid(s,p,len(w1)) = w1 then
            s = left(s,p-1) + w2 + right(s,  len(s) - p - len(w1) + 1)
        end if
    next p
    return s
end function

'=============================================

dim as string  previousResponse  'to check for input repetition
dim as string  Response    'input Response
dim as string  extension   'string snipped off Response after any found keyword
dim as string  reply
dim as string  previousReply 'computer repeats itself
dim as string  keyword     'used to select a reply
dim as integer keyNumber   'number of keyword to address replies

'=========================================

function getResponse() as string
    dim as string s
    input s
    s = UCASE(s)   'key words are in capital case only
    s = removeChar(s,"'")
    s = removeExcessSpaces(s)
    s = " " + s + " "
    return s
end function

'=========================================

print "HI! I'M ELIZA. LET'S TALK.  TYPE 'BYE' TO END THIS SESSION."

'get user response

Response = getResponse()

while Response <> " BYE "

    if Response = previousResponse then
        print "You have already said that"
    else 'search for keyword
        
        previousResponse = Response  'save before it is changed
        
        keyword = "NOKEYFOUND"  'default word
        keyNumber = 0           'default word number
        
        'scans keywords for anything not NOKEYFOUND and if it finds one chooses
        'one of the responses attached to that keyword.
        
        for i as integer = 1 to KEYWORD_COUNT  'with each keyword skip first one, default nokeyfound
            
            for p as integer = 1 to len(Response) - len( keyWords(i) ) + 1 'search Response
                
                'the " " borders the keyword string so something like IM doesn't match an internal IM such as in LIME
                if mid(Response, p, len(keyWords(i))+2 ) = " " + keyWords(i) + " " then  'match
                    
                    'if keyword = "NOKEYFOUND then this match is the first find
                    'otherwise a match has been found so don't change it to any
                    'matches further in the response string.
                    
                    if keyword = "NOKEYFOUND" then  
                        
                        keyWord = keyWords(i)
                        keyNumber = i   'number of keyword to find replies
                        
                        'snip off extension after keyword in case it is used
                        extension = right(Response,len(Response)-p-len(keyWord)-1 ) 
                        'look for each conj1 to swap with conj2
                        for ii as integer = 1 to 13  
                            swapWords(extension,conj1(ii),conj2(ii))
                        next ii
                        extension = removeChar(extension,"+")
                        
                    end if
                    
                end if
                
            next p
        next i

        'now use key word to select a reply
        dim as integer choice
        
        'select a random reply number
        choice = int(rnd(1)*replyCount(keyNumber))
        reply = replies(keyNumber, choice)
        
        'prevent computer repeating a reply
        while reply = previousReply
            'select a random reply number
            choice = int(rnd(1)*replyCount(keyNumber))
            reply = replies(keyNumber, choice) 
        wend
        previousReply = reply  'save reply
        
        ' if reply has extension character * remove the * and
        ' insert extension to the reply string
        
        'search for '*'
        for i as integer = 1 to len(reply)
            if mid(reply,i,1)="*" then
                reply = left(reply,i-1) + extension + right(reply,len(reply)-i-1)
            end if
        next i
    
        color 11
        print "keyword found:";
        color 14
        print keyword
        color 15
        print reply  'print the reply
    
    end if
    
    Response = getResponse()  'get the next user response

wend

print "Goodbye."
sleep 1000


  '                --STRING DATA FOR CONJUGATIONS---

  'the reason for the + is so if the first is found and changed such
  'as ARE to AM it isnt changed back again to ARE if further along
  'the string an AM is found. After the process any + is removed.
  
  '       conj1       conj2
    DATA " ARE "   , " AM+ "
    DATA " AM "    , " ARE+ "
    DATA " WERE "  , " WAS+ "
    DATA " WAS "   , " WERE+ "
    DATA " YOU "   , " I+ "
    DATA " I "     , " YOU+ "
    DATA " YOUR "  , " MY+ "
    DATA " MY "    , " YOUR+ "
    DATA " YOUVE " , " IVE+ "
    DATA " IM "    , " YOURE+ "
    DATA " ME "    , " YOU+ "
    DATA " YOU "   , " ME+ "
    DATA " ANY "   , " "


  ' data layout is:
  ' keyword
  ' number of replies
  ' replies


data "NOKEYFOUND"
data 4
data "I'm not sure I understand you fully."
data "Come elucidate your thoughts."
data "Can you elaborate on that?"
data "That is quite interesting."

data "I REMEMBER"
data 6
data "Do you often think of *?"
data "Does thinking of * bring anything else to mind?"
data "What else do you remember?"
data "Why do you recall * right now?"
data "What in the present situation reminds you of *?"
data "What is the connection between me and *?"
  
data "CAN YOU"
data 3
data "Don't you believe that I can *?"
data "Perhaps you would like to be able to *?"
data "You want me to be able to *?"

data "CAN I"
data 2
data "Perhaps you don't want to *?"
data "Do you want to be able to *?"

data "YOU ARE"
data 4
data "What makes you thing I am *"
data "Does it please you to believe I am *?"
data "Perhaps you would like to be *?"
data "Do you sometimes wish you were *?"

data "YOURE"
data 4
data "What makes you thing I am *?"
data "Does it please you to believe I am *"
data "Perhaps you would like to be *?"
data "Do you sometimes wish you were *?"

data "I DONT"
data 4
data "Don't you really *?"
data "Why don't you *"
data "Do you wish to be able to *?"
data "Does that trouble you?"

data "I FEEL"
data 3
data "Tell me more about such feelings."
data "Do you often feel *?"
data "Do you enjoy feeling *?"

data "WHY DONT YOU"
data 4
data "Do you really believe I don't *?"
data "Do you want me to *?"
data "Should you * yourself?"
data "Perhaps I will * in good time"

data "WHY CANT I"
data 2
data "Do you think you should be able to *?"
data "Why can't you *?"

data "ARE YOU"
data 3
data "Why are you interested in whether or not I am *?"
data "Would you prefer if I were not *?"
data "Perhaps in your fantasies I am *?"

data "WHY CANT I"
data 3
data "How do you know you can't *?"
data "Have you tried?"
data "Perhaps you can now *?"

data "I AM"
data 4
data "Did you come to me because you are *?"
data "How long have you been *?"
data "Do you believe it is normal to be *?"
data "Do you enjoy being *?"

data "IM"
data 4
data "Did you come to me because you are *?"
data "How long have you been *?"
data "Do you believe it is normal to be *?"
data "Do you enjoy being *?"

data "YOU ARE"
data 3
data "We were discussing you-- not me."
data "Oh, I *?"
data "You're not really talking about me, are you?"

data "I WANT"
data 5
data "What would it mean to you if you got *?"
data "Why do you want *?"
data "Suppose you soon got *?"
data "What if you never got *?"
data "I sometimes also want *?"

data "WHAT"
data 9
data "Why do you ask?"
data "Does that question interest you?"
data "What answer would please you the most?"
data "What do you think?"
data "Are such questions on your mind often?"
data "What is it that you really want to know?"
data "Have you asked anyone else?"
data "Have you asked such questions before?"
data "What else comes to mind when you ask that?"

data "HOW"
data 9
data "Why do you ask?"
data "Does that question interest you?"
data "What answer would please you the most?"
data "What do you think?"
data "Are such questions on your mind often?"
data "What is it that you really want to know?"
data "Have you asked anyone else?"
data "Have you asked such questions before?"
data "What else comes to mind when you ask that?"

data "WHEN"
data 9
data "Why do you ask?"
data "Does that question interest you?"
data "What answer would please you the most?"
data "What do you think?"
data "Are such questions on your mind often?"
data "What is it that you really want to know?"
data "Have you asked anyone else?"
data "Have you asked such questions before?"
data "What else comes to mind when you ask that?"

data "WHERE"
data 9
data "Why do you ask?"
data "Does that question interest you?"
data "What answer would please you the most?"
data "What do you think?"
data "Are such questions on your mind often?"
data "What is it that you really want to know?"
data "Have you asked anyone else?"
data "Have you asked such questions before?"
data "What else comes to mind when you ask that?"

data "WHY"
data 9
data "Why do you ask?"
data "Does that question interest you?"
data "What answer would please you the most?"
data "What do you think?"
data "Are such questions on your mind often?"
data "What is it that you really want to know?"
data "Have you asked anyone else?"
data "Have you asked such questions before?"
data "What else comes to mind when you ask that?"


data "NAME"
data 2
data "Names don't interest me."
data "I don't care about names-- please go on."

data "CAUSE"
data 4
data "Is that the real reason?"
data "Don't any other reasons come to mind?"
data "Does that reason explain anything else?"
data "What other reasons might there be?"

data "SORRY"
data 4
data "Please don't apologize!"
data "Apologies aren't necessary."
data "What feelings do you have when you apologize?"
data "Don't be so defensive!"

data "DREAM"
data 4
data "What does that dream suggest to you?"
data "Do you dream often?"
data "What persons appear in your dreams?"
data "Are you disturbed by your dreams?"

data "HELLO"
data 2
data "How do you do ... Please state your problem."
data "Hello. Please state your problem."

data "HI"
data 2
data "Hi. Please state your problem."
data "How do you do. Please state your problem."

data "MAYBE"
data 5
data "You don't seem quite certain."
data "Why the uncertain tone?"
data "Can't you be more positive?"
data "You aren't sure?"
data "Don't you know?"

data "NO"
data 5
data "Are you saying no just to be negative?"
data "You are being a bit negative."
data "Why not?"
data "Are you sure?"
data "Why no?"

data "YOUR"
data 2
data "Why are you concerned about my *?"
data "What about your own *?"

data "ALWAYS"
data 4
data "Can you think of a specific example?"
data "When?"
data "What are you thinking of?"
data "Really, always!!"

data "THINK"
data 3
data "Do you really think so?"
data "But you are not so sure you *?"
data "Do you doubt you *?"

data "ALIKE"
data 7
data "In what way?"
data "What resemblance do you see?"
data "What does the similarity suggest to you?"
data "What other connections do you see?"
data "Could there really be some connection?"
data "How?"
data "You seem quite positive."

data "YES"
data 3
data "Are you sure?"
data "I see."
data "I understand."

data "FRIEND"
data 6
data "Why do you bring up the topic of friends?"
data "Do your friends worry you?"
data "Do your friends pick on you?"
data "Are you sure you have any friends?"
data "Do you impose on your friends?"
data "Perhaps your love for friends worries you."

data "COMPUTER"
data 10
data "Do computers worry you?"
data "Are you talking about me in particular?"
data "Are you frightened by machines?"
data "Why do you mention computers?"
data "What do you think machines have to do with your problem?"
data "Don't you think computers can help people?"
data "What is it about machines that worry you?"
data "Say, do you have any psychological problems?"
data "What does that suggest to you?"
data "I see."

data "COMPUTERS"
data 10
data "Do computers worry you?"
data "Are you talking about me in particular?"
data "Are you frightened by machines?"
data "Why do you mention computers?"
data "What do you think machines have to do with your problem?"
data "Don't you think computers can help people?"
data "What is it about machines that worry you?"
data "Say, do you have any psychological problems?"
data "What does that suggest to you?"
data "I see."

data "I LIKE"
data 2
data "How long have you liked *?"
data "Why do you like *?"

data "WAS I"
data 3
data "What if you were *?"
data "Do you think you were *?"
data "What would it mean if you were *?"

data "I HAVE"
data 2
data "How long have you had *?"
data "Do you like having *?"

data "I DREAMT"
data 2
data "How do you feel about * in reality?"
data "Do you dream often?"

data "I WAS"
data 3
data "Were you really?"
data "Perhaps I already knew you were *?"
data "Why do you tell me you were * now?"

Lothar Schirm
Posts: 437
Joined: Sep 28, 2013 15:08
Location: Germany

Re: ELIZA

Post by Lothar Schirm »

Can this be regarded as a first prototype of chat gpt? :lol:
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: ELIZA

Post by BasicCoder2 »

The lesson that perhaps can be taken from the Eliza program is not to confuse clever programming and a clever program.
Soloman
Posts: 1
Joined: May 01, 2023 7:36
Location: Netherlands, Europe

Re: ELIZA

Post by Soloman »

Yesterday I installed FreeBasic. To test the compiler I used this program. It worked very well!
Luxan
Posts: 222
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: ELIZA

Post by Luxan »

I was able to compile the old version with '$lang: "fblite" .


The more recent version was easy to compile as a standard fb program .

This is weird and mind bending at the same time.

Might this be relevant in, say genetics .
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: ELIZA

Post by BasicCoder2 »

@Luxan

If you understand how it works it shouldn't be mind bending. I find software like chatGPT kind of astonishing because I can't visualize how those natural language outputs are generated from all the text data used to train the recurrent neural networks.

They say it is something like predictive text meaning given a series of words you can predict the probability of the next word. Seems like a kind of associative memory just as "A" predicts "B" predicts "C" in the context of the alphabet. Your input sentence predicts the next sentence which in turn predicts the sentence after that.

Human associative memory is only in one direction as you will notice if you try and recite the alphabet backwards. The ability of humans to quickly form strong associations is displayed in the ability to learn how to remember things like the order of a random single presentation of playing cards without the need for repetition to fix the sequence in your head.

I cannot see the Eliza program being relevant with regards to genetics.

Some Freebasic attempts at genetic programs.

https://freebasic.net/forum/viewtopic.php?p=294633
viewtopic.php?p=143880
Luxan
Posts: 222
Joined: Feb 18, 2009 12:47
Location: New Zealand

Re: ELIZA

Post by Luxan »

I've just read up on Eliza, tomorrow morning I likely will
comprehend better, at first glance it appears to use weighted key words
syntax checking and some string operations.
As it isn't particularly useful I shan't dwell upon it.


As for Chat-GPT, if that's just associative memory based then how does
it differ from a parser or lexer , like bison.

Also some decades ago now, I wrote an equation parser in BASIC using
Backus-Naur diagrams, does that have a vague similarity to Chat-GPT.

The links you provided to the genetic algorithms are interesting, especially
if I get some ideas on implementing other versions , for other purposes.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: ELIZA

Post by BasicCoder2 »

@Luxan
As it isn't particularly useful I shan't dwell upon it.
Some found it useful to talk about their problems without judgement and without answers.

I found it interesting in showing how you can use keywords to search for a response without any deep analysis of the sentence or even what it means. Although Eliza does not answer questions you could use keywords to answer questions although it would be limited by the size of the data base. Maybe a data base of questions and answers. Maybe someone who knows how to write a program to access data on the internet using google could use the data in the links rather than just giving the link itself to generate a reply. That would of course be plagiarism but is it any less plagiarism when you "remember" how to make a cake and then put it in your own words. I suspect that is what chatGPT does and every student does when they answer questions in an exam or a teacher gets paid to do when they regurgitate things they have remembered.

I wonder how much of ChatGPT is just the ability to collect information in many forms and then rewrite it using rules for generating text?
Post Reply