From bf6c6c267cb577dc6909c226beeb5b25b33ee590 Mon Sep 17 00:00:00 2001 From: Gunther Cox Date: Tue, 11 Jul 2017 06:40:09 -0400 Subject: [PATCH] Properly order returned conversation statements from mongodb --- chatterbot/filters.py | 6 ++--- chatterbot/storage/mongodb.py | 43 ++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/chatterbot/filters.py b/chatterbot/filters.py index 80a613496..9f13cbc34 100644 --- a/chatterbot/filters.py +++ b/chatterbot/filters.py @@ -29,9 +29,9 @@ def filter_selection(self, chatterbot, session_id): text_of_recent_responses = [] # TODO: Add a larger quantity of response history - text_of_recent_responses.append( - chatterbot.storage.get_latest_response(session_id) - ) + latest_response = chatterbot.storage.get_latest_response(session_id) + if latest_response: + text_of_recent_responses.append(latest_response.text) # Return the query with no changes if there are no statements to exclude if not text_of_recent_responses: diff --git a/chatterbot/storage/mongodb.py b/chatterbot/storage/mongodb.py index 613efb8c0..2a130d7f6 100644 --- a/chatterbot/storage/mongodb.py +++ b/chatterbot/storage/mongodb.py @@ -243,9 +243,7 @@ def create_conversation(self): """ Create a new conversation. """ - conversation_id = self.conversations.insert_one({ - 'statements': [] - }).inserted_id + conversation_id = self.conversations.insert_one({}).inserted_id return conversation_id def get_latest_response(self, conversation_id): @@ -253,32 +251,45 @@ def get_latest_response(self, conversation_id): Returns the latest response in a conversation if it exists. Returns None if a matching conversation cannot be found. """ - conversation = self.conversations.find_one({ - '_id': conversation_id - }) + from pymongo import DESCENDING - if not conversation['statements']: - return None + statements = list(self.statements.find({ + 'conversations.id': conversation_id + }).sort('conversations.created_at', DESCENDING)) - # TODO: Check if ordering is needed + if not statements: + return None - return conversation['statements'][-2] + return self.mongo_to_object(statements[-2]) def add_to_converation(self, conversation_id, statement, response): """ Add the statement and response to the conversation. """ - self.conversations.update_one( + from datetime import datetime, timedelta + self.statements.update_one( + { + 'text': statement.text + }, + { + '$push': { + 'conversations': { + 'id': conversation_id, + 'created_at': datetime.utcnow() + } + } + } + ) + self.statements.update_one( { - 'id': conversation_id + 'text': response.text }, { '$push': { 'conversations': { - '$each': [ - statement.text, - response.text - ] + 'id': conversation_id, + # Force the response to be at least one millisecond after the input statement + 'created_at': datetime.utcnow() + timedelta(milliseconds=1) } } }