Skip to content

Commit

Permalink
Add save method to statements
Browse files Browse the repository at this point in the history
Calling .save() should be more intuitive than passing the statement
to the update method of the storage adapter.
  • Loading branch information
gunthercox committed Dec 18, 2016
1 parent ab0bd09 commit 73d81da
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def learn_response(self, statement, previous_statement):
previous_statement.text
))

# Update the database after selecting a response
# Save the statement after selecting a response
self.storage.update(statement)

def set_trainer(self, training_class, **kwargs):
Expand Down
8 changes: 8 additions & 0 deletions chatterbot/conversation/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def __init__(self, text, **kwargs):
self.in_response_to = kwargs.pop('in_response_to', [])
self.extra_data = kwargs.pop('extra_data', {})

self.storage = None

def __str__(self):
return self.text

Expand All @@ -31,6 +33,12 @@ def __eq__(self, other):

return self.text == other

def save(self):
"""
Save the statement in the database.
"""
self.storage.update(self)

def add_extra_data(self, key, value):
"""
This method allows additional data to be stored on the statement object.
Expand Down
8 changes: 4 additions & 4 deletions chatterbot/storage/jsonfile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import warnings
from chatterbot.storage import StorageAdapter
from chatterbot.conversation import Statement, Response
from chatterbot.conversation import Response


class JsonFileStorageAdapter(StorageAdapter):
Expand Down Expand Up @@ -69,7 +69,7 @@ def deserialize_responses(self, response_list):
Takes the list of response items and returns
the list converted to Response objects.
"""
proxy_statement = Statement('')
proxy_statement = self.Statement('')

for response in response_list:
data = response.copy()
Expand Down Expand Up @@ -98,7 +98,7 @@ def json_to_object(self, statement_data):
# Remove the text attribute from the values
text = statement_data.pop('text')

return Statement(text, **statement_data)
return self.Statement(text, **statement_data)

def _all_kwargs_match_values(self, kwarguments, values):
for kwarg in kwarguments:
Expand Down Expand Up @@ -159,7 +159,7 @@ def update(self, statement, **kwargs):
for response_statement in statement.in_response_to:
response = self.find(response_statement.text)
if not response:
response = Statement(response_statement.text)
response = self.Statement(response_statement.text)
self.update(response)

return statement
Expand Down
8 changes: 4 additions & 4 deletions chatterbot/storage/mongodb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from chatterbot.storage import StorageAdapter
from chatterbot.conversation import Statement, Response
from chatterbot.conversation import Response


class Query(object):
Expand Down Expand Up @@ -126,14 +126,14 @@ def find(self, statement_text):
values.get('in_response_to', [])
)

return Statement(statement_text, **values)
return self.Statement(statement_text, **values)

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

for response in response_list:
text = response['text']
Expand All @@ -157,7 +157,7 @@ def mongo_to_object(self, statement_data):
statement_data.get('in_response_to', [])
)

return Statement(statement_text, **statement_data)
return self.Statement(statement_text, **statement_data)

def filter(self, **kwargs):
"""
Expand Down
16 changes: 16 additions & 0 deletions chatterbot/storage/storage_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ def __init__(self, base_query=None, *args, **kwargs):
self.adapter_supports_queries = True
self.base_query = None

@property
def Statement(self):
"""
Create a storage-aware statement.
"""
import os

if 'DJANGO_SETTINGS_MODULE' in os.environ:
from chatterbot.ext.django_chatterbot.models import Statement
return Statement
else:
from chatterbot.conversation.statement import Statement
statement = Statement
statement.storage = self
return statement

def generate_base_query(self, chatterbot, session_id):
"""
Create a base query for the storage adapter.
Expand Down

0 comments on commit 73d81da

Please sign in to comment.