Skip to content

Commit

Permalink
Levenshtein: as fallback, look for prefix option
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed May 23, 2021
1 parent 05ae159 commit cf69d53
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ object Levenshtein {
else None // Don't return candidate when difference is large.
}
val result = candidatesWithRatio.sortBy(_._2).headOption.map(_._1)
result
result.orElse(prefixCandidate(query, candidates))
}

private def prefixCandidate(
query: String,
candidates: Seq[String]
): Option[String] = {
val prefixCandidates = candidates.flatMap { candidate =>
if (candidate.startsWith(query)) Some(candidate) else None
}
Option(prefixCandidates).filter(_.length == 1).map(_.head)
}

/** Levenshtein distance. Implementation based on Wikipedia's algorithm. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ 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 cf69d53

Please sign in to comment.