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

Request for Comments: Small change to the BestMatch search algorithm #2092

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions chatterbot/logic/best_match.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from chatterbot.logic import LogicAdapter
from chatterbot import filters
import random


class BestMatch(LogicAdapter):
Expand Down Expand Up @@ -36,6 +37,14 @@ def process(self, input_statement, additional_response_selection_parameters=None
if result.confidence >= self.maximum_similarity_threshold:
break

# If closest_match and the next result have equal cofidence, then choose either randomly.
if result.confidence == closest_match.confidence:
closest_match = random.choice([result, closest_match])

# If our new result is better than our previous closest_match, choose closest_match instead.
elif result.confidence > closest_match.confidence:
closest_match = result

self.chatbot.logger.info('Using "{}" as a close match to "{}" with a confidence of {}'.format(
closest_match.text, input_statement.text, closest_match.confidence
))
Expand Down