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 6120105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:37:49+00:00 2026-05-23T15:37:49+00:00

I am trying to create or find a CoffeeScript implementation of the Levenshtein Distance

  • 0

I am trying to create or find a CoffeeScript implementation of the Levenshtein Distance formula, aka Edit Distance. Here is what I have so far, any help at all would be much appreciated.

levenshtein = (s1,s2) ->
    n = s1.length
    m = s2.length
    if n < m
        return levenshtein(s2, s1) 
    if not s1 
        return s2.length
    previous_row = [s2.length + 1]
    for c1, i in s1
        current_row = [i + 1]
        for c2, j in s2
            insertions = previous_row[j + 1] + 1
            deletions = current_row[j] + 1
            substitutions = previous_row[j] # is this unnescessary?-> (c1 != c2)
            current_row.push(Math.min(insertions,deletions,substitutions))
        previous_row = current_row
    return previous_row[previous_row.length-1]
#End Levenshetein Function

Btw: I know this code is wrong on many levels, I am happy to receive any and all constructive criticism. Just looking to improve, and figure out this formula!

CodeEdit1: Patched up the errors Trevor pointed out, current code above includes those changes

Update: The question I am asking is – how do we do Levenshtein in CoffeeScript?

Here is the ‘steps’ for the Levenshtein Distance Algorithm to help you see what I am trying to accomplish.

Steps
1
Set n to be the length of s.
Set m to be the length of t.
If n = 0, return m and exit.
If m = 0, return n and exit.
Construct a matrix containing 0..m rows and 0..n columns.

2
Initialize the first row to 0..n.
Initialize the first column to 0..m.

3 Examine each character of s (i from 1 to n).

4 Examine each character of t (j from 1 to m).

5 If s[i] equals t[j], the cost is 0.
If s[i] doesn’t equal t[j], the cost is 1.

6 Set cell d[i,j] of the matrix equal to the minimum of:
a. The cell immediately above plus 1: d[i-1,j] + 1.
b. The cell immediately to the left plus 1: d[i,j-1] + 1.
c. The cell diagonally above and to the left plus the cost: d[i-1,j-1] + cost.

7 After the iteration steps (3, 4, 5, 6) are complete, the distance is found in cell d[n,m].

source:http://www.merriampark.com/ld.htm

  • 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-05-23T15:37:50+00:00Added an answer on May 23, 2026 at 3:37 pm

    This page (linked to from the resource you mentioned) offers a JavaScript implementation of the Levenshtein distance algorithm. Based on both that and the code you posted, here’s my CoffeeScript version:

    LD = (s, t) ->
      n = s.length
      m = t.length
      return m if n is 0
      return n if m is 0
    
      d       = []
      d[i]    = [] for i in [0..n]
      d[i][0] = i  for i in [0..n]
      d[0][j] = j  for j in [0..m]
    
      for c1, i in s
        for c2, j in t
          cost = if c1 is c2 then 0 else 1
          d[i+1][j+1] = Math.min d[i][j+1]+1, d[i+1][j]+1, d[i][j] + cost
    
      d[n][m]
    

    It seems to hold up to light testing, but let me know if there are any problems.

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

Sidebar

Related Questions

I have been trying to find a good guide to create a templated control.
I'm trying to create a jQuery Slider like this . I can't find any
I am trying to create an app that will help to find nearby places
I'm trying to create a custom decorator in Django but I couldn't find any
I couldn't find any information pertaining to this question. I am trying to create
I'm trying to create a cmdlet with the name Invoke-Something. But I cannot find
I am trying to create a customized UISearchBar. I am not able to find
Trying to find the best way of create an overlap/overlay layer to fill the
Im trying to find out a good JavaScript library that can create a nice
I am trying to find the best option to create a simple progress bar

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.