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

Allow statements containing specific words to be excluded from response results #1511

Merged
merged 1 commit into from
Nov 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion chatterbot/logic/best_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def process(self, input_statement):
# Get all statements that are in response to the closest match
response_list = list(self.chatbot.storage.filter(
search_in_response_to=closest_match.search_text,
exclude_text=recent_repeated_responses
exclude_text=recent_repeated_responses,
exclude_text_words=self.excluded_words
))

if response_list:
Expand Down
9 changes: 9 additions & 0 deletions chatterbot/logic/logic_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ def __init__(self, chatbot, **kwargs):
'maximum_similarity_threshold', 0.95
)

'''
The excluded_words parameter allows a list of words to be set that will
prevent the logic adapter from returning statements that have text
containing any of those words. This can be useful for preventing your
chat bot from saying swears when it is being demonstrated in front of
an audience.
'''
self.excluded_words = kwargs.get('excluded_words')

# The maximum number of records to load into memory at a time when searching
self.search_page_size = kwargs.get(
'search_page_size', 1000
Expand Down
28 changes: 24 additions & 4 deletions tests/logic/test_best_match.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from unittest.mock import MagicMock
from chatterbot.logic import BestMatch
from chatterbot.conversation import Statement
from tests.base_case import ChatBotTestCase
Expand All @@ -17,11 +16,32 @@ def test_no_choices(self):
"""
An exception should be raised if there is no data in the database.
"""
self.adapter.chatbot.storage.filter = MagicMock(return_value=[])
self.adapter.chatbot.storage.count = MagicMock(return_value=0)

statement = Statement(text='What is your quest?')
response = self.adapter.get(statement)

self.assertEqual(response.text, 'What is your quest?')
self.assertEqual(response.confidence, 0)

def test_excluded_words(self):
"""
Test that the logic adapter cannot return a response containing
any of the listed words for exclusion.
"""
self.chatbot.storage.create(
text='I like to count.'
)
self.chatbot.storage.create(
text='Counting is dumb.',
in_response_to='I like to count.'
)
self.chatbot.storage.create(
text='Counting is fun!',
in_response_to='I like to count.'
)

self.adapter.excluded_words = ['dumb']

response = self.adapter.process(Statement(text='I like to count.'))

self.assertEqual(response.confidence, 1)
self.assertEqual(response.text, 'Counting is fun!')