Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More
Max. 2000 characters
Replies

usr_profile.png

User

( 4 months ago )

 

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"))
            print("Save file loaded!")
        else:
            print("No save file found.")
        print()
        for key, value in self.known.items():
            self.wordcount += 1
    def question(self, x):
        self.wordcount += 1
        a = "w" + str(self.wordcount)
        d = {"name": x, "resp": [x], "uses": 0}
        self.known[a] = d
    def talk(self):
        talking = True
        prevres = ""
        while talking:
            if self.autosave:
                self.sescount += 1
                if self.sescount >= self.autocount:
                    self.sescount = 0
                    pickle.dump(self.known, open('known.data', 'wb'
usr_profile.png

User

( 4 months ago )

  1. 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.
    1. One blank line between the functions in bot.
    2. Some more blank lines in between blocks of code in the module-level, and in any function in the bot class.
  2. Secondly, the two variables near the top of the file, startmes, and helpmes, should be in one docstring, at the very top of the file, above the import statements.
  3. Using os.system("cls"), or os.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").
  4. 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.
  5. Most of your naming is okay. You do have some odd names like maxwords, or wordcount. Preferably, variables with multiple words in their names, like these, should be named like this: max_wordsword_count. In terms of how names are styled, functions and variables should be snake_case, and classes should be PascalCase. If the variable is constant it should be UPPERCASE_SNAKE_CASE.
  6. You don't need to include two parentheses () after class bot. By default, you can write a class declaration like this, class MyClass:, and by default, it will inherit from object. Only use the parentheses if the class is inherited from another class, like this. class Enemy(Character):.
  7. 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("'", "")
  8. 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.

what's your interest