Created
September 14, 2011 15:14
-
-
Save kedarbellare/1216841 to your computer and use it in GitHub Desktop.
Scala implementation of threshold edit distance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def thresholdLevenshtein(_s: String, _t: String, threshold: Int): Int = { | |
val (s, t) = if (_s.length > _t.length) (_s, _t) else (_t, _s) | |
val slen = s.length | |
val tlen = t.length | |
var prev = Array.fill[Int](tlen+1)(Int.MaxValue) | |
var curr = Array.fill[Int](tlen+1)(Int.MaxValue) | |
for (n <- 0 until math.min(tlen+1, threshold+1)) prev(n) = n | |
for (row <- 1 until (slen+1)) { | |
curr(0) = row | |
val min = math.min(tlen+1, math.max(1, row - threshold)) | |
val max = math.min(tlen+1, row + threshold + 1) | |
// println("row[" + row + "] min=" + min + " max=" + max) | |
if (min > 1) curr(min-1) = Int.MaxValue | |
for (col <- min until max) { | |
curr(col) = if (s(row-1) == t(col-1)) prev(col-1) | |
else math.min(prev(col-1), math.min(curr(col-1), prev(col))) + 1 | |
} | |
// println("row[" + row + "]: " + curr.mkString(" ")) | |
prev = curr | |
curr = Array.fill[Int](tlen+1)(Int.MaxValue) | |
} | |
prev(tlen) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment