Łukasz Bednarczyk
04/14/2023, 6:50 AMsealed class EmbeddedRule : NormalizationRule {
object SingleSpaceRule : EmbeddedRule() {
override val ruleName: String
get() = "single_space_rule"
override fun normalize(value: String): Result<String> {
val charIterator = value.iterator()
val output = StringBuilder("")
while (charIterator.hasNext()) {
val currentChar = charIterator.nextChar()
if (output.isEmpty())
output.append(currentChar)
else
if (output.last().code != CharDirectionality.WHITESPACE.value && currentChar.code != CharDirectionality.WHITESPACE.value)
output.append(currentChar)
}
return Result.success(output.toString())
}
}
companion object {
fun values(): List<EmbeddedRule> {
return EmbeddedRule::class.sealedSubclasses.mapNotNull { it.objectInstance }
}
fun findByRuleName(name: String): Result<EmbeddedRule> {
return when (name) {
UpperCase.ruleName -> Result.success(UpperCase)
else -> Result.failure(IllegalArgumentException(name))
}
}
}
}
My test code and exception:
context("SingleSpaceRule") {
describe(".normalize") {
it("returns only 1 spaces") {
val result = EmbeddedRule.SingleSpaceRule.normalize(" ")
result.isSuccess
result.getOrThrow() == " "
}
}
}
io.kotest.core.SpecInstantiationException: Could not create instance of class com.vascoelectronics.nlptool.normalizetext.domain.models.rules.EmbeddedRuleSpec
sam
04/14/2023, 2:25 PMLeoColman
04/14/2023, 5:37 PMŁukasz Bednarczyk
04/16/2023, 6:25 PM