Skip to content

Commit

Permalink
Levenshtein: change how ratio is computed
Browse files Browse the repository at this point in the history
Don't subtract the length difference from distance, as that leads to low
numbers (frequently, zero) on pairs which are quite dissimilar. Instead,
divide by the length of the longer of the two.
  • Loading branch information
kitbellew committed May 23, 2021
1 parent d47046d commit 05ae159
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ object Levenshtein {
candidates: Seq[String]
): Option[String] = {
val candidatesWithRatio = candidates.flatMap { candidate =>
val minDifference = math.abs(query.length() - candidate.length())
val difference = distance(candidate)(query).toDouble - minDifference
val ratio = difference.toDouble /
math.min(query.length(), candidate.length())
val levDist = distance(candidate)(query).toDouble
val ratio = levDist / math.max(query.length(), candidate.length())
if (ratio < 0.4) Some((candidate, ratio))
else None // Don't return candidate when difference is large.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,55 +287,51 @@ class CliParserSuite extends BaseCliParserSuite {
"skip hidden",
"--hidden1" :: "10" :: Nil,
"""|found argument '--hidden1' which wasn't expected, or isn't valid in this context.
| Did you mean '--in'?
|""".stripMargin
)

checkError(
"match decoding",
"--decoding" :: "10" :: Nil,
"""|found argument '--decoding' which wasn't expected, or isn't valid in this context.
| Did you mean '--in'?
| Did you mean '--encoding'?
|""".stripMargin
)

checkError(
"match decodung",
"--decodung" :: "10" :: Nil,
"""|found argument '--decodung' which wasn't expected, or isn't valid in this context.
| Did you mean '--conf'?
| Did you mean '--encoding'?
|""".stripMargin
)

checkError(
"match decodingg",
"--decodingg" :: "10" :: Nil,
"""|found argument '--decodingg' which wasn't expected, or isn't valid in this context.
| Did you mean '--in'?
| Did you mean '--encoding'?
|""".stripMargin
)

checkError(
"match decoddung",
"--decoddung" :: "10" :: Nil,
"""|found argument '--decoddung' which wasn't expected, or isn't valid in this context.
| Did you mean '--conf'?
|""".stripMargin
)

checkError(
"match decodding1",
"--decodding1" :: "10" :: Nil,
"""|found argument '--decodding1' which wasn't expected, or isn't valid in this context.
| Did you mean '--in'?
|""".stripMargin
)

checkError(
"match decodunk",
"--decodunk" :: "10" :: Nil,
"""|found argument '--decodunk' which wasn't expected, or isn't valid in this context.
| Did you mean '--conf'?
|""".stripMargin
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ class SubcommandSuite extends FunSuite {
checkError(
List("test", "--max=40"),
"""|error: found argument '--max' which wasn't expected, or isn't valid in this context.
| Did you mean '--max-count'?
|""".stripMargin
)
checkError(
Expand Down

0 comments on commit 05ae159

Please sign in to comment.