Skip to content

Commit 135d4c3

Browse files
Choosing matches with greater certainty than the original if available.
1 parent d911b4a commit 135d4c3

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

chatterbot/logic/best_match.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from chatterbot.logic import LogicAdapter
22
from chatterbot import filters
3+
import random
34

45

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

40+
# If closest_match and the next result have equal cofidence, then choose either randomly.
41+
if result.confidence == closest_match.confidence:
42+
closest_match = random.choice([result])
43+
44+
# If our new result is better than our previous closest_match, choose closest_match instead.
45+
elif result.confidence > closest_match.confidence:
46+
closest_match = result
47+
3948
self.chatbot.logger.info('Using "{}" as a close match to "{}" with a confidence of {}'.format(
4049
closest_match.text, input_statement.text, closest_match.confidence
4150
))

0 commit comments

Comments
 (0)