Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'chatterbot.ext.django_chatterbot',
'tests_django',
]
CHATTERBOT = {
'name': 'Test Django ChatterBot',
'logic_adapters': [
{
'import_path': 'chatterbot.logic.BestMatch',
},
{
'import_path': 'chatterbot.logic.MathematicalEvaluation',
}
],
'storage_adapter': 'chatterbot.storage.DjangoStorageAdapter',
'django_app_name': constants.DEFAULT_DJANGO_APP_NAME,
'initialize': False
}
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
tag_association_table = Table(
'tag_association',
Base.metadata,
Column('tag_id', Integer, ForeignKey('tag.id')),
Column('statement_id', Integer, ForeignKey('statement.id'))
)
class Tag(Base):
"""
A tag that describes a statement.
"""
name = Column(
String(constants.TAG_NAME_MAX_LENGTH),
unique=True
)
class Statement(Base, StatementMixin):
"""
A Statement represents a sentence or phrase.
"""
confidence = 0
text = Column(
String(constants.STATEMENT_TEXT_MAX_LENGTH)
)
search_text = Column(
"""
confidence = 0
text = Column(
String(constants.STATEMENT_TEXT_MAX_LENGTH)
)
search_text = Column(
String(constants.STATEMENT_TEXT_MAX_LENGTH),
nullable=False,
server_default=''
)
conversation = Column(
String(constants.CONVERSATION_LABEL_MAX_LENGTH),
nullable=False,
server_default=''
)
created_at = Column(
DateTime(timezone=True),
server_default=func.now()
)
tags = relationship(
'Tag',
secondary=lambda: tag_association_table,
backref='statements'
)
in_response_to = Column(
backref='statements'
)
in_response_to = Column(
String(constants.STATEMENT_TEXT_MAX_LENGTH),
nullable=True
)
search_in_response_to = Column(
String(constants.STATEMENT_TEXT_MAX_LENGTH),
nullable=False,
server_default=''
)
persona = Column(
String(constants.PERSONA_MAX_LENGTH),
nullable=False,
server_default=''
)
def get_tags(self):
"""
Return a list of tags for this statement.
"""
return [tag.name for tag in self.tags]
def add_tags(self, *tags):
"""
Add a list of strings to the statement as tags.
"""
self.tags.extend([
Tag(name=tag) for tag in tags
name = Column(
String(constants.TAG_NAME_MAX_LENGTH),
unique=True
)
class Statement(Base, StatementMixin):
"""
A Statement represents a sentence or phrase.
"""
confidence = 0
text = Column(
String(constants.STATEMENT_TEXT_MAX_LENGTH)
)
search_text = Column(
String(constants.STATEMENT_TEXT_MAX_LENGTH),
nullable=False,
server_default=''
)
conversation = Column(
String(constants.CONVERSATION_LABEL_MAX_LENGTH),
nullable=False,
server_default=''
)
created_at = Column(
DateTime(timezone=True),
from chatterbot.conversation import StatementMixin
from chatterbot import constants
from django.db import models
from django.utils import timezone
from django.conf import settings
DJANGO_APP_NAME = constants.DEFAULT_DJANGO_APP_NAME
STATEMENT_MODEL = 'Statement'
TAG_MODEL = 'Tag'
if hasattr(settings, 'CHATTERBOT'):
"""
Allow related models to be overridden in the project settings.
Default to the original settings if one is not defined.
"""
DJANGO_APP_NAME = settings.CHATTERBOT.get(
'django_app_name',
DJANGO_APP_NAME
)
STATEMENT_MODEL = settings.CHATTERBOT.get(
'statement_model',
STATEMENT_MODEL
)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.django_app_name = kwargs.get(
'django_app_name',
constants.DEFAULT_DJANGO_APP_NAME
)
search_text = models.CharField(
max_length=constants.STATEMENT_TEXT_MAX_LENGTH,
blank=True
)
conversation = models.CharField(
max_length=constants.CONVERSATION_LABEL_MAX_LENGTH
)
created_at = models.DateTimeField(
default=timezone.now,
help_text='The date and time that the statement was created at.'
)
in_response_to = models.CharField(
max_length=constants.STATEMENT_TEXT_MAX_LENGTH,
null=True
)
search_in_response_to = models.CharField(
max_length=constants.STATEMENT_TEXT_MAX_LENGTH,
blank=True
)
persona = models.CharField(
max_length=constants.PERSONA_MAX_LENGTH
)
tags = models.ManyToManyField(
TAG_MODEL,
related_name='statements'
)
"""
Default ChatterBot settings for Django.
"""
from django.conf import settings
from chatterbot import constants
CHATTERBOT_SETTINGS = getattr(settings, 'CHATTERBOT', {})
CHATTERBOT_DEFAULTS = {
'name': 'ChatterBot',
'storage_adapter': 'chatterbot.storage.DjangoStorageAdapter',
'django_app_name': constants.DEFAULT_DJANGO_APP_NAME
}
CHATTERBOT = CHATTERBOT_DEFAULTS.copy()
CHATTERBOT.update(CHATTERBOT_SETTINGS)
"""
These factories are used to generate fake data for testing.
"""
import factory
from chatterbot.ext.django_chatterbot import models
from chatterbot import constants
from factory.django import DjangoModelFactory
class StatementFactory(DjangoModelFactory):
text = factory.Faker(
'text',
max_nb_chars=constants.STATEMENT_TEXT_MAX_LENGTH
)
class Meta:
model = models.Statement
class ResponseFactory(DjangoModelFactory):
statement = factory.SubFactory(StatementFactory)
response = factory.SubFactory(StatementFactory)
class Meta:
model = models.Response