Simple chat bot

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years 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'
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years 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.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.