Skip to content

Commit

Permalink
Remove response object
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Jan 28, 2017
1 parent 7c712a4 commit c9e3b29
Show file tree
Hide file tree
Showing 22 changed files with 115 additions and 482 deletions.
4 changes: 1 addition & 3 deletions chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ def learn_response(self, statement, previous_statement):
from .conversation import Response

if previous_statement:
statement.add_response(
Response(previous_statement.text)
)
statement.in_response_to = previous_statement
self.logger.info('Adding "{}" as a response to "{}"'.format(
statement.text,
previous_statement.text
Expand Down
3 changes: 0 additions & 3 deletions chatterbot/conversation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
from .statement import Statement
from .response import Response
from .session import Session

Conversation = Session
34 changes: 0 additions & 34 deletions chatterbot/conversation/response.py

This file was deleted.

13 changes: 11 additions & 2 deletions chatterbot/conversation/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ def all(self):
"""
Return all statements in the conversation.
"""
return self.storage.filter(conversation__id=self.conversation_id)
return self.storage.filter(
conversation__id=self.conversation_id,
order_by='created_at'
)

def add(self, statement):
"""
Expand All @@ -25,6 +28,12 @@ def add(self, statement):
statement.conversation_id = self.conversation_id
self.storage.update(statement)

def count(self):
return len(self.all())

def exists(self):
return self.count() > 0


class Session(object):
"""
Expand Down Expand Up @@ -52,7 +61,7 @@ def get_last_response_statement(self):
statements = self.statements.all()
if statements:
# Return the latest output statement (This should be ordering them by date to get the latest)
return statements[-1]
return statements[1]
return None


Expand Down
100 changes: 23 additions & 77 deletions chatterbot/conversation/statement.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
# -*- coding: utf-8 -*-
from .response import Response
from datetime import datetime


class StatementSerializer(object):

def serialize(self, obj):
"""
:returns: A dictionary representation of the statement object.
:rtype: dict
"""
data = {}

data['text'] = obj.text
data['in_response_to'] = {'text': obj.in_response_to.text}
data['created_at'] = obj.created_at
data['extra_data'] = obj.extra_data

return data

def deserialize(self, data):
pass


class Statement(object):
"""
A statement represents a single spoken entity, sentence or
Expand All @@ -11,8 +30,7 @@ class Statement(object):

def __init__(self, text, **kwargs):
self.text = text
self.conversation_id = kwargs.pop('conversation_id', None)
self.in_response_to = kwargs.pop('in_response_to', [])
self.in_response_to = kwargs.pop('in_response_to', None)

# The date and time that this statement was created at
self.created_at = kwargs.pop('created_at', datetime.now())
Expand Down Expand Up @@ -68,81 +86,9 @@ def add_extra_data(self, key, value):
"""
self.extra_data[key] = value

def add_response(self, response):
"""
Add the response to the list of statements that this statement is in response to.
If the response is already in the list, increment the occurrence count of that response.
:param response: The response to add.
:type response: `Response`
"""
if not isinstance(response, Response):
raise Statement.InvalidTypeException(
'A {} was recieved when a {} instance was expected'.format(
type(response),
type(Response(''))
)
)

updated = False
for index in range(0, len(self.in_response_to)):
if response.text == self.in_response_to[index].text:
self.in_response_to[index].occurrence += 1
updated = True

if not updated:
self.in_response_to.append(response)

def remove_response(self, response_text):
"""
Removes a response from the statement's response list based
on the value of the response text.
:param response_text: The text of the response to be removed.
:type response_text: str
"""
for response in self.in_response_to:
if response_text == response.text:
self.in_response_to.remove(response)
return True
return False

def get_response_count(self, statement):
"""
Find the number of times that the statement has been used
as a response to the current statement.
:param statement: The statement object to get the count for.
:type statement: `Statement`
:returns: Return the number of times the statement has been used as a response.
:rtype: int
"""
for response in self.in_response_to:
if statement.text == response.text:
return response.occurrence

return 0

def serialize(self):
"""
:returns: A dictionary representation of the statement object.
:rtype: dict
"""
data = {}

data['text'] = self.text
data['in_response_to'] = []
data['created_at'] = self.created_at
data['extra_data'] = self.extra_data

if self.conversation_id:
data['conversation_id'] = self.conversation_id

for response in self.in_response_to:
data['in_response_to'].append(response.serialize())

return data
serializer = StatementSerializer()
return serializer.serialize(self)

@property
def response_statement_cache(self):
Expand Down
8 changes: 1 addition & 7 deletions chatterbot/ext/django_chatterbot/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from chatterbot.ext.django_chatterbot.models import Statement, Response, Conversation
from chatterbot.ext.django_chatterbot.models import Statement, Conversation


class StatementAdmin(admin.ModelAdmin):
Expand All @@ -8,15 +8,9 @@ class StatementAdmin(admin.ModelAdmin):
search_fields = ('text', )


class ResponseAdmin(admin.ModelAdmin):
list_display = ('statement', 'occurrence', )


class ConversationAdmin(admin.ModelAdmin):
list_display = ('statement', 'occurrence', )
list_display = ('root', )


admin.site.register(Statement, StatementAdmin)
admin.site.register(Response, ResponseAdmin)
admin.site.register(Conversation, ConversationAdmin)
108 changes: 10 additions & 98 deletions chatterbot/ext/django_chatterbot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,24 @@ class Statement(models.Model):
"""

text = models.CharField(
unique=True,
blank=False,
null=False,
max_length=255
)

conversation = models.ForeignKey(
'Conversation',
related_name='statements',
blank=True,
null=True
)

created_at = models.DateTimeField(
default=timezone.now,
help_text='The date and time that this statement was created at.'
)

extra_data = models.CharField(max_length=500)

response = models.OneToOneField(
'Statement',
related_name='in_response_to',
null=True
)

# This is the confidence with which the chat bot believes
# this is an accurate response. This value is set when the
# statement is returned by the chat bot.
Expand All @@ -47,12 +45,11 @@ def __init__(self, *args, **kwargs):
# Responses to be saved if the statement is updated with the storage adapter
self.response_statement_cache = []

@property
def in_response_to(self):
def responses(self):
"""
Return the response objects that are for this statement.
Return a list of statements that are known responses to this statement.
"""
return Response.objects.filter(statement=self)
return Statement.objects.filter(in_response_to__text=self.text)

def add_extra_data(self, key, value):
"""
Expand All @@ -68,45 +65,6 @@ def add_extra_data(self, key, value):

self.extra_data = json.dumps(extra_data)

def add_response(self, statement):
"""
Add a response to this statement.
"""
self.response_statement_cache.append(statement)

def remove_response(self, response_text):
"""
Removes a response from the statement's response list based
on the value of the response text.
:param response_text: The text of the response to be removed.
:type response_text: str
"""
is_deleted = False
response = self.in_response.filter(response__text=response_text)

if response.exists():
is_deleted = True

return is_deleted

def get_response_count(self, statement):
"""
Find the number of times that the statement has been used
as a response to the current statement.
:param statement: The statement object to get the count for.
:type statement: chatterbot.conversation.statement.Statement
:returns: Return the number of times the statement has been used as a response.
:rtype: int
"""
try:
response = self.in_response.get(response__text=statement.text)
return response.occurrence
except Response.DoesNotExist:
return 0

def serialize(self):
"""
:returns: A dictionary representation of the statement object.
Expand All @@ -119,56 +77,10 @@ def serialize(self):
self.extra_data = '{}'

data['text'] = self.text
data['in_response_to'] = []
data['in_response_to'] = {'text': self.in_response_to.text}
data['created_at'] = self.created_at
data['extra_data'] = json.loads(self.extra_data)

for response in self.in_response.all():
data['in_response_to'].append(response.serialize())

return data


class Response(models.Model):
"""
Connection between a response and the statement that triggered it.
Comparble to a ManyToMany "through" table, but without the M2M indexing/relations.
The text and number of times the response has occurred are stored.
"""

statement = models.ForeignKey(
'Statement',
related_name='in_response'
)

response = models.ForeignKey(
'Statement',
related_name='responses'
)

unique_together = (('statement', 'response'),)

occurrence = models.PositiveIntegerField(default=1)

def __str__(self):
statement = self.statement.text
response = self.response.text
return '{} => {}'.format(
statement if len(statement) <= 20 else statement[:17] + '...',
response if len(response) <= 40 else response[:37] + '...'
)

def serialize(self):
"""
:returns: A dictionary representation of the statement object.
:rtype: dict
"""
data = {}

data['text'] = self.response.text
data['occurrence'] = self.occurrence

return data


Expand Down
Loading

0 comments on commit c9e3b29

Please sign in to comment.