Still learning python on the side, although I have been informed that there are a couple other languages that are more proficient for making chatbots (like OpenNLP or NLTK). Recently, I nabbed some code from this here blog to test for myself. After a couple attempts of trying to run it, I found that the indentation was slightly wrong. Once it was fixed, it worked very well.
There are actually two programs. The first one, a "trainer," takes a text file (.txt), and spends a long time compiling a word tree. It looks at each word and sees what words come after that word. It then does the same for the words that come after that word, and so on and so forth. It puts this word tree into a "lexicon" file. The actual "naive" chatbot program (meaning that it doesn't really respond to the user logically) uses this lexicon file to create responses. The user types in a sentence, the program chooses a random word that the user types, and does the same tree building, taking random branches until it reaches a punctuation mark. The result is a sort of gobble-d-gook of the text's author, but occasionally a line is produced that makes total sense (though not really in context). I personally like "pronounced it not to such answer as the wonted way to his bulk And I was the fatness of twelve."
The writer of the original code used the complete text of War & Peace by Tolstoy (which would take hours in the trainer program). I made do with Hamlet (which, in my opinion, produced much funnier responses).
The writer of the original code used the complete text of War & Peace by Tolstoy (which would take hours in the trainer program). I made do with Hamlet (which, in my opinion, produced much funnier responses).