mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-16 02:40:41 +07:00
Suggest corrections for misspelt conditionals; Better text similarity for strings with errors at the start
This commit is contained in:
@ -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()))
|
||||||
|
}
|
||||||
|
@ -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
|
||||||
|
Reference in New Issue
Block a user