You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add ratio_min() function to the difflib library #90244
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/tim-one'closed_at=<Date2022-01-20.22:15:08.930>created_at=<Date2021-12-15.17:37:14.440>labels= ['type-feature', 'library', '3.11']
title='Add ratio_min() function to the difflib library'updated_at=<Date2022-01-20.22:15:08.929>user='https://github.com/gi-ba-bu'
Here I propose a new function, namely .ratio_min(self,m).
.ratio_min(self,m) is an extension of the difflib's function .ratio(self). Equivalently to .ratio(self), .ratio_min(self,m) returns a measure of two sequences' similarity (float in [0,1]). In addition to .ratio(), it can ignore matched substrings if these substrings have length less than a given threshold m. m is the second variable of the function.
It is very useful to avoid spurious high similarity scores.
# NEW FUNCTION:
defratio_min(self,m):
"""Return a measure of the sequences' similarity (float in [0,1]). Where T is the total number of elements in both sequences, and M_min is the number of matches with every single match has length at least m, this is 2.0*M_min / T. Note that this is 1 if the sequences are identical, and 0 if they have no substring of length m or more in common. .ratio_min() is similar to .ratio(). .ratio_min(1) is equivalent to .ratio(). >>> s = SequenceMatcher(None, "abcd", "bcde") >>> s.ratio_min(1) 0.75 >>> s.ratio_min(2) 0.75 >>> s.ratio_min(3) 0.75 >>> s.ratio_min(4) 0.0 """matches=sum(triple[-1] fortripleinself.get_matching_blocks() iftriple[-1] >=m)
return_calculate_ratio(matches, len(self.a) +len(self.b))
However, in my opinion, this is better suited to be something like a cookbook recipe. The number of use cases for this will be low, and there would be little advantage to having this in the stdlib rather than elsewhere.
Activity
gi-ba-bu commentedon Dec 15, 2021
Here I propose a new function, namely .ratio_min(self,m).
.ratio_min(self,m) is an extension of the difflib's function .ratio(self). Equivalently to .ratio(self), .ratio_min(self,m) returns a measure of two sequences' similarity (float in [0,1]). In addition to .ratio(), it can ignore matched substrings if these substrings have length less than a given threshold m. m is the second variable of the function.
It is very useful to avoid spurious high similarity scores.
AlexWaygood commentedon Dec 15, 2021
I am removing 3.10 from the "versions" field, since additions to the standard library are only considered for unreleased versions of Python.
taleinat commentedon Jan 8, 2022
Thanks for the suggestion and the PR, Giacomo!
However, in my opinion, this is better suited to be something like a cookbook recipe. The number of use cases for this will be low, and there would be little advantage to having this in the stdlib rather than elsewhere.
taleinat commentedon Jan 20, 2022
I'm closing this for now since nobody has followed up and to the best of my understanding this wouldn't be an appropriate addition to the stdlib.
This can be re-opened in the future if needed, of course.