closestMatch

fun Collection<String>.closestMatch(input: String, maxDistance: Int = 3): String?

Finds the element in this collection that is closest to input by Levenshtein distance, provided the distance does not exceed maxDistance.

This is used by the Deserializer to suggest corrections for unknown YAML keys. When multiple candidates tie at the same distance, the first one encountered (iteration order) is returned.

Examples:

val keys = setOf("database", "server", "logging")

keys.closestMatch("databse") // "database" (distance 1)
keys.closestMatch("srvr") // "server" (distance 2)
keys.closestMatch("completelyWrong") // null (distance 3)
keys.closestMatch("srvr", maxDistance = 1) // null (distance 2 maxDistance 1)

Return

The closest matching string, or null if no element is within maxDistance.

Since

1.0

Parameters

input

The string to match against elements in this collection.

maxDistance

The maximum Levenshtein distance to accept. Candidates with a greater distance are discarded. Defaults to 3.

See also