Skip to content

mjafarpour87/forough

Repository files navigation

forough

Forough is an interface to interact with NLP-based chat bot. Forough (Persian: فروغ ) is a Persian feminine given name meaning brightness. It is also the name of influential Iranian poet (Forough Farrokhzad). This project is named forough after the Iranian poet symbolizing the ability of the chatbot to process natural language.

Project Overview

The ChatterBot library combines language corpora, text processing, machine learning algorithms, and data storage and retrieval to allow you to build flexible chatbots. You can build an industry-specific chatbot by training it with relevant data. Additionally, the chatbot will remember user responses and continue building its internal graph structure to improve the responses that it can give.

Attention

While ChatterBot is still a popular open source solution for building a chatbot in Python, it hasn’t been actively maintained for a while and has therefore accumulated a significant number of issues.

There are multiple forks of the project that implement fixes and updates to the existing codebase, but you’ll have to personally pick the fork that implements the solution you’re looking for and then install it directly from GitHub. A fork might also come with additional installation instructions.

To get started, however, you won’t use a fork. Instead, you’ll use a specific pinned version of the library, as distributed on PyPI. You’ll find more information about installing ChatterBot in step one.

Prerequsites

Before you get started, make sure that you have a Python version available that works for this ChatterBot project. What version of Python you need depends on your operating system:

Windows

You need to use a Python version below 3.8 to successfully work with the recommended version of ChatterBot in this tutorial. You can install Python 3.7.9 using pyenv-win.

Linux

You should be able to run the project on Ubuntu Linux with a variety of Python versions. However, if you bump into any issues, then you can try to install Python 3.7.9, for example using pyenv.

Mac OS

You can run the project with a variety of Python versions. The chatbot was built and tested with Python 3.10.7 but should also run with older Python versions.

If you’ve installed the right Python version for your operating system, then you’re ready to get started.

Step 1: Create a Chatbot Using Python ChatterBot

In this step, you’ll set up a virtual environment and install the necessary dependencies. You’ll also create a working command-line chatbot that can reply to you—but it won’t have very interesting replies for you yet.

To get started with your chatbot project, create and activate a virtual environment, then install chatterbot and pytz:

Windows

PS> python -m venv venv
PS> venv\Scripts\activate
(venv) PS> python -m pip install chatterbot==1.0.4 pytz

Linux & Mac OS

$ python -m venv venv
$ source venv/bin/activate
(venv) $ python -m pip install chatterbot==1.0.4 pytz

Running these commands in your terminal application installs ChatterBot and its dependencies into a new Python virtual environment. After the installation is complete, running python -m pip freeze should bring up list of installed dependencies that’s similar to what you can find in the provided code’s requirements.txt file.

With the installation out of the way, and ignoring some of the issues that the library currently has, you’re ready to get started! Create a new Python file, call it bot.py, and add the code that you need to get a basic chatbot up and running:

# bot.py
from chatterbot import ChatBot
chatbot = ChatBot("Chatpot")
exit_conditions = (":q", "quit", "exit")
while True:
   query = input("> ")
   if query in exit_conditions:
       break
   else:
       print(f"🪴 {chatbot.get_response(query)}")

After importing ChatBot in line 3, you create an instance of ChatBot in line 5. The only required argument is a name, and you call this one "Chatpot". No, that’s not a typo—you’ll actually build a chatty flowerpot chatbot in this tutorial! You’ll soon notice that pots may not be the best conversation partners after all. In line 8, you create a while loop that’ll keep looping unless you enter one of the exit conditions defined in line 7. Finally, in line 13, you call .get_response() on the ChatBot instance that you created earlier and pass it the user input that you collected in line 9 and assigned to query. When you run this script, ChatterBot might download some data and language models associated with the NLTK project. It’ll print some information about that to your console. Python won’t download this data again during subsequent runs.

Note

The NLTK project installs the data that ChatterBot uses into a default location on your operating system:

    Windows: C:\nltk_data\
    Linux: /usr/share/nltk_data/
    macOS: /Users/<username>/nltk_data/

NLTK will automatically create the directory during the first run of your chatbot. If you’re ready to communicate with your freshly homegrown Chatbot, then you can go ahead and run the Python file:

$ python bot.py

fter the language models are set up, you’ll see the greater than sign (>) that you defined in bot.py as your input prompt. You can now start to interact with your chat bot. Even if your chat bot doesn’t have much to say yet, it’s already learning and growing. To test this out, stop the current session. You can do this by typing one of the exit conditions—":q", "quit", or "exit". Then start the chatbot another time. Enter a different message, and you’ll notice that the chatbot remembers what you typed during the previous run.

During the first run, ChatterBot created a SQLite database file where it stored all your inputs and connected them with possible responses. There should be three new files that have popped up in your working directory:

./
├── bot.py
├── db.sqlite3
├── db.sqlite3-shm
└── db.sqlite3-wal

