Match substrings fuzzily

This commit is contained in:
Willow Carretero Chavez 2024-03-11 13:33:17 -04:00
parent 3548e35360
commit 655b0bdc77

View file

@ -31,7 +31,19 @@ class FuzzyQuery(StringFieldQuery):
val = val.lower()
query_matcher = difflib.SequenceMatcher(None, pattern, val)
threshold = config["fuzzy"]["threshold"].as_number()
return query_matcher.quick_ratio() >= threshold
# Adjust match threshold for the case that the pattern is shorter
# than the value being matched. This allows the pattern to match
# substrings of the value, not just the entire value.
if len(pattern) < len(val):
max_possible_ratio = 2 * len(pattern) / (len(pattern) + len(val))
threshold *= max_possible_ratio
# If upper bound of the ratio meets threshold, then calculate
# the actual ratio.
if query_matcher.quick_ratio() >= threshold:
return query_matcher.ratio() >= threshold
return False
class FuzzyPlugin(BeetsPlugin):