a.i. chatbox (part 2)

Hi people!

This post is the following post from:
http://twolivesleft.com/Codea/Talk/discussion/3617/ai-chatbox#Item_7

I use the following two codes for my program, but I am unable to combine them.
I’ve tried anything, but have to admit that my knowledge is too short.
When I can combine the “menu” with the " conversation" this little project is good to go ( I suppose)!

The screen/menu:

displayMode(FULLSCREEN)

function setup()
    s1=screen1()
    s2=screen2()
    s3=screen3()
    s=1
end

function draw()
    fontSize(40)
    if s==1 then
        s1:draw()
    end
    if s==2 then
        s2:draw()
    end
    if s==3 then
        s3:draw()
    end
end

function touched(t)
    if t.state==BEGAN then
        s=s+1
        if s>3 then
            s=1
        end
    end
end

screen1=class()

function screen1:init()
end

function screen1:draw()
    background(255, 220, 0, 94)
    fill(255,0,0)
    text("screen 1",WIDTH/2,HEIGHT/2)   
    text("tap screen for next screen",WIDTH/2,HEIGHT/2-200)
end

screen2=class()

function screen2:init()
end

function screen2:draw()
    background(95, 255, 0, 95)
    fill(255,0,0)
    text("screen 2",WIDTH/2,HEIGHT/2)  
    text("tap screen for next screen",WIDTH/2,HEIGHT/2-200)
end

screen3=class()

function screen3:init()
end

function screen3:draw()
    background(0, 255, 239, 95)
    fill(255,0,0)
    text("screen 3",WIDTH/2,HEIGHT/2)   
    text("tap screen for next screen",WIDTH/2,HEIGHT/2-200)
end

The chat, basic:

function setup()
    print("Touch to show Keyboard\
and press return to show a message")
    q = {"What is your name?","What is your occupation?"} -- program's questions
    a = {"I'm pleased to meet you ","That's an interesting job "} --program's answers
    said = false
    i=1
    memory ={} -- this is for saving answers from user.
end

function draw()
    background(255)
    textMode(LEFT)

    if i<3 then
        drawquestion(q[i]) -- drawing the questions
        drawanswer(a[i]) -- drawing the answers
    end
end


function drawquestion(pre)
    text(pre,100,600)
    w,h = textSize(pre)
    if not said then
        memory[i] = keyboardBuffer()
    end
    if memory[i] then
        text(memory[i],100+w+10,600)
    end
end

function drawanswer(res)
    if i<3 and said then
        text(res..memory[i],100,600- h)
    end
end
function touched(touch)
    if touch.state==ENDED then
        showKeyboard()
        if said then
            i=i+1
        end
        said = false
    end
end


function keyboard(key)
    if key == "\
" then
        hideKeyboard()
        said =true
    end
end 

I hope this question isn’t because I am too unexperienced.
The above codes are unaltered versions from topics on this forum, although I know how to change them.
Combining them would solve my startup issues.

ps: The .lua code from Eliza isn’t compatible with codea.

ps2: Thank you so much in advance, again!

Regards

Try this:


function setup()
    Scene = 1
    --Scene 1 is menu
    --Scene 2 is ChatBox
    print("Touch to show Keyboard\
and press return to show a message")
    q = {"What is your name?","What is your occupation?"} -- program's questions
    a = {"I'm pleased to meet you ","That's an interesting job "} --program's answers
    said = false
    i=1
    memory ={} -- this is for saving answers from user.
end

function draw()
    if Scene == 1 then
        background(255)
        fontSize(45)
        text("AI ChatBox",WIDTH/2,600)
        fontSize(23)
        text("Touch To Start",WIDTH/2,500)
    elseif Scene == 2 then
    background(255)
    textMode(LEFT)

    if i<3 then
        drawquestion(q[i]) -- drawing the questions
        drawanswer(a[i]) -- drawing the answers
    end
end
end

function drawquestion(pre)
    text(pre,100,600)
    w,h = textSize(pre)
    if not said then
        memory[i] = keyboardBuffer()
    end
    if memory[i] then
        text(memory[i],100+w+10,600)
    end
end

function drawanswer(res)
    if i<3 and said then
        text(res..memory[i],100,600- h)
    end
end
function touched(touch)
    if Scene == 1 then
        Scene = 2
        end
    if Scene == 2 then
    if touch.state==ENDED then
        showKeyboard()
        if said then
            i=i+1
        end
        said = false
    end
end
end


function keyboard(key)
    if key == "\
" then
        hideKeyboard()
        said =true
    end
end 

