I made a simple little learning chatbot in Python. Check it out:
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.
There are 2 commands:
show known
Prints all it's known replies.
show history
Prints chat history.
Offline
This makes me want to learn Python even more. :X
Offline
transparent wrote:
This makes me want to learn Python even more. :X
http://www.codecademy.com/tracks/python
Offline
slinger wrote:
Nice work!
Thanks! c: I'll make a more complex one when I have more knowledge of Python.
Offline
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.
Offline
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.
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! >
Offline
Works awesome. But not as good as my Scratch chatbot Julia
Offline
jji7skyline wrote:
Works awesome. But not as good as my Scratch chatbot Julia
Thanks. I might reprogram that in Python...
Offline
That wouldn't be too hard, and with Python's extra features, it could become a really nice chatbot o:
Offline
Offline
Yeah, I know. This one's a simple chatbot.
Offline
Nice. This is great for a start. The explaining is kind of annoying though.
Offline
nickbrickmaster wrote:
Nice. This is great for a start. The explaining is kind of annoying though.
Thanks. What explaining are you talking about, exactly?
Offline
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
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?
Sorry. The "Oops, I don't know what that means."
Offline
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?
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
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.
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
Forgive my n00bness.
Offline
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 ) or you can submit data via a url and let Python and PHP work hand in hand
Offline
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 ) or you can submit data via a url and let Python and PHP work hand in hand
PHP, thought so. Problem is I don't know PHP. I've only been doing this for a week.
I'll check it out. Thanks!
Offline
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 ):
<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
Anyway, the URL should look like this "gravi.tation.php?word=Hello&reply=Hey,%20what's%20up?"
Offline
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 ):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
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