patternpythonModerate
Simple chat bot
Viewed 0 times
simplechatbot
Problem
I made a chat bot, that, as you talk to it, it learns to respond. But the way it speaks is strange, so if you have any ideas on how to make its response any more human, then please say so.
Anyway, you have to start a new chat bot. You'll notice that when you start his responses will be incredibly stupid. Once you talk to him enough, he gets more human, but not by much.
I am not asking for you to review features in the code, I am asking for the code and overall style to be reviewed.
```
import random, pickle, os
import os.path
startmes = """Machine Learning Conversational Program by Jake Speiran, 2015. ver 1.0
Hello! In a moment you will begin chatting with Machine Learning Conversational
Program, or MLCP. Everything you say he will learn, and every response you make
he will remember. The goal is that he will someday be able to talk. Type
"#help" to learn more. To quit the program, type "#quit" into the command
prompt.
"""
helpmes = """This is the help message for MLCP.
In order to communicate with the bot, simply type what you want to say into the
input space. When typing please use only lower case characters and no special
characters.
So this:
"You're a real star!"
Becomes this:
"youre a real star"
The reason for this is that otherwise you would have many entries that are
copies of the same word, ie Hey, hey, hey! and Hey all mean the same thing
but would be entered differently.
Sometimes what the bot says can be hard to interpret, but keep trying and
use your imagination.
"""
class bot():
def __init__(self, autosave, deldups, autocount, maxwords, maxresp):
self.autosave = autosave
self.autocount = autocount
self.deldups = deldups
self.maxwords = maxwords
self.maxresp = maxresp
self.known = {}
self.wordcount = 0
self.sescount = 0
os.system("cls")
print(startmes)
if os.path.isfile("known.data"):
self.known = pickle.load(open('known.data', "rb"))
Anyway, you have to start a new chat bot. You'll notice that when you start his responses will be incredibly stupid. Once you talk to him enough, he gets more human, but not by much.
I am not asking for you to review features in the code, I am asking for the code and overall style to be reviewed.
```
import random, pickle, os
import os.path
startmes = """Machine Learning Conversational Program by Jake Speiran, 2015. ver 1.0
Hello! In a moment you will begin chatting with Machine Learning Conversational
Program, or MLCP. Everything you say he will learn, and every response you make
he will remember. The goal is that he will someday be able to talk. Type
"#help" to learn more. To quit the program, type "#quit" into the command
prompt.
"""
helpmes = """This is the help message for MLCP.
In order to communicate with the bot, simply type what you want to say into the
input space. When typing please use only lower case characters and no special
characters.
So this:
"You're a real star!"
Becomes this:
"youre a real star"
The reason for this is that otherwise you would have many entries that are
copies of the same word, ie Hey, hey, hey! and Hey all mean the same thing
but would be entered differently.
Sometimes what the bot says can be hard to interpret, but keep trying and
use your imagination.
"""
class bot():
def __init__(self, autosave, deldups, autocount, maxwords, maxresp):
self.autosave = autosave
self.autocount = autocount
self.deldups = deldups
self.maxwords = maxwords
self.maxresp = maxresp
self.known = {}
self.wordcount = 0
self.sescount = 0
os.system("cls")
print(startmes)
if os.path.isfile("known.data"):
self.known = pickle.load(open('known.data', "rb"))
Solution
- Whitespace. It looks nice, and is important. Use it. There are many areas here where you could insert some whitespace, and then the code will magically become much easier to read. Here are a few areas where whitespace is needed.
- One blank line between the functions in
bot.
- Some more blank lines in between blocks of code in the module-level, and in any function in the
botclass.
- Secondly, the two variables near the top of the file,
startmes, andhelpmes, should be in one docstring, at the very top of the file, above the import statements.
- Using
os.system("cls"), oros.system("clear")is not very portable, or cross-platform. At the moment, the most portable/cross-platform way to clear the screen would be this:os.system("cls" if os.name == "nt" else "clear").
- Add some docstrings to these functions. Preferably, these docstrings should also be fleshed out with useful information on what these functions do, and how they do it.
- Most of your naming is okay. You do have some odd names like
maxwords, orwordcount. Preferably, variables with multiple words in their names, like these, should be named like this:max_words,word_count. In terms of how names are styled, functions and variables should besnake_case, and classes should bePascalCase. If the variable is constant it should beUPPERCASE_SNAKE_CASE.
- You don't need to include two parentheses
()afterclass bot. By default, you can write a class declaration like this,class MyClass:, and by default, it will inherit fromobject. Only use the parentheses if the class is inherited from another class, like this.class Enemy(Character):.
-
You mention in the
helpmes variable, that the user shouldn't enter punctuation of any kind. This is sort of a hacky way to get user input, so I'd recommend using str.replace(item_to_replace, replace_with) to remove punctuation. Here's an example that removes periods, commas, and apostrophes.user_input = raw_input("> ")
.replace(".", "")
.replace(",", "")
.replace("'", "")-
Finally if you ever implement a more advanced command system than just
#help or #quit, I'd recommend using a dictionary, rather than chaining if/elif/else statements.Code Snippets
user_input = raw_input("> ")
.replace(".", "")
.replace(",", "")
.replace("'", "")Context
StackExchange Code Review Q#94232, answer score: 14
Revisions (0)
No revisions yet.