ChatterBot uses the default SQLStorageAdapter and creates a SQLite file database unless you specify a different storage adapter.

Note

The main database file is db.sqlite3, while the other two, ending with -wal and -shm, are temporary support files.

Because you said both hello and hi at the beginning of the chat, your chat-pot learned that it can use these messages interchangeably. That means if you chat a lot with your new chatbot, it’ll gradually have better replies for you. But improving its responses manually sounds like a long process!

Now that you’ve created a working command-line chatbot, you’ll learn how to train it so you can have slightly more interesting conversations.

Step 2: Begin Training Your Chatbot

In the previous step, you built a chatbot that you could interact with from your command line. The chatbot started from a clean slate and wasn’t very interesting to talk to.

In this step, you’ll train your chatbot using ListTrainer to make it a little smarter from the start. You’ll also learn about built-in trainers that come with ChatterBot, including their limitations.

Your chatbot doesn’t have to start from scratch, and ChatterBot provides you with a quick way to train your bot. You’ll use ChatterBot’s ListTrainer to provide some conversation samples that’ll give your chatbot more room to grow:

# bot.py
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
chatbot = ChatBot("Chatpot")
trainer = ListTrainer(chatbot)
trainer.train([
    "Hi",
    "Welcome, friend 🤗",
])
trainer.train([
    "Are you a plant?",
    "No, I'm the pot below the plant!",
])
exit_conditions = (":q", "quit", "exit")
while True:
    query = input("> ")
    if query in exit_conditions:
        break
    else:
        print(f"🪴 {chatbot.get_response(query)}")

In line 4, you import ListTrainer, to which you pass your chatbot on line 8 to create trainer.

In lines 9 to 12, you set up the first training round, where you pass a list of two strings to trainer.train(). Using .train() injects entries into your database to build upon the graph structure that ChatterBot uses to choose possible replies.

Note

If you pass an iterable with exactly two items to ListTrainer.train(), then ChatterBot considers the first item a statement and the second item an acceptable response.

You can run more than one training session, so in lines 13 to 16, you add another statement and another reply to your chatbot’s database.

If you now run the interactive chatbot once again using python bot.py, you can elicit somewhat different responses from it than before.

The conversation isn’t yet fluent enough that you’d like to go on a second date, but there’s additional context that you didn’t have before! When you train your chatbot with more data, it’ll get better at responding to user inputs.

The ChatterBot library comes with some corpora that you can use to train your chatbot. However, at the time of writing, there are some issues if you try to use these resources straight out of the box.

Note

The issues come from mismatches between versions of the dependencies, as well as the Python version that you use. You can work around them, but it’ll require some fiddling on your end.

Alternatively, you could parse the corpus files yourself using pyYAML because they’re stored as YAML files.

While the provided corpora might be enough for you, in this tutorial you’ll skip them entirely and instead learn how to adapt your own conversational input data for training with ChatterBot’s ListTrainer.

To train your chatbot to respond to industry-relevant questions, you’ll probably need to work with custom data, for example from existing support requests or chat logs from your company.

Step 3: Create a shell on Chatbot training Engine

ChatterBot has direct support for integration with Django’s ORM. It is relatively easy to use ChatterBot within your Django application to create conversational pages and endpoints.

Django Integration

Install packages

Begin by making sure that you have installed both django and chatterbot.

pip install django chatterbot

For more details on installing Django, see the Django documentation.

Installed Apps

Add chatterbot.ext.django_chatterbot to your INSTALLED_APPS in the settings.py file of your Django project.

INSTALLED_APPS = (
    # ...
    'chatterbot.ext.django_chatterbot',
)

Migrations

You can run the Django database migrations for your chat bot with the following command.

python manage.py migrate django_chatterbot

Chatterbot Django Settings

You can edit the ChatterBot configuration through your Django settings.py file.

CHATTERBOT = {
    'name': 'Tech Support Bot',
    'logic_adapters': [
        'chatterbot.logic.MathematicalEvaluation',
        'chatterbot.logic.TimeLogicAdapter',
        'chatterbot.logic.BestMatch'
    ]
}

Any setting that gets set in the CHATTERBOT dictionary will be passed to the chat bot that powers your django app.

Additional Django settings

django_app_name [default: ‘django_chatterbot’] The Django app name to look up the models from.

Step 4: Installation Guide

create a virtual environment

PS> python -m venv venv

Activate the virtual environment

PS> .\venv\Scripts\activate

install library

(venv) PS> python -m pip install r requirements.txt

run server

python .\forough\manage.py runserver 127.0.0.1:8001

Conclusion

Next Steps

References

  1. [ChatterBot: Build a Chatbot With Python](https://realpython.com/build-a-chatbot-python-chatterbot/#project-overview)
  2. [ChatterBot Django Integration](https://chatterbot.readthedocs.io/en/stable/django/index.html)

Releases

No releases published

Packages

No packages published

Languages