Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:34:00+00:00 2026-06-17T04:34:00+00:00

According to the python-Levenshtein.ratio source: https://github.com/miohtama/python-Levenshtein/blob/master/Levenshtein.c#L722 it’s computed as (lensum – ldist) / lensum

  • 0

According to the python-Levenshtein.ratio source:

https://github.com/miohtama/python-Levenshtein/blob/master/Levenshtein.c#L722

it’s computed as (lensum - ldist) / lensum. This works for

# pip install python-Levenshtein
import Levenshtein
Levenshtein.distance('ab', 'a') # returns 1
Levenshtein.ratio('ab', 'a')    # returns 0.666666

However, it seems to break with

Levenshtein.distance('ab', 'ac')  # returns 1
Levenshtein.ratio('ab', 'ac')     # returns 0.5

I feel I must be missing something very simple.. but why not 0.75?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:34:01+00:00Added an answer on June 17, 2026 at 4:34 am

    By looking more carefully at the C code, I found that this apparent contradiction is due to the fact that ratio treats the “replace” edit operation differently than the other operations (i.e. with a cost of 2), whereas distance treats them all the same with a cost of 1.

    This can be seen in the calls to the internal levenshtein_common function made within ratio_py function:


    https://github.com/miohtama/python-Levenshtein/blob/master/Levenshtein.c#L727

    static PyObject*
    ratio_py(PyObject *self, PyObject *args)
    {
      size_t lensum;
      long int ldist;
    
      if ((ldist = levenshtein_common(args, "ratio", 1, &lensum)) < 0) //Call
        return NULL;
    
      if (lensum == 0)
        return PyFloat_FromDouble(1.0);
    
      return PyFloat_FromDouble((double)(lensum - ldist)/(lensum));
    }
    

    and by distance_py function:

    https://github.com/miohtama/python-Levenshtein/blob/master/Levenshtein.c#L715

    static PyObject*
    distance_py(PyObject *self, PyObject *args)
    {
      size_t lensum;
      long int ldist;
    
      if ((ldist = levenshtein_common(args, "distance", 0, &lensum)) < 0)
        return NULL;
    
      return PyInt_FromLong((long)ldist);
    }
    

    which ultimately results in different cost arguments being sent to another internal function, lev_edit_distance, which has the following doc snippet:

    @xcost: If nonzero, the replace operation has weight 2, otherwise all
            edit operations have equal weights of 1.
    

    Code of lev_edit_distance():

    /**
     * lev_edit_distance:
     * @len1: The length of @string1.
     * @string1: A sequence of bytes of length @len1, may contain NUL characters.
     * @len2: The length of @string2.
     * @string2: A sequence of bytes of length @len2, may contain NUL characters.
     * @xcost: If nonzero, the replace operation has weight 2, otherwise all
     *         edit operations have equal weights of 1.
     *
     * Computes Levenshtein edit distance of two strings.
     *
     * Returns: The edit distance.
     **/
    _LEV_STATIC_PY size_t
    lev_edit_distance(size_t len1, const lev_byte *string1,
                      size_t len2, const lev_byte *string2,
                      int xcost)
    {
      size_t i;
    

    [ANSWER]

    So in my example,

    ratio('ab', 'ac') implies a replacement operation (cost of 2), over the total length of the strings (4), hence 2/4 = 0.5.

    That explains the “how”, I guess the only remaining aspect would be the “why”, but for the moment I’m satisfied with this understanding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to https://developers.google.com/appengine/docs/python/config/cron cron jobs can run for 10 minutes. However, when I try
According to http://www.php2python.com/wiki/function.preg-replace-callback/ re.sub is the python equivlant of PHP's preg_replace_callback, but the php
According to this post: https://groups.google.com/forum/?fromgroups#!topic/kivy-users/n7c3thksnzg , it is possible to use Eclipse as an
For example, Marshal is still parsing the input data, according to Python source. .....
According to this: http://code.activestate.com/lists/python-list/413540/ , tokenize.generate_tokens should be used and not tokenize.tokenize . This
According to the Python 2.6.5 docs [1], the bsddb module has been deprecated for
So according to the Zen of Python ... Explicit is better than implicit ...
According to Jython's documentation : Jython is an implementation of the Python language for
I am trying to overload operators of a C++ class using Boost.Python. According to
According to this: http://www.codeplex.com/IronPython/Wiki/View.aspx?title=IP20VsCPy25Perf&referringTitle=IronPython%20Performance IronPython (Python for .Net) is faster than regular Python (cPython)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.