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

#26 2012-10-14 08:54:31

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

Re: A simple learning chatbot in Python

Gravitation wrote:

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?

Nevermind! Thank you so much, slinger. I have a problem, your PHP script erases the whole file before adding the new line.

Offline

 

#27 2012-10-14 09:04:20

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

Re: A simple learning chatbot in Python

Hmm, at least it adds something!  tongue
(Sorry about that, I hope you didn't get a huge list going D: )
You could try this (I'm pretty bad at php so don't expect this to work perfectly either :p)

Code:

fwrite($chatFile, $chatFile.'\n'.$_GET['word'].':'. $_GET['reply']);

Gravitation wrote:

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.

:')

Last edited by slinger (2012-10-14 09:05:43)


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

Offline

 

#28 2012-10-14 09:10:25

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

Re: A simple learning chatbot in Python

Don't worry, I'm worse.  tongue
And no, I didn't. I tested it in my browser.  big_smile

Oh, and I put a comment in my code to give credit to you.
But it's still not working, after running
http://gravi.tation.tk/chat.php?word=testing&reply=Cool, you're testing something!
chat.dat is

Resource id #9\ntesting|Cool, you\'re testing something!

hmm

Offline

 

#29 2012-10-14 09:13:04

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

Re: A simple learning chatbot in Python

You don't need to  wink
Weird  hmm
Maybe check out these:
http://html.net/tutorials/php/lesson15.php
http://html.net/tutorials/php/lesson16.php


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

Offline

 

#30 2012-10-14 10:06:56

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

Re: A simple learning chatbot in Python

The resource thing is annoying. >.<
Perhaps this would help? Yeah, it's for mySQL problems, but it might be the same method.  smile


yes, yes i do.

Offline

 

#31 2012-10-14 10:18:27

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

Re: A simple learning chatbot in Python

transparent wrote:

The resource thing is annoying. >.<
Perhaps this would help? Yeah, it's for mySQL problems, but it might be the same method.  smile

Thanks, but I'm horrible at that stuff so... I can't figure it out.  tongue

Offline

 

#32 2012-10-14 11:07:27

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

Re: A simple learning chatbot in Python

Actually I figured it out.  tongue

Thanks guys, here: New version! It's a stand alone Python file, just save it as a .py and chat!  big_smile

Code:

''' Thanks to slinger for showing me
how to keep the memory file
on my website! '''

import urllib
print "Please wait..."
known = {}
opener = urllib.FancyURLopener({})
chatmem = opener.open("http://gravi.tation.tk/chat.dat")
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 = {}
            opener = urllib.FancyURLopener({})
            chatmem = opener.open("http://gravi.tation.tk/chat.dat")
            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 = opener.open("http://gravi.tation.tk/chat.php?word=" + temp + "&reply=" + appr)
            chatmem.close()
            known[temp] = appr
            history.append("Unknown reply, user gives \"" + appr + "\" as correct output.")
            print "Thanks, I understand now."

Offline

 

#33 2012-10-14 11:46:03

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: A simple learning chatbot in Python

This is still my favourite chatbot:

Code:

import random
messages = []
while 1:
    messages.append(raw_input("? "))
    print random.choice(messages)

tongue


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#34 2012-10-14 12:04:22

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

Re: A simple learning chatbot in Python

blob8108 wrote:

This is still my favourite chatbot:

Code:

import random
messages = []
while 1:
    messages.append(raw_input("? "))
    print random.choice(messages)

tongue

tongue

I knew you would post here, since you're a Python programmer.  tongue

Offline

 

#35 2012-10-14 12:17:00

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

Re: A simple learning chatbot in Python

Gravitation wrote:

Actually I figured it out.  tongue

Thanks guys, here: New version! It's a stand alone Python file, just save it as a .py and chat!  big_smile

Code:

''' Thanks to slinger for showing me
how to keep the memory file
on my website! '''

import urllib
print "Please wait..."
known = {}
opener = urllib.FancyURLopener({})
chatmem = opener.open("http://gravi.tation.tk/chat.dat")
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 = {}
            opener = urllib.FancyURLopener({})
            chatmem = opener.open("http://gravi.tation.tk/chat.dat")
            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 = opener.open("http://gravi.tation.tk/chat.php?word=" + temp + "&reply=" + appr)
            chatmem.close()
            known[temp] = appr
            history.append("Unknown reply, user gives \"" + appr + "\" as correct output.")
            print "Thanks, I understand now."

Congratulations, I knew you would get it!  big_smile


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

Offline

 

#36 2012-10-14 12:20:48

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

Re: A simple learning chatbot in Python

slinger wrote:

Gravitation wrote:

Actually I figured it out.  tongue

Thanks guys, here: New version! It's a stand alone Python file, just save it as a .py and chat!  big_smile

Code:

''' Thanks to slinger for showing me
how to keep the memory file
on my website! '''

import urllib
print "Please wait..."
known = {}
opener = urllib.FancyURLopener({})
chatmem = opener.open("http://gravi.tation.tk/chat.dat")
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 = {}
            opener = urllib.FancyURLopener({})
            chatmem = opener.open("http://gravi.tation.tk/chat.dat")
            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 = opener.open("http://gravi.tation.tk/chat.php?word=" + temp + "&reply=" + appr)
            chatmem.close()
            known[temp] = appr
            history.append("Unknown reply, user gives \"" + appr + "\" as correct output.")
            print "Thanks, I understand now."

Congratulations, I knew you would get it!  big_smile

:') Thanks. Here's the code:

Code:

<?php
  $openfile = fopen ("chat.dat","r") or die ("");
  $file_size=filesize("chat.dat");
  $file_contents = fread ($openfile,$file_size);
  $writefile = fopen ("chat.dat","w") or die ("");
  fwrite($writefile, $file_contents. "\n". $_GET['word'].'|'. $_GET['reply']);
    ?>

Offline

 

#37 2012-10-14 12:26:53

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

Re: A simple learning chatbot in Python

Somebody cussed on it, I've temporarily taken it down.

Offline

 

#38 2012-10-14 12:49:56

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

Re: A simple learning chatbot in Python

And it's back up.

(sorry for the consecutive posts, I can't edit yet)

Offline

 

#39 2012-10-14 12:59:46

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

Re: A simple learning chatbot in Python

Yeah, that's one thing I thought about. Maybe have passwords and usernames where you can block people from the chat?


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

Offline

 

#40 2012-10-14 13:01:30

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

Re: A simple learning chatbot in Python

slinger wrote:

Yeah, that's one thing I thought about. Maybe have passwords and usernames where you can block people from the chat?

Yeah, but how?  hmm

Offline

 

#41 2012-10-14 13:03:18

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

Re: A simple learning chatbot in Python

Maybe with databases. But that would require a little bit more PHP :p


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

Offline

 

#42 2012-10-14 13:04:52

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

Re: A simple learning chatbot in Python

slinger wrote:

Maybe with databases. But that would require a little bit more PHP :p

I'm scared of databases. ~O~

Offline

 

#43 2012-10-14 13:07:10

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

Re: A simple learning chatbot in Python

They aren't too bad after you get used to them.  wink


yes, yes i do.

Offline

 

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

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

Re: A simple learning chatbot in Python

transparent wrote:

They aren't too bad after you get used to them.  wink

That's so with everything, right?

Offline

 

#45 2012-10-14 13:10:04

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

Re: A simple learning chatbot in Python

Too true :p


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

Offline

 

#46 2012-10-14 13:13:40

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

Re: A simple learning chatbot in Python

Gravitation wrote:

transparent wrote:

They aren't too bad after you get used to them.  wink

That's so with everything, right?

Very true.  smile


yes, yes i do.

Offline

 

#47 2012-10-14 13:14:07

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

Re: A simple learning chatbot in Python

Gravitation wrote:

And it's back up.

(sorry for the consecutive posts, I can't edit yet)

Excuse me for being stupid, but where is your website?


"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

 

#48 2012-10-14 13:15:29

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

Re: A simple learning chatbot in Python

Molybdenum wrote:

Gravitation wrote:

And it's back up.

(sorry for the consecutive posts, I can't edit yet)

Excuse me for being stupid, but where is your website?

Gravi.tation.tk

The files are chat.php and chat.dat.

Offline

 

#49 2012-10-14 13:17:52

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

Re: A simple learning chatbot in Python

Wow, you're website rocks  yikes


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

Offline

 

#50 2012-10-14 13:20:04

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

Re: A simple learning chatbot in Python

slinger wrote:

Wow, you're website rocks  yikes

Thanks! I've made better, though.

Offline

 

Board footer