This commit is contained in:
Willow Carretero Chavez 2026-02-03 03:28:22 +10:00 committed by GitHub
commit ac967fc167
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -29,7 +29,19 @@ class FuzzyQuery(StringFieldQuery[str]):
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):