Skip to content
Browse files

treat case differences differently

  • Loading branch information...
1 parent 4561606 commit dd6120a7023b258dcb4724319ffdfc8c0e35dcee @harrah committed Dec 12, 2011
Showing with 5 additions and 3 deletions.
  1. +1 −1 main/Command.scala
  2. +4 −2 util/complete/EditDistance.scala
View
2 main/Command.scala
@@ -131,7 +131,7 @@ object Command
def suggestions(a: String, bs: Seq[String], maxDistance: Int = 3, maxSuggestions: Int = 3): Seq[String] =
bs.map { b => (b, distance(a, b) ) } filter (_._2 <= maxDistance) sortBy(_._2) take(maxSuggestions) map(_._1)
def distance(a: String, b: String): Int =
- EditDistance.levenshtein(a, b, insertCost = 1, deleteCost = 1, subCost = 2, transposeCost = 1, matchCost = -1, true)
+ EditDistance.levenshtein(a, b, insertCost = 1, deleteCost = 1, subCost = 2, transposeCost = 1, matchCost = -1, caseCost = 1, true)
def spacedAny(name: String): Parser[String] = spacedC(name, any)
def spacedC(name: String, c: Parser[Char]): Parser[String] =
View
6 util/complete/EditDistance.scala
@@ -1,12 +1,14 @@
package sbt.complete
+ import java.lang.Character.{toLowerCase => lower}
+
/** @author Paul Phillips*/
object EditDistance {
/** Translated from the java version at
* http://www.merriampark.com/ld.htm
* which is declared to be public domain.
*/
- def levenshtein(s: String, t: String, insertCost: Int = 1, deleteCost: Int = 1, subCost: Int = 1, transposeCost: Int = 1, matchCost: Int = 0, transpositions: Boolean = false): Int = {
+ def levenshtein(s: String, t: String, insertCost: Int = 1, deleteCost: Int = 1, subCost: Int = 1, transposeCost: Int = 1, matchCost: Int = 0, caseCost: Int = 1, transpositions: Boolean = false): Int = {
val n = s.length
val m = t.length
if (n == 0) return m
@@ -18,7 +20,7 @@ object EditDistance {
for (i <- 1 to n ; val s_i = s(i - 1) ; j <- 1 to m) {
val t_j = t(j - 1)
- val cost = if (s_i == t_j) matchCost else subCost
+ val cost = if (s_i == t_j) matchCost else if(lower(s_i) == lower(t_j)) caseCost else subCost
val tcost = if (s_i == t_j) matchCost else transposeCost

0 comments on commit dd6120a

Please sign in to comment.
Something went wrong with that request. Please try again.