This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-10-13 07:41:20

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

A simple learning chatbot in Python

I made a simple little learning chatbot in Python. Check it out:

Code:

known = {"hi" : "Hello!"}
history = []
print "Hi, welcome!"
while True:
    ginputd = raw_input()
    temp = ""
    for i in ginputd:
        if i != "," and i != "." and i != "!" and i != "?":
            temp = temp + str(i).lower()
    if temp == "show known" or temp == "show history":
        if temp == "show known":
            print ""
            print "    Known:"
            for i in known:
                print "        Reply to \"" + i + "\": \"" + known[i] + "\""
            print ""
        else:
            print ""
            print "    History:"
            for i in history:
                print "        " + i
            print ""
    else:
        history.append("User: " + ginputd)
        ginputd = False
        for i in known:
            if temp.endswith(i):
                ginputd = i
        if ginputd != False:
            print known[ginputd]
            history.append("Chatbot: " + known[ginputd])
        else:
            print "Whoops, I don't understand that! What would be the appropriate reply?"
            appr = raw_input()
            known[temp] = appr
            history.append("Unknown reply, user gives \"" + appr + "\" as correct output.")
            print "Thanks, I understand now."

Save that as a .py file and open it using the Python interpreter (I tested it in 2.7.2), and chat until you can't chat no more!

Unfortunately you'll have to teach it everything but "hi" and it doesn't have any memory.  tongue

There are 2 commands:

show known

   Prints all it's known replies.

show history

   Prints chat history.

Offline

 

#2 2012-10-13 08:15:22

transparent
Scratcher
Registered: 2011-04-19
Posts: 1000+

Re: A simple learning chatbot in Python

This makes me want to learn Python even more. :X


yes, yes i do.

Offline

 

#3 2012-10-13 08:16:25

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: A simple learning chatbot in Python

transparent wrote:

This makes me want to learn Python even more. :X

http://www.codecademy.com/tracks/python

Offline

 

#4 2012-10-13 08:17:18

slinger
Scratcher
Registered: 2011-06-21
Posts: 1000+

Re: A simple learning chatbot in Python

Nice work!

transparent wrote:

This makes me want to learn Python even more. :X

It's really easy :p

Last edited by slinger (2012-10-13 08:17:35)


http://s0.bcbits.com/img/buttons/bandcamp_130x27_blue.png

Offline

 

#5 2012-10-13 08:19:29

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: A simple learning chatbot in Python

slinger wrote:

Nice work!

Thanks! c: I'll make a more complex one when I have more knowledge of Python.

Offline

 

#6 2012-10-13 08:25:44

transparent
Scratcher
Registered: 2011-04-19
Posts: 1000+

Re: A simple learning chatbot in Python

Gravitation wrote:

transparent wrote:

This makes me want to learn Python even more. :X

http://www.codecademy.com/tracks/python

I guess that'll make me want to join codeacademy more as well.  tongue


yes, yes i do.

Offline

 

#7 2012-10-13 08:29:13

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: A simple learning chatbot in Python

transparent wrote:

Gravitation wrote:

transparent wrote:

This makes me want to learn Python even more. :X

http://www.codecademy.com/tracks/python

I guess that'll make me want to join codeacademy more as well.  tongue

Well, I'm learning Python there.

Their Python Labs thingy doesn't like my bot, it gives an indentation error for line 6, which there clearly isn't! > sad

Offline

 

#8 2012-10-13 08:32:17

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: A simple learning chatbot in Python

Works awesome. But not as good as my Scratch chatbot Julia  tongue


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#9 2012-10-13 08:41:54

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: A simple learning chatbot in Python

jji7skyline wrote:

Works awesome. But not as good as my Scratch chatbot Julia  tongue

Thanks. I might reprogram that in Python...  tongue

Offline

 

#10 2012-10-13 08:44:22

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: A simple learning chatbot in Python

That wouldn't be too hard, and with Python's extra features, it could become a really nice chatbot o:


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#11 2012-10-13 08:51:57

Molybdenum
Scratcher
Registered: 2012-06-17
Posts: 1000+

Re: A simple learning chatbot in Python


"The Enrichment Center is required to remind you that you will be baked, and then there will be cake."
(|Balls and Platforms: Stay on!|) (|NaOS-H: An operating system... Or is it?|)

