diff --git a/beetsplug/fuzzy.py b/beetsplug/fuzzy.py index 959544ed3..9cd8915d6 100644 --- a/beetsplug/fuzzy.py +++ b/beetsplug/fuzzy.py @@ -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):