-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Django management command to train chat bot
- Loading branch information
1 parent
bf1fd63
commit cc5d40e
Showing
11 changed files
with
100 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
23 changes: 23 additions & 0 deletions
23
chatterbot/ext/django_chatterbot/management/commands/train.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Trains the database used by the chat bot' | ||
can_import_settings = True | ||
|
||
def handle(self, *args, **options): | ||
from chatterbot import ChatBot | ||
from chatterbot.ext.django_chatterbot import settings | ||
|
||
chatterbot = ChatBot(**settings.CHATTERBOT) | ||
|
||
chatterbot.train(chatterbot.training_data) | ||
|
||
# Django 1.8 does not define SUCCESS | ||
if hasattr(self.style, 'SUCCESS'): | ||
style = self.style.SUCCESS | ||
else: | ||
style = self.style.NOTICE | ||
|
||
training_class = chatterbot.trainer.__class__.__name__ | ||
self.stdout.write(style('ChatterBot trained using "%s"' % training_class)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ Django applications. | |
:maxdepth: 2 | ||
|
||
settings | ||
training | ||
views | ||
|
||
Installation | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
======== | ||
Training | ||
======== | ||
|
||
Management command | ||
================== | ||
|
||
When using ChatterBot with Django, the training process can be | ||
executed by running the training management command. | ||
|
||
.. code-block:: bash | ||
python manage.py train | ||
Training settings | ||
================= | ||
|
||
You can specify any data that you want to be passed to the chat bot | ||
trainer in the :code:`training_data` parameter in your :code:`CHATTERBOT` | ||
Django settings. | ||
|
||
.. code-block:: python | ||
CHATTERBOT = { | ||
# ... | ||
'trainer': 'chatterbot.trainers.ChatterBotCorpusTrainer', | ||
'training_data': [ | ||
'chatterbot.corpus.english.greetings' | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.core.management import call_command | ||
from django.test import TestCase | ||
from django.utils.six import StringIO | ||
from chatterbot.ext.django_chatterbot.models import Statement | ||
|
||
|
||
class TrainCommandTestCase(TestCase): | ||
|
||
def test_command_output(self): | ||
out = StringIO() | ||
call_command('train', stdout=out) | ||
self.assertIn('ChatterBot trained', out.getvalue()) | ||
|
||
def test_command_data_argument(self): | ||
out = StringIO() | ||
statements_before = Statement.objects.exists() | ||
call_command('train', stdout=out) | ||
statements_after = Statement.objects.exists() | ||
|
||
self.assertFalse(statements_before) | ||
self.assertTrue(statements_after) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters