Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Commons-lang has a pretty fast implementation. See <a href="http://web.archive.org/web/20120526085419/http://www.merriampark.com/ldjava.htm" rel="nofollow noreferrer">http://web.archive.org/web/20120526085419/http://www.merriampark.com/ldjava.htm</a>.</p> <p>Here's my translation of that into Scala:</p> <pre class="lang-scala prettyprint-override"><code>// The code below is based on code from the Apache Commons lang project. /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance with the * License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ /** * assert(levenshtein("algorithm", "altruistic")==6) * assert(levenshtein("1638452297", "444488444")==9) * assert(levenshtein("", "") == 0) * assert(levenshtein("", "a") == 1) * assert(levenshtein("aaapppp", "") == 7) * assert(levenshtein("frog", "fog") == 1) * assert(levenshtein("fly", "ant") == 3) * assert(levenshtein("elephant", "hippo") == 7) * assert(levenshtein("hippo", "elephant") == 7) * assert(levenshtein("hippo", "zzzzzzzz") == 8) * assert(levenshtein("hello", "hallo") == 1) * */ def levenshtein(s: CharSequence, t: CharSequence, max: Int = Int.MaxValue) = { import scala.annotation.tailrec def impl(s: CharSequence, t: CharSequence, n: Int, m: Int) = { // Inside impl n &lt;= m! val p = new Array[Int](n + 1) // 'previous' cost array, horizontally val d = new Array[Int](n + 1) // cost array, horizontally @tailrec def fillP(i: Int) { p(i) = i if (i &lt; n) fillP(i + 1) } fillP(0) @tailrec def eachJ(j: Int, t_j: Char, d: Array[Int], p: Array[Int]): Int = { d(0) = j @tailrec def eachI(i: Int) { val a = d(i - 1) + 1 val b = p(i) + 1 d(i) = if (a &lt; b) a else { val c = if (s.charAt(i - 1) == t_j) p(i - 1) else p(i - 1) + 1 if (b &lt; c) b else c } if (i &lt; n) eachI(i + 1) } eachI(1) if (j &lt; m) eachJ(j + 1, t.charAt(j), p, d) else d(n) } eachJ(1, t.charAt(0), d, p) } val n = s.length val m = t.length if (n == 0) m else if (m == 0) n else { if (n &gt; m) impl(t, s, m, n) else impl(s, t, n, m) } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload