Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of django_storage #705

Merged
merged 2 commits into from
May 1, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion chatterbot/ext/django_chatterbot/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
'storage_adapter': 'chatterbot.storage.DjangoStorageAdapter',
'input_adapter': 'chatterbot.input.VariableInputTypeAdapter',
'output_adapter': 'chatterbot.output.OutputAdapter',
'use_django_models': True
'use_django_models': True,
'django_app_name': 'django_chatterbot'
}

CHATTERBOT = CHATTERBOT_DEFAULTS.copy()
Expand Down
36 changes: 25 additions & 11 deletions chatterbot/storage/django_storage.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import json
from chatterbot.storage import StorageAdapter


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should not have been deleted. Python's style guide specifies that there should be two blank lines between imports and the a class definition. Once this is fixed I will merge this pull request.

Copy link
Collaborator Author

@vkosuri vkosuri May 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Master!. I'll made my changes through PyCharm. I found other issues with Train python manage.py train with import_module option isn't working train command. it was failing always. The workaround I have made like this

I tried figured out bug, i couldn't, could you please let me know why it failin and how to solve this problem?

CHATTERBOT_TRAIN = {
    'name': 'Test Chatter Bot',
    'trainer': 'chatterbot.trainers.ChatterBotCorpusTrainer',
    'training_data': [
        './data/greetings.corpus.json'
    ],
    'storage_adapter': 'chatterbot.storage.DjangoStorageAdapter',
    'django_app_name': 'howdy',
    'use_django_models': True
}
# ChatterBot settings
CHATTERBOT = {
    'name': 'Test Chatter Bot',
    'logic_adapters': [
        {
            'import_path': 'chatterbot.logic.BestMatch',
        },
        {
            'import_path': 'chatterbot.logic.LowConfidenceAdapter',
            'threshold': 0.75,
            'default_response': 'I am sorry, but I do not understand.'
        },
        {
            'import_path': 'chatterbot.logic.SpecificResponseAdapter',
            'input_text': 'Help me!',
            'output_text': 'Ok, here is a link: http://chatterbot.readthedocs.io/en/stable/'
        }
    ],
    'filters': [
            'chatterbot.filters.RepetitiveResponseFilter'
    ],
    'storage_adapter': 'chatterbot.storage.DjangoStorageAdapter',
    'django_app_name': 'howdy',
    'use_django_models': True
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also the response and statements are still reversed in django_storage adapter

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the import_module, feel free to open a new issue for it.

class DjangoStorageAdapter(StorageAdapter):
"""
Storage adapter that allows ChatterBot to interact with
Expand All @@ -12,13 +10,16 @@ def __init__(self, **kwargs):
super(DjangoStorageAdapter, self).__init__(**kwargs)

self.adapter_supports_queries = False
self.django_app_name = kwargs.get('django_app_name', 'django_chatterbot')

def count(self):
from chatterbot.ext.django_chatterbot.models import Statement
from django.apps import apps
Statement = apps.get_model(self.django_app_name, 'Statement')
return Statement.objects.count()

def find(self, statement_text):
from chatterbot.ext.django_chatterbot.models import Statement
from django.apps import apps
Statement = apps.get_model(self.django_app_name, 'Statement')
try:
return Statement.objects.get(text=statement_text)
except Statement.DoesNotExist as e:
Expand All @@ -30,7 +31,8 @@ def filter(self, **kwargs):
Returns a list of statements in the database
that match the parameters specified.
"""
from chatterbot.ext.django_chatterbot.models import Statement
from django.apps import apps
Statement = apps.get_model(self.django_app_name, 'Statement')
from django.db.models import Q

order = kwargs.pop('order_by', None)
Expand Down Expand Up @@ -77,7 +79,8 @@ def update(self, statement):
"""
Update the provided statement.
"""
from chatterbot.ext.django_chatterbot.models import Statement
from django.apps import apps
Statement = apps.get_model(self.django_app_name, 'Statement')

response_statement_cache = statement.response_statement_cache

Expand Down Expand Up @@ -108,7 +111,8 @@ def get_random(self):
"""
Returns a random statement from the database
"""
from chatterbot.ext.django_chatterbot.models import Statement
from django.apps import apps
Statement = apps.get_model(self.django_app_name, 'Statement')
return Statement.objects.order_by('?').first()

def remove(self, statement_text):
Expand All @@ -117,9 +121,12 @@ def remove(self, statement_text):
Removes any responses from statements if the response text matches the
input text.
"""
from chatterbot.ext.django_chatterbot.models import Statement
from chatterbot.ext.django_chatterbot.models import Response
from django.apps import apps
from django.db.models import Q

Statement = apps.get_model(self.django_app_name, 'Statement')
Response = apps.get_model(self.django_app_name, 'Response')

statements = Statement.objects.filter(text=statement_text)

responses = Response.objects.filter(
Expand All @@ -133,7 +140,11 @@ def drop(self):
"""
Remove all data from the database.
"""
from chatterbot.ext.django_chatterbot.models import Statement, Response, Conversation
from django.apps import apps

Statement = apps.get_model(self.django_app_name, 'Statement')
Response = apps.get_model(self.django_app_name, 'Response')
Conversation = apps.get_model(self.django_app_name, 'Conversation')

Statement.objects.all().delete()
Response.objects.all().delete()
Expand All @@ -146,8 +157,11 @@ def get_response_statements(self):
in_response_to field. Otherwise, the logic adapter may find a closest
matching statement that does not have a known response.
"""
from chatterbot.ext.django_chatterbot.models import Statement, Response
from django.apps import apps
Statement = apps.get_model(self.django_app_name, 'Statement')
Response = apps.get_model(self.django_app_name, 'Response')

responses = Response.objects.all()

return Statement.objects.filter(in_response__in=responses)

9 changes: 6 additions & 3 deletions chatterbot/storage/storage_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ def Statement(self):

if 'DJANGO_SETTINGS_MODULE' in os.environ:
django_project = __import__(os.environ['DJANGO_SETTINGS_MODULE'])
if django_project.settings.CHATTERBOT['use_django_models'] is True:
from chatterbot.ext.django_chatterbot.models import Statement
return Statement
if 'use_django_models' in django_project.settings.CHATTERBOT:
if django_project.settings.CHATTERBOT['use_django_models'] is True:
from django.apps import apps
Statement = apps.get_model(django_project.settings.CHATTERBOT['django_app_name'], 'Statement')
return Statement

from chatterbot.conversation.statement import Statement
statement = Statement
Expand Down Expand Up @@ -145,3 +147,4 @@ class AdapterMethodNotImplementedError(NotImplementedError):
Typically this indicates that the method should be implement in a subclass.
"""
pass

3 changes: 2 additions & 1 deletion examples/django_app/example_app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
'trainer': 'chatterbot.trainers.ChatterBotCorpusTrainer',
'training_data': [
'chatterbot.corpus.english.greetings'
]
],
'django_app_name': 'django_chatterbot'
}

MIDDLEWARE_CLASSES = (
Expand Down