
.10.dp.source-code.alg.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core_2.10 Show documentation
Show all versions of core_2.10 Show documentation
Core drx utilities that have no other dependencies other than the core scala lib
The newest version!
/*
Copyright 2010 Aaron J. Radke
Licensed 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.
*/
package cc.drx
object Alg{
//https://gist.github.com/tixxit/1246894
def levenshteinUnlimited[A](a: Iterable[A], b: Iterable[A]):Int = a.foldLeft((0 to b.size).toList)((prev, x) =>
(prev zip prev.tail zip b).scanLeft(prev.head + 1) {
case (h, ((d, v), y)) => math.min(math.min(h + 1, v + 1), d + (if (x == y) 0 else 1))
}).last
def levenshtein[A](a: Iterable[A], b: Iterable[A], maxDistance:Int=100):Int= {
val diff = math.abs(a.size - b.size)
if(diff > maxDistance) maxDistance else levenshteinUnlimited(a,b)
}
}