Code Monkey home page Code Monkey logo

Comments (8)

gunthercox avatar gunthercox commented on May 22, 2024 1

Here is an alternative method you can try:

def remove_hyphens(statement):
    # ...

bot = ChatBot('Test',
    # ...
)

bot.preprocessors.append(
    remove_hyphens
)

trainer = ListTrainer(bot)
# ...

By doing this, you can retroactively add your custom preprocessor to the chat bot.

from chatterbot-corpus.

gunthercox avatar gunthercox commented on May 22, 2024

ChatterBot's training process respects the preprocessors that are configured for the chatbot being trained. Because of this you could create a preprocessor that removes hyphens.

https://chatterbot.readthedocs.io/en/stable/preprocessors.html

def remove_hyphens(statement):
    """
    Remove hypnens.
    """
    statement.text = statement.text.replace('-', '')

    return statement


chatbot = ChatBot(
    'Example,
    preprocessors=[
        remove_hyphens
    ]
)

from chatterbot-corpus.

bufferbox avatar bufferbox commented on May 22, 2024

This is the error I'm getting after trying that:
AttributeError: NameError: name 'remove_hyphens' is not defined

Here is my code:

from chatterbot.trainers import ListTrainer
from chatterbot import ChatBot
from chatterbot.conversation import Statement
import os

bot = ChatBot('Test',
      storage_adapter='chatterbot.storage.SQLStorageAdapter',
      database_uri='sqlite:///database.sqlite3',
      logic_adapters=[
                      'chatterbot.logic.MathematicalEvaluation',
                      {
                        'import_path': 'chatterbot.logic.BestMatch',
                        'default_response': 'I am sorry, but I do not understand.',
                        'maximum_similarity_threshold': 0.90
                      }
                    ],
      preprocessors=[
                     remove_hyphens
                     ],
      trainer='chatterbot.trainers.ListTrainer')

trainer = ListTrainer(bot)

for _file in os.listdir('files'):
    chats = open('files/' + _file, 'r').readlines()
    trainer.train(chats)

def remove_hyphens(statement):
    """
    Remove hypnens.
    """
    statement.text = statement.text.replace('-', '')
    return statement

while True:
    try:
        message = input('You:')
        if message.strip() == 'Bye':
            print('ChatBot: Bye')
            break
        else:
            reply = bot.get_response(message)
            if reply.confidence == 0:
                print("I don't understand")
            else:
                print('ChatBot:', reply)

    # Press ctrl-c or ctrl-d on the keyboard to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

from chatterbot-corpus.

gunthercox avatar gunthercox commented on May 22, 2024

You are seeing that error because you need to define the remove_hyphens function before you reference it in the ChatBot instance. (Just move the def remove_hyphens before the bot = line.)

from chatterbot-corpus.

bufferbox avatar bufferbox commented on May 22, 2024

Yes, you're right.
However, I am getting this error now: [AttributeError: 'function' object has no attribute 'split']

Traceback (most recent call last):
File "C:\Users\wasai\AppData\Local\Programs\Python\Python37-32\practise\newtrial\improved main\improvedMain.py", line 30, in
trainer='chatterbot.trainers.ListTrainer')
File "C:\Users\wasai\AppData\Local\Programs\Python\Python37-32\lib\site-packages\chatterbot\chatterbot.py", line 50, in init
self.preprocessors.append(utils.import_module(preprocessor))
File "C:\Users\wasai\AppData\Local\Programs\Python\Python37-32\lib\site-packages\chatterbot\utils.py", line 14, in import_module
module_parts = dotted_path.split('.')
AttributeError: 'function' object has no attribute 'split'

from chatterbot-corpus.

gunthercox avatar gunthercox commented on May 22, 2024

You might need to move the remove_hyphens function into it's own python file. Once you make that change, you should be able to modify the corresponding value in the preprocessors list to be a string representing a relative import.

from chatterbot-corpus.

bufferbox avatar bufferbox commented on May 22, 2024

I tried that and now I am getting a different kind of error. Hope you can help me.

from chatterbot.trainers import ListTrainer
from chatterbot import ChatBot
import os

bot = ChatBot('Test',
      storage_adapter='chatterbot.storage.SQLStorageAdapter',
      database_uri='sqlite:///database.sqlite3',
      logic_adapters=[
                      {
                        'import_path': 'chatterbot.logic.BestMatch',
                        'default_response': 'I am sorry, but I do not understand.',
                        'maximum_similarity_threshold': 0.95
                      }
                    ],
      preprocessors= [{'import_path':'chatterbot.preprocessors.remove_hyphens'}],
      trainer='chatterbot.trainers.ListTrainer')

trainer = ListTrainer(bot)

for _file in os.listdir('files'):
    chats = open('files/' + _file, 'r').readlines()
    trainer.train(chats)

while True:
    try:

        message = input('You:')

        if message.strip() == 'Bye':
            print('ChatBot: Bye')
            break
        else:
            reply = bot.get_response(message)
            if reply.confidence == 0:
                print("Chatbot: I don't understand")
            else:
                print('ChatBot:', reply)

    # Press ctrl-c or ctrl-d on the keyboard to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

Error:

Traceback (most recent call last):
  File "C:\Users\wasai\AppData\Local\Programs\Python\Python37-32\practise\newtrial\improved main\improvedMain.py", line 19, in <module>
    trainer='chatterbot.trainers.ListTrainer')
  File "C:\Users\wasai\AppData\Local\Programs\Python\Python37-32\lib\site-packages\chatterbot\chatterbot.py", line 50, in __init__
    self.preprocessors.append(utils.import_module(preprocessor))
  File "C:\Users\wasai\AppData\Local\Programs\Python\Python37-32\lib\site-packages\chatterbot\utils.py", line 14, in import_module
    module_parts = dotted_path.split('.')
AttributeError: 'dict' object has no attribute 'split'
>>> 

preprocessors.py

def remove_hyphens(statement):
    """
    Remove hyphens.
    """
    statement.text = statement.text.replace('-', '')
    return statement

from chatterbot-corpus.

bufferbox avatar bufferbox commented on May 22, 2024

Thank you! It works now :)

from chatterbot-corpus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.