Skip to content

Commit

Permalink
Allow statements containing specific words to be excluded from respon…
Browse files Browse the repository at this point in the history
…se results.
  • Loading branch information
gunthercox committed Nov 25, 2018
1 parent c6c6804 commit 3b5c276
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
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!')

0 comments on commit 3b5c276

Please sign in to comment.