Skip to content

Commit

Permalink
Remove conversation_sessions variable
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Jul 8, 2017
1 parent 47b2273 commit 16d5d04
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
3 changes: 0 additions & 3 deletions chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class ChatBot(object):
"""

def __init__(self, name, **kwargs):
from .conversation.session import ConversationSessionManager
from .logic import MultiLogicAdapter

self.name = name
Expand Down Expand Up @@ -72,8 +71,6 @@ def __init__(self, name, **kwargs):
self.trainer = TrainerClass(self.storage, **kwargs)
self.training_data = kwargs.get('training_data')

self.conversation_sessions = ConversationSessionManager()

self.default_session_id = self.storage.create_conversation()

self.logger = kwargs.get('logger', logging.getLogger(__name__))
Expand Down
18 changes: 11 additions & 7 deletions chatterbot/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ class RepetitiveResponseFilter(Filter):

def filter_selection(self, chatterbot, session_id):

session = chatterbot.conversation_sessions.get(session_id)

if session.conversation.empty():
return chatterbot.storage.base_query

text_of_recent_responses = []

for statement, response in session.conversation:
text_of_recent_responses.append(response.text)
# TODO: Add a larger quantity of response history
text_of_recent_responses.append(
chatterbot.storage.get_latest_response(session_id)
)

# Return the query with no changes if there are no statements to exclude
if not text_of_recent_responses:
return super(RepetitiveResponseFilter, self).filter_selection(
chatterbot,
session_id
)

query = chatterbot.storage.base_query.statement_text_not_in(
text_of_recent_responses
Expand Down
18 changes: 14 additions & 4 deletions chatterbot/storage/sql_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

Base = declarative_base()


class StatementTable(Base):
"""
StatementTable, placeholder for a sentence or phrase.
Expand Down Expand Up @@ -97,7 +96,7 @@ class Conversation(Base):

__tablename__ = 'conversation'

id = Column(Integer, primary_key=True, autoincrement=True, default=1)
id = Column(Integer, primary_key=True)

statements = relationship(
'StatementTable',
Expand Down Expand Up @@ -347,6 +346,19 @@ def add_to_converation(self, conversation_id, statement, response):
text=response.text
).first()

# Make sure the statements exist
if not statement_query:
self.update(statement)
statement_query = session.query(StatementTable).filter_by(
text=statement.text
).first()

if not response_query:
self.update(response)
response_query = session.query(StatementTable).filter_by(
text=response.text
).first()

conversation.statements.append(statement_query)
conversation.statements.append(response_query)

Expand All @@ -358,8 +370,6 @@ 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.
"""
from sqlalchemy import text

session = self.Session()
statement = None

Expand Down
21 changes: 14 additions & 7 deletions tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ def test_modify_chatbot(self):
When one adapter modifies its chatbot instance,
the change should be the same in all other adapters.
"""
session = self.chatbot.input.chatbot.conversation_sessions.new()
self.chatbot.input.chatbot.conversation_sessions.update(
session.id_string,
('A', 'B', )
self.chatbot.input.chatbot.read_only = 'TESTING'

value = self.chatbot.output.chatbot.read_only

self.assertEqual('TESTING', value)

def test_get_latest_response(self):
from chatterbot.conversation import Statement
conversation_id = self.chatbot.storage.create_conversation()
self.chatbot.storage.add_to_converation(
conversation_id, Statement(text='A'), Statement(text='B')
)

session = self.chatbot.output.chatbot.conversation_sessions.get(
session.id_string
response_statement = self.chatbot.storage.get_latest_response(
conversation_id
)

self.assertIn(('A', 'B', ), session.conversation)
self.assertEqual('A', response_statement.text)

0 comments on commit 16d5d04

Please sign in to comment.