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 sqlite pragma settings to sql_storage.py - change distinct to aggregate in mongodb.py #916

Merged
merged 18 commits into from
Aug 11, 2017
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 1 addition & 1 deletion chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def get_response(self, input_item, conversation_id=None):

if not self.read_only:
self.learn_response(statement, previous_statement)
self.storage.add_to_converation(conversation_id, statement, response)
self.storage.add_to_conversation(conversation_id, statement, response)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


# Process the response output with the output adapter
return self.output.process_response(response, conversation_id)
Expand Down
2 changes: 1 addition & 1 deletion chatterbot/storage/django_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def create_conversation(self):
conversation = Conversation.objects.create()
return conversation.id

def add_to_converation(self, conversation_id, statement, response):
def add_to_conversation(self, conversation_id, statement, response):
"""
Add the statement and response to the conversation.
"""
Expand Down
20 changes: 13 additions & 7 deletions chatterbot/storage/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ def __init__(self, **kwargs):
# Use the default host and port
self.client = MongoClient(self.database_uri)

# Increase the sort buffer to 42M
self.client.admin.command({'setParameter': 1, 'internalQueryExecMaxBlockingSortBytes': 44040192})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like tests are failing after this change;

pymongo.errors.OperationFailure: no such command: 'internalQueryExecMaxBlockingSortBytes', bad cmd: '{
internalQueryExecMaxBlockingSortBytes: 44040192, setParameter: 1
}'


# Specify the name of the database
self.database = self.client[self.database_name]

Expand Down Expand Up @@ -262,7 +265,7 @@ def get_latest_response(self, conversation_id):

return self.mongo_to_object(statements[-2])

def add_to_converation(self, conversation_id, statement, response):
def add_to_conversation(self, conversation_id, statement, response):
"""
Add the statement and response to the conversation.
"""
Expand Down Expand Up @@ -331,23 +334,26 @@ 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.
"""
response_query = self.statements.distinct('in_response_to.text')
response_query = self.statements.aggregate([{'$group': {'_id': '$in_response_to.text'}}])

responses = []
for r in response_query:
try:
responses.extend(r['_id'])
except TypeError:
pass

_statement_query = {
'text': {
'$in': response_query
'$in': responses
}
}

_statement_query.update(self.base_query.value())

statement_query = self.statements.find(_statement_query)

statement_objects = []

for statement in list(statement_query):
statement_objects.append(self.mongo_to_object(statement))

return statement_objects

def drop(self):
Expand Down
13 changes: 12 additions & 1 deletion chatterbot/storage/sql_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ def __init__(self, **kwargs):

self.engine = create_engine(self.database_uri, convert_unicode=True)

from re import search

if search('^sqlite://', self.database_uri):
from sqlalchemy.engine import Engine
from sqlalchemy import event

@event.listens_for(Engine, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
dbapi_connection.execute('PRAGMA journal_mode=WAL')
dbapi_connection.execute('PRAGMA synchronous=NORMAL')

self.read_only = self.kwargs.get(
"read_only", False
)
Expand Down Expand Up @@ -245,7 +256,7 @@ def create_conversation(self):

return conversation_id

def add_to_converation(self, conversation_id, statement, response):
def add_to_conversation(self, conversation_id, statement, response):
"""
Add the statement and response to the conversation.
"""
Expand Down
4 changes: 2 additions & 2 deletions chatterbot/storage/storage_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ def create_conversation(self):
'The `create_conversation` method is not implemented by this adapter.'
)

def add_to_converation(self, conversation_id, statement, response):
def add_to_conversation(self, conversation_id, statement, response):
"""
Add the statement and response to the conversation.
"""
raise self.AdapterMethodNotImplementedError(
'The `add_to_converation` method is not implemented by this adapter.'
'The `add_to_conversation` method is not implemented by this adapter.'
)

def get_random(self):
Expand Down
4 changes: 2 additions & 2 deletions examples/learning_feedback_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
output_adapter='chatterbot.output.TerminalAdapter'
)

DEFAULT_SESSION_ID = bot.default_session.id
DEFAULT_SESSION_ID = bot.default_conversation_id
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could also rephrase to DEFAULT_CONVERSATION_ID

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.



def get_feedback():
Expand Down Expand Up @@ -56,7 +56,7 @@ def get_feedback():

# Update the conversation history for the bot
# It is important that this happens last, after the learning step
bot.storage.add_to_converation(bot.default_session, statement, response)
bot.storage.add_to_conversation(bot.default_session, statement, response)

# Press ctrl-c or ctrl-d on the keyboard to exit
except (KeyboardInterrupt, EOFError, SystemExit):
Expand Down
2 changes: 1 addition & 1 deletion examples/learning_new_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

bot.train("chatterbot.corpus.english")

DEFAULT_SESSION_ID = bot.default_session.id
DEFAULT_SESSION_ID = bot.default_conversation_id
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could also rephrase to DEFAULT_CONVERSATION_ID

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.



def get_feedback():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_modify_chatbot(self):
def test_get_latest_response(self):
from chatterbot.conversation import Statement
conversation_id = self.chatbot.storage.create_conversation()
self.chatbot.storage.add_to_converation(
self.chatbot.storage.add_to_conversation(
conversation_id, Statement(text='A'), Statement(text='B')
)

Expand Down