-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace filter classes with a single method
- Loading branch information
1 parent
26bf1d2
commit 6dcdb07
Showing
10 changed files
with
50 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,26 @@ | ||
""" | ||
Filters set the base query that gets passed to the storage adapter. | ||
""" | ||
|
||
|
||
class Filter(object): | ||
""" | ||
A base filter object from which all other | ||
filters should be subclassed. | ||
""" | ||
|
||
def filter_selection(self, chatterbot, conversation): | ||
""" | ||
Because this is the base filter class, this method just | ||
returns the storage adapter's base query. Other filters | ||
are expected to override this method. | ||
""" | ||
return chatterbot.storage.base_query | ||
|
||
|
||
class RepetitiveResponseFilter(Filter): | ||
def get_recent_repeated_responses(chatbot, conversation, sample=10, threshold=3, quantity=3): | ||
""" | ||
A filter that eliminates possibly repetitive responses to prevent | ||
a chat bot from repeating statements that it has recently said. | ||
""" | ||
from collections import Counter | ||
|
||
def filter_selection(self, chatterbot, conversation): | ||
from collections import Counter | ||
|
||
# Get the 10 most recent statements from the conversation | ||
conversation_statements = chatterbot.storage.filter( | ||
conversation=conversation, | ||
order_by=['id'] | ||
)[-10:] | ||
|
||
text_of_recent_responses = [ | ||
statement.in_response_to for statement in conversation_statements | ||
if statement is not None and statement.in_response_to is not None | ||
] | ||
|
||
counter = Counter(text_of_recent_responses) | ||
# Get the most recent statements from the conversation | ||
conversation_statements = list(chatbot.storage.filter( | ||
conversation=conversation, | ||
order_by=['id'] | ||
))[sample * -1:] | ||
|
||
# Find the three most common responses from the conversation | ||
most_common = counter.most_common(3) | ||
text_of_recent_responses = [ | ||
statement.text for statement in conversation_statements | ||
] | ||
|
||
# Return the query with no changes if there are no statements to exclude | ||
if not most_common: | ||
return super(RepetitiveResponseFilter, self).filter_selection( | ||
chatterbot, | ||
conversation | ||
) | ||
counter = Counter(text_of_recent_responses) | ||
|
||
query = chatterbot.storage.base_query.statement_in_response_to_not_in( | ||
[text[0] for text in most_common] | ||
) | ||
# Find the n most common responses from the conversation | ||
most_common = counter.most_common(quantity) | ||
|
||
return query | ||
return [ | ||
counted[0] for counted in most_common | ||
if counted[1] >= threshold | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
======= | ||
Filters | ||
======= | ||
|
||
Filters are an efficient way to create queries that can be passed to ChatterBot's storage adapters. | ||
Filters will reduce the number of statements that a chat bot has to process when it is selecting a response. | ||
|
||
Setting filters | ||
=============== | ||
|
||
.. code-block:: python | ||
chatbot = ChatBot( | ||
"My ChatterBot", | ||
filters=[filters.get_recent_repeated_responses] | ||
) | ||
.. automodule:: chatterbot.filters | ||
:members: |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters