Skip to content

bpo-46086: Add ratio_min(self,m) function - #30125

Closed
gi-ba-bu wants to merge 1 commit into
python:3.10from
gi-ba-bu:patch-1
Closed

gi-ba-bu wants to merge 1 commit into
python:3.10from
gi-ba-bu:patch-1

Conversation

@gi-ba-bu

@gi-ba-bu gi-ba-bu commented Dec 15, 2021 •

Copy link
Copy Markdown

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 CODE:

def ratio_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] for triple in self.get_matching_blocks() if triple[-1] >=m)
    return _calculate_ratio(matches, len(self.a) + len(self.b))

https://bugs.python.org/issue46086

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.
@the-knights-who-say-ni

Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@gi-ba-bu

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@taleinat

taleinat commented Jan 8, 2022 •

Copy link
Copy Markdown
Contributor

I've added the "DO-NOT-MERGE" label for now, since this suggestion still requires discussion and acceptance.

Also, note that (if accepted) this change will need to be made only on the main branch, so this PR will need to be changed accordingly or replaced by a new PR.

@taleinat

Copy link
Copy Markdown
Contributor

I'm closing this for now since nobody has followed up(here or on the issue tracker) 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.

@taleinat taleinat closed this Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants