Suggest corrections for misspelt conditionals; Better text similarity for strings with errors at the start

This commit is contained in:
Yair Morgenstern
2024-02-27 14:28:08 +02:00
parent 817ac64e58
commit ffeae91b0a
2 changed files with 21 additions and 4 deletions

View File

@ -1,5 +1,7 @@
package com.unciv.models.ruleset.validation package com.unciv.models.ruleset.validation
import kotlin.math.min
/** /**
* Algorithm: * Algorithm:
* - Keep an index for each string. * - Keep an index for each string.
@ -71,5 +73,10 @@ fun getTextDistance(text1: String, text2: String): Int {
return dist return dist
} }
/** @return the [getTextDistance] of two strings relative to their average length. */ /** @return the [getTextDistance] of two strings relative to their average length.
fun getRelativeTextDistance(text1: String, text2: String) = getTextDistance(text1, text2).toDouble() / (text1.length + text2.length) * 2.0 * The original algorithm is very weak to short strings with errors at the start (can't figure out that "on [] tiles" and "in [] tiles" are the same)
* So we run it twice, once with the string reversed */
fun getRelativeTextDistance(text1: String, text2: String): Double{
fun textDistance(a:String, b:String):Double = getTextDistance(a, b).toDouble() / (text1.length + text2.length) * 2.0
return min(textDistance(text1, text2), textDistance(text1.reversed(), text2.reversed()))
}

View File

@ -123,9 +123,19 @@ class UniqueValidator(val ruleset: Ruleset) {
} }
if (conditional.type == null) { if (conditional.type == null) {
var text = "$prefix contains the conditional \"${conditional.text}\"," +
" which is of an unknown type!"
val similarConditionals = UniqueType.values().filter {
getRelativeTextDistance(
it.placeholderText,
conditional.placeholderText
) <= RulesetCache.uniqueMisspellingThreshold
}
if (similarConditionals.isNotEmpty())
text += " May be a misspelling of \""+ similarConditionals.joinToString("\", or \"") { it.text } +"\""
rulesetErrors.add( rulesetErrors.add(
"$prefix contains the conditional \"${conditional.text}\"," + text,
" which is of an unknown type!",
RulesetErrorSeverity.Warning, uniqueContainer RulesetErrorSeverity.Warning, uniqueContainer
) )
return return