Skip to content

Commit

Permalink
Start removing 'add_response' method
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Jan 27, 2017
1 parent fc020f2 commit 1d1b1e6
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 320 deletions.
6 changes: 1 addition & 5 deletions chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,8 @@ def learn_response(self, statement, previous_statement):
"""
Learn that the statement provided is a valid response.
"""
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
1 change: 0 additions & 1 deletion chatterbot/conversation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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.

59 changes: 1 addition & 58 deletions chatterbot/conversation/statement.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from .response import Response
from datetime import datetime


Expand Down Expand Up @@ -31,7 +30,7 @@ class Statement(object):

def __init__(self, text, **kwargs):
self.text = text
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 @@ -87,62 +86,6 @@ 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):
serializer = StatementSerializer()
return serializer.serialize(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)
12 changes: 6 additions & 6 deletions chatterbot/ext/django_chatterbot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def __init__(self, *args, **kwargs):
# Responses to be saved if the statement is updated with the storage adapter
self.response_statement_cache = []

def responses(self):
"""
Return a list of statements that are known responses to this statement.
"""
return Statement.objects.filter(in_response_to__text=self.text)

def add_extra_data(self, key, value):
"""
Add extra data to the extra_data field.
Expand All @@ -59,12 +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 serialize(self):
"""
:returns: A dictionary representation of the statement object.
Expand Down
21 changes: 5 additions & 16 deletions chatterbot/storage/django_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def get_random(self):
Returns a random statement from the database
"""
from chatterbot.ext.django_chatterbot.models import Statement

return Statement.objects.order_by('?').first()

def remove(self, statement_text):
Expand All @@ -118,25 +119,15 @@ def remove(self, statement_text):
input text.
"""
from chatterbot.ext.django_chatterbot.models import Statement
from chatterbot.ext.django_chatterbot.models import Response
from django.db.models import Q
statements = Statement.objects.filter(text=statement_text)

responses = Response.objects.filter(
Q(statement__text=statement_text) | Q(response__text=statement_text)
)

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

def drop(self):
"""
Remove all data from the database.
"""
from chatterbot.ext.django_chatterbot.models import Statement, Response, Conversation
from chatterbot.ext.django_chatterbot.models import Statement, Conversation

Statement.objects.all().delete()
Response.objects.all().delete()
Conversation.objects.all().delete()

def get_response_statements(self):
Expand All @@ -146,8 +137,6 @@ 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

responses = Response.objects.all()
from chatterbot.ext.django_chatterbot.models import Statement

return Statement.objects.filter(in_response__in=responses)
return Statement.objects.filter(in_response_to__isnull=False)
32 changes: 3 additions & 29 deletions chatterbot/storage/jsonfile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import warnings
from chatterbot.storage import StorageAdapter
from chatterbot.conversation import Response


class JsonFileStorageAdapter(StorageAdapter):
Expand Down Expand Up @@ -59,24 +58,6 @@ def remove(self, statement_text):

self.database.delete(statement_text)

def deserialize_responses(self, response_list):
"""
Takes the list of response items and returns
the list converted to Response objects.
"""
proxy_statement = self.Statement('')

for response in response_list:
data = response.copy()
text = data['text']
del data['text']

proxy_statement.add_response(
Response(text, **data)
)

return proxy_statement.in_response_to

def json_to_object(self, statement_data):
"""
Converts a dictionary-like object to a Statement object.
Expand All @@ -86,8 +67,8 @@ def json_to_object(self, statement_data):
statement_data = statement_data.copy()

# Build the objects for the response list
statement_data['in_response_to'] = self.deserialize_responses(
statement_data['in_response_to']
statement_data['in_response_to'] = self.Statement(
**statement_data['in_response_to']
)

# Remove the text attribute from the values
Expand Down Expand Up @@ -150,14 +131,8 @@ def update(self, statement):
Update a statement in the database.
"""
statements = self.database['statements']

statements.append(statement.serialize())

# Make sure that an entry for each response exists
if statement.in_response_to:
response = self.Statement(statement.in_response_to.text)
statements.append(statement.in_response_to.serialize())

self.database.data(key='statements', value=statements)

return statement
Expand All @@ -168,8 +143,7 @@ def get_random(self):
if self.count() < 1:
raise self.EmptyDatabaseException()

statement = choice(self.database['statements'])
return statement
return choice(self.database['statements'])

def drop(self):
"""
Expand Down
5 changes: 0 additions & 5 deletions chatterbot/storage/mongodb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from chatterbot.storage import StorageAdapter
from chatterbot.conversation import Response


class Query(object):
Expand Down Expand Up @@ -131,10 +130,6 @@ def deserialize_responses(self, response_list):
text = response['text']
del response['text']

proxy_statement.add_response(
Response(text, **response)
)

return proxy_statement.in_response_to

def mongo_to_object(self, statement_data):
Expand Down
12 changes: 4 additions & 8 deletions chatterbot/trainers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from .conversation import Statement, Response
from .conversation import Statement


class Trainer(object):
Expand Down Expand Up @@ -82,9 +82,7 @@ def train(self, conversation):
statement = self.get_or_create(text)

if statement_history:
statement.add_response(
Response(statement_history[-1].text)
)
statement.in_response_to = statement_history[-1]

statement_history.append(statement)
self.storage.update(statement)
Expand Down Expand Up @@ -197,7 +195,7 @@ def get_statements(self):
if tweet.in_reply_to_status_id:
try:
status = self.api.GetStatus(tweet.in_reply_to_status_id)
statement.add_response(Response(status.text))
statement.in_response_to = Statement(status.text)
statements.append(statement)
except TwitterError as error:
self.logger.warning(str(error))
Expand Down Expand Up @@ -346,9 +344,7 @@ def train(self):
statement.add_extra_data('addressing_speaker', row[2])

if statement_history:
statement.add_response(
Response(statement_history[-1].text)
)
statement.in_response_to = statement_history[-1]

statement_history.append(statement)
self.storage.update(statement)
9 changes: 0 additions & 9 deletions tests/conversation_tests/test_responses.py

This file was deleted.

Loading

0 comments on commit 1d1b1e6

Please sign in to comment.