diff --git a/chatterbot/chatterbot.py b/chatterbot/chatterbot.py index 346fa8eca..f02319894 100644 --- a/chatterbot/chatterbot.py +++ b/chatterbot/chatterbot.py @@ -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) # Process the response output with the output adapter return self.output.process_response(response, conversation_id) diff --git a/chatterbot/storage/django_storage.py b/chatterbot/storage/django_storage.py index 12b9409d3..2b6a42625 100644 --- a/chatterbot/storage/django_storage.py +++ b/chatterbot/storage/django_storage.py @@ -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. """ diff --git a/chatterbot/storage/mongodb.py b/chatterbot/storage/mongodb.py index 2a130d7f6..504c59af0 100644 --- a/chatterbot/storage/mongodb.py +++ b/chatterbot/storage/mongodb.py @@ -81,6 +81,7 @@ class MongoDatabaseAdapter(StorageAdapter): def __init__(self, **kwargs): super(MongoDatabaseAdapter, self).__init__(**kwargs) from pymongo import MongoClient + from pymongo.errors import OperationFailure self.database_name = self.kwargs.get( 'database', 'chatterbot-database' @@ -92,6 +93,12 @@ def __init__(self, **kwargs): # Use the default host and port self.client = MongoClient(self.database_uri) + # Increase the sort buffer to 42M if possible + try: + self.client.admin.command({'setParameter': 1, 'internalQueryExecMaxBlockingSortBytes': 44040192}) + except OperationFailure: + pass + # Specify the name of the database self.database = self.client[self.database_name] @@ -262,7 +269,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. """ @@ -331,23 +338,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): diff --git a/chatterbot/storage/sql_storage.py b/chatterbot/storage/sql_storage.py index 5647fef62..6f802387d 100644 --- a/chatterbot/storage/sql_storage.py +++ b/chatterbot/storage/sql_storage.py @@ -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 ) @@ -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. """ diff --git a/chatterbot/storage/storage_adapter.py b/chatterbot/storage/storage_adapter.py index 09967cb9a..a9308de19 100644 --- a/chatterbot/storage/storage_adapter.py +++ b/chatterbot/storage/storage_adapter.py @@ -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): diff --git a/examples/learning_feedback_example.py b/examples/learning_feedback_example.py index 7ae25bd3c..b7e850749 100644 --- a/examples/learning_feedback_example.py +++ b/examples/learning_feedback_example.py @@ -22,7 +22,7 @@ output_adapter='chatterbot.output.TerminalAdapter' ) -DEFAULT_SESSION_ID = bot.default_session.id +DEFAULT_CONVERSATION_ID = bot.default_conversation_id def get_feedback(): @@ -45,7 +45,7 @@ def get_feedback(): while True: try: input_statement = bot.input.process_input_statement() - statement, response = bot.generate_response(input_statement, DEFAULT_SESSION_ID) + statement, response = bot.generate_response(input_statement, DEFAULT_CONVERSATION_ID) print('\n Is "{}" this a coherent response to "{}"? \n'.format(response, input_statement)) @@ -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): diff --git a/examples/learning_new_response.py b/examples/learning_new_response.py index 7ab9abb8e..72a4fe801 100644 --- a/examples/learning_new_response.py +++ b/examples/learning_new_response.py @@ -16,7 +16,7 @@ bot.train("chatterbot.corpus.english") -DEFAULT_SESSION_ID = bot.default_session.id +DEFAULT_CONVERSATION_ID = bot.default_conversation_id def get_feedback(): @@ -39,7 +39,7 @@ def get_feedback(): while True: try: input_statement = bot.input.process_input_statement() - statement, response = bot.generate_response(input_statement, DEFAULT_SESSION_ID) + statement, response = bot.generate_response(input_statement, DEFAULT_CONVERSATION_ID) bot.output.process_response(response) print('\n Is "{}" a coherent response to "{}"? \n'.format(response, input_statement)) if get_feedback(): diff --git a/tests/test_context.py b/tests/test_context.py index add6dd5d9..a2af84508 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -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') )