Offline

 

#12 2012-10-13 08:59:54

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: A simple learning chatbot in Python

Yeah, I know. This one's a simple chatbot.  tongue

Offline

 

#13 2012-10-13 11:32:15

nickbrickmaster
Scratcher
Registered: 2010-02-02
Posts: 500+

Re: A simple learning chatbot in Python

Nice. This is great for a start. The explaining is kind of annoying though.


Ask me what I'm doing, wait an hour than roll a die, if it's 4-6, I'm playing Skyrim, if it's 1, I'm eating, if it's 2-3 I'm programming.
Steam: nickbrickmaster | RotMG: PwnThemAll | Minecraft: nickbrickmaster | League Of Legends: BaneOfTitans

Offline

 

#14 2012-10-13 12:17:11

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: A simple learning chatbot in Python

nickbrickmaster wrote:

Nice. This is great for a start. The explaining is kind of annoying though.

Thanks. What explaining are you talking about, exactly?  hmm

Offline

 

#15 2012-10-13 13:13:30

laser314
Scratcher
Registered: 2010-07-16
Posts: 100+

Re: A simple learning chatbot in Python

You might be able to give it memory by making it write to a .txt file, or by using Pickle(A python module.


http://alpha.scratch.mit.edu/scratchr2/static//images/logo_sm.png 2.0 Alpha Tester!http://i49.tinypic.com/1zckcqb.png

Offline

 

#16 2012-10-13 14:12:53

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: A simple learning chatbot in Python

laser314 wrote:

You might be able to give it memory by making it write to a .txt file, or by using Pickle(A python module.

Okay, done.

Guys, new version:
http://gravi.tation.tk/chat.zip

New command:

update memory

    Updates the reply memory. Do this before quitting.

Offline

 

#17 2012-10-13 15:58:59

nickbrickmaster
Scratcher
Registered: 2010-02-02
Posts: 500+

Re: A simple learning chatbot in Python

Gravitation wrote:

nickbrickmaster wrote:

Nice. This is great for a start. The explaining is kind of annoying though.

Thanks. What explaining are you talking about, exactly?  hmm

Sorry. The "Oops, I don't know what that means."


Ask me what I'm doing, wait an hour than roll a die, if it's 4-6, I'm playing Skyrim, if it's 1, I'm eating, if it's 2-3 I'm programming.
Steam: nickbrickmaster | RotMG: PwnThemAll | Minecraft: nickbrickmaster | League Of Legends: BaneOfTitans

Offline

 

#18 2012-10-13 16:17:49

MelonInc
Scratcher
Registered: 2012-06-08
Posts: 100+

Re: A simple learning chatbot in Python

Sweet! I'm trying to learn Python right now!


http://i37.tinypic.com/2qixx6c.png

Offline

 

#19 2012-10-13 16:21:26

MelonInc
Scratcher
Registered: 2012-06-08
Posts: 100+

Re: A simple learning chatbot in Python

I made something almost exactly like this in Scratch, except it took up 3 times more space! LOL


http://i37.tinypic.com/2qixx6c.png

Offline

 

#20 2012-10-14 07:01:00

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: A simple learning chatbot in Python

nickbrickmaster wrote:

Gravitation wrote:

nickbrickmaster wrote:

Nice. This is great for a start. The explaining is kind of annoying though.

Thanks. What explaining are you talking about, exactly?  hmm

Sorry. The "Oops, I don't know what that means."

Ah. You can always open the .py file in Notepad or something and change the message.

@melon Good! Python is a great beginner language. It was probably the GUI taking up all the space, this is a simple console program.

Offline

 

#21 2012-10-14 07:59:25

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: A simple learning chatbot in Python

I was planning on putting a file on my server and putting it's words there so that everybody can teach it, but of course, Python can't r/w files on the web.

Code:

known = {}
chatmem = open("http://gravi.tation.tk/chat.dat", "r")
check = chatmem.readlines()
for i in check:
    templ = []
    cont = ""
    for j in i:
        if j != "|":
            cont += j
        else:
            templ.append(cont)
            cont = ""
    templ.append(cont)
    known[templ[0]] = templ[1]
chatmem.close()
history = []
print "Chatbot ready. Please type something."
while True:
    ginputd = raw_input()
    temp = ""
    for i in ginputd:
        if i != "," and i != "." and i != "!" and i != "?":
            temp = temp + str(i).lower()
    if temp == "show known" or temp == "show history":
        if temp == "show known":
            print ""
            print "    Known:"
            for i in known:
                print "        Reply to \"" + i + "\": \"" + known[i] + "\""
            print ""
        elif temp == "show history":
            print ""
            print "    History:"
            for i in history:
                print "        " + i
            print ""
    else:
        history.append("User: " + ginputd)
        ginputd = False
        for i in known:
            if temp.endswith(i):
                ginputd = i
        if ginputd != False:
            print known[ginputd]
            history.append("Chatbot: " + known[ginputd])
        else:
            print "Whoops, I don't understand that! What would be the appropriate reply?"
            appr = raw_input()
            known = {}
            chatmem = open("http://gravi.tation.tk/chat.dat", "r")
            check = chatmem.readlines()
            for i in check:
                templ = []
                cont = ""
                for j in i:
                    if j != "|":
                        cont += j
                    else:
                        templ.append(cont)
                        cont = ""
                templ.append(cont)
                known[templ[0]] = templ[1]
            chatmem.close()
            chatmem = open(http://gravi.tation.tk/chat.dat, "w")
            chatmem.write(temp + "|" + appr)
            chatmem.close()
            known[temp] = appr
            history.append("Unknown reply, user gives \"" + appr + "\" as correct output.")
            print "Thanks, I understand now."

Can somebody help me out? I don't have any experience in server stuff, so idiot-proof please  tongue

Forgive my n00bness.

Offline

 

#22 2012-10-14 08:07:24

slinger
Scratcher
Registered: 2011-06-21
Posts: 1000+

Re: A simple learning chatbot in Python

Try urllib.
Also, you can write to files online with Python but you'll either need to put your FTP info in the script (not exactly the best thing to do  tongue ) or you can submit data via a url and let Python and PHP work hand in hand  smile


http://s0.bcbits.com/img/buttons/bandcamp_130x27_blue.png

Offline

 

#23 2012-10-14 08:16:49

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: A simple learning chatbot in Python

slinger wrote:

Try urllib.
Also, you can write to files online with Python but you'll either need to put your FTP info in the script (not exactly the best thing to do  tongue ) or you can submit data via a url and let Python and PHP work hand in hand  smile

PHP, thought so. Problem is I don't know PHP.  tongue  I've only been doing this for a week.  tongue
I'll check it out. Thanks!

Offline

 

#24 2012-10-14 08:30:37

slinger
Scratcher
Registered: 2011-06-21
Posts: 1000+

Re: A simple learning chatbot in Python

Okay, PHP isn't too hard but it is a little weird at times.
Here is basically what the code would look like (it may need to be edited  tongue ):

Code:

<html>
<head>
</head>
<body>
<?php
$chatFile = fopen("chat.txt", "w");
fwrite($chatFile, $_GET['word'].':'. $_GET['reply']); //$_GET gets data from url, I'm using : to separate
//the two variables, and reply and word are all taken from the url. 
fclose($chatFile);
</body>
</html>

Not sure if that's entirely correct, if not I don't doubt that sci or pf will change it for me  tongue
Anyway, the URL should look like this "gravi.tation.php?word=Hello&reply=Hey,%20what's%20up?"


http://s0.bcbits.com/img/buttons/bandcamp_130x27_blue.png

Offline

 

#25 2012-10-14 08:38:14

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: A simple learning chatbot in Python

slinger wrote:

Okay, PHP isn't too hard but it is a little weird at times.
Here is basically what the code would look like (it may need to be edited  tongue ):

Code:

<html>
<head>
</head>
<body>
<?php
$chatFile = fopen("chat.txt", "w");
fwrite($chatFile, $_GET['word'].':'. $_GET['reply']); //$_GET gets data from url, I'm using : to separate
//the two variables, and reply and word are all taken from the url. 
fclose($chatFile);
</body>
</html>

Not sure if that's entirely correct, if not I don't doubt that sci or pf will change it for me  tongue
Anyway, the URL should look like this "gravi.tation.php?word=Hello&reply=Hey,%20what's%20up?"

Ah, okay. Thanks! You're a great programmer.

What code do I put in my Python program?

Offline

 

Board footer