@jonathanneels Always put 3 tildes ~ on a line before and after your code so it formats right. Pulling sections of code from different programs and thinking that just by combining them you’ll have a working program isn’t going to work, unless you know what you’re doing. These 2 sections of code have nothing to do with each other, so I’m not sure what you’re expecting to get by combining them. The chat section seems to work so far, but the screen/menu section isn’t going to do anything usefull. What are you trying to accomplish by combining the above code. Maybe then we can help you.

@dave1707

Thank you for the tilde help, I knew I was doing something wrong there…

What I actually want, is to create a startup screen before the basic code.
That’s it :p. I don’t seem to understand how to add a splashscreen (and other screens) with the “chat code” above.

Don’t mind the splashscreen code.

Thank you!
(Sorry for my little bit clumsiness.)

Hi @jonathanneels, do you want to have something like a Turing test (http://en.wikipedia.org/wiki/Turing_test)? (because of the 3 screens and the kind of ai interaction you plan to use) If so, I’m interested too! I think, it wouldn’t be difficult to combine your two pieces of program, but the interplay between the players (I think). You can concentrate, at a first attempt, on the second piece (ai vs human interaction), because it is not yet totally fine.
Greetings.

Edit: I didn’t see your new post.

@jonathanneels Is this something like what you’re after.


displayMode(FULLSCREEN)

function setup()
    menu=true
    q = {"What is your name?","What is your occupation?"} -- program's questions
    a = {"I'm pleased to meet you ","That's an interesting job "} --program's answers
    said = false
    i=1
    memory ={} -- this is for saving answers from user.
end

function draw()
    background(255)
    if menu then    -- show menu
        fontSize(30)
        textMode(CENTER)
        text("Chat Box by jonathanneels",WIDTH/2,HEIGHT-150)
        text("Touch to show Keyboard",WIDTH/2,HEIGHT-250)
        text("Press return to show a message",WIDTH/2,HEIGHT-300)
    elseif i<3 then    -- show questions/answers
        fontSize(20)
        textMode(LEFT)
        drawquestion(q[i]) -- drawing the questions
        drawanswer(a[i]) -- drawing the answers
        text("Touch to show Keyboard",100,100)
    end
end


function drawquestion(pre)
    textMode(CORNER)
    text(pre,100,600)
    w,h = textSize(pre)
    if not said then
        memory[i] = keyboardBuffer()
    end
    if memory[i] then
        text(memory[i],100+w+10,600)
    end
end

function drawanswer(res)
    if i<3 and said then
        text(res..memory[i],100,600- h)
    end
end
function touched(touch)
    if touch.state==BEGAN and menu then
        menu=false
    end
    if touch.state==ENDED then
        showKeyboard()
        if said then
            i=i+1
            if i>=3 then    -- show menu when finished
                menu=true
            end
        end
        said = false
    end
end


function keyboard(key)
    if key == "\
" then
        hideKeyboard()
        said =true
    end
end 

@dave1707 & @Epicpotato:
Yes, both are great and are what I wanted! Thank you so much!

@Quezadav:
Yes, I would like to develop a rather intelligent program where you can have a decent conversation with. It would be great to make it so complex that it passes the Turing test!

On paper I have a great way of “question posing” and “question answering” system, but for now my knowledge is a bit to scarce to implement every aspect.
First I have to figure out how I can create the option in Codea to have different answers.
Example: How are you today? (a) Great (then the program goes to another menu, silently, with a certain table of conversations). (b) Bad! (the program jumps to another menu with peptalk conversations.
→ to create a believable ai you will have a lot of different menus.

If that succeeds, I even would dare to add some secretary options (like search the web for…), but that is thinking waaaaay up ahead!

It sounds interesting! I think, you’ll need random access to different data (or knowledge) bases, in form of tables (instead menus), smartly enough arranged to simulate a “smart” bot. You can start to practice (the acces to tables) with the above given example programs: try modifying the tables “q” and “a” and the access to them ( drawquestion(q[i]) and drawanswer(a[i]) ). By the way, as suggested by dave1707 in a past comment, instead of “if i<3” use “if i<=#q” so it depends on the size of “q” (or “a”).

@quezadav

Thank you for the advice! I will try this out tomorrow .
As a dreamy addition: I was actually thinking of making the “Eliza bot” smarter through a conversation.
For example: hey J, what are your hobby’s? You answer for example football and on a later stadium she/it answers: “you STILL like football?”.

This concept might be possible in c#, but would it be impossible for lua?

Regards

Probably, try saving it as “hobby” and if the next time they answer the same thing (if answer == readLocalData("hobby") then blah blah end) you could have it say ‘you STILL like’…readData(“hobby”)