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

Add tags attribute to models #906

Merged
merged 3 commits into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 11 additions & 2 deletions chatterbot/ext/django_chatterbot/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.contrib import admin
from chatterbot.ext.django_chatterbot.models import Statement, Response, Conversation
from chatterbot.ext.django_chatterbot.models import (
Statement, Response, Conversation, Tag
)


class StatementAdmin(admin.ModelAdmin):
Expand All @@ -14,9 +16,16 @@ class ResponseAdmin(admin.ModelAdmin):


class ConversationAdmin(admin.ModelAdmin):
pass
list_display = ('id', )


class TagAdmin(admin.ModelAdmin):
list_display = ('name', )
list_filter = ('name', )
search_fields = ('name', )


admin.site.register(Statement, StatementAdmin)
admin.site.register(Response, ResponseAdmin)
admin.site.register(Conversation, ConversationAdmin)
admin.site.register(Tag, TagAdmin)
35 changes: 35 additions & 0 deletions chatterbot/ext/django_chatterbot/migrations/0009_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11a1 on 2017-07-07 00:12
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('django_chatterbot', '0008_update_conversations'),
]

operations = [
migrations.CreateModel(
name='Tag',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.SlugField()),
],
options={
'abstract': False,
},
),
migrations.AlterField(
model_name='statement',
name='text',
field=models.CharField(max_length=255, unique=True),
),
migrations.AddField(
model_name='tag',
name='statements',
field=models.ManyToManyField(related_name='tags', to='django_chatterbot.Statement'),
),
]
30 changes: 30 additions & 0 deletions chatterbot/ext/django_chatterbot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,29 @@ def __str__(self):
return str(self.id)


class AbstractBaseTag(models.Model):
"""
The abstract base tag allows other models to
be created using the attributes that exist on the
default models.
"""

name = models.SlugField(
max_length=50
)

statements = models.ManyToManyField(
'Statement',
related_name='tags'
)

class Meta:
abstract = True

def __str__(self):
return self.name


class Statement(AbstractBaseStatement):
"""
A statement represents a single spoken entity, sentence or
Expand All @@ -225,3 +248,10 @@ class Conversation(AbstractBaseConversation):
A sequence of statements representing a conversation.
"""
pass


class Tag(AbstractBaseTag):
"""
A label that categorizes a statement.
"""
pass
20 changes: 20 additions & 0 deletions chatterbot/storage/sql_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
from sqlalchemy.sql import func
from sqlalchemy import Table, Column, Integer, String, DateTime, ForeignKey, PickleType

tag_association_table = Table(
'tag_association',
Base.metadata,
Column('tag_id', Integer, ForeignKey('tag.id')),
Column('statement_id', Integer, ForeignKey('StatementTable.id'))
)

class Tag(Base):
"""
A tag that describes a statement.
"""

name = Column(String)

class StatementTable(Base):
"""
StatementTable, placeholder for a sentence or phrase.
Expand All @@ -25,6 +39,12 @@ def get_statement(self):

text = Column(String, unique=True)

tags = relationship(
'Tag',
secondary=lambda: tag_association_table,
backref='statements'
)

extra_data = Column(PickleType)

in_response_to = relationship(
Expand Down