diff --git a/build.gradle.kts b/build.gradle.kts index e052ec7..da192b1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "com.londogard" -version = "1.0-beta" +version = "1.0.1-beta" repositories { mavenCentral() diff --git a/src/main/kotlin/com/londogard/fuzzymatch/FuzzyMatcher.kt b/src/main/kotlin/com/londogard/fuzzymatch/FuzzyMatcher.kt index f54a597..0f2d7bf 100644 --- a/src/main/kotlin/com/londogard/fuzzymatch/FuzzyMatcher.kt +++ b/src/main/kotlin/com/londogard/fuzzymatch/FuzzyMatcher.kt @@ -62,7 +62,7 @@ class FuzzyMatcher(private val scoreConfig: ScoreConfig = ScoreConfig()) { } val results = recursiveParams.recursiveResults + Result(recursiveParams.matches, 10) - if (results.isEmpty() || recursiveParams.textLeft.isEmpty() && !pattern.last().equals(text.last(), ignoreCase = true)) emptyResult + if (recursiveParams.matches.size != pattern.length) emptyResult else results.filter { it.score > 0 }.map { Result(it.indices, scoringFunction(it.indices, fullText)) }.maxBy { it.score }?.copy(text = fullText)!! } } @@ -76,10 +76,10 @@ class FuzzyMatcher(private val scoreConfig: ScoreConfig = ScoreConfig()) { */ fun fuzzyMatch(texts: List, pattern: String, topN: Int = 20): List = texts - .asSequence() .map { fuzzyMatchFunc(it, pattern) } + .filter { it.score > 0 } .sortedByDescending { it.score } - .take(topN).toList() + .take(topN) private fun scoringFunction(indices: List, text: String): Int { return listOf( @@ -90,10 +90,10 @@ class FuzzyMatcher(private val scoreConfig: ScoreConfig = ScoreConfig()) { val neighbour = text[indexWindow.first()] val camelCase = if (neighbour.isLowerCase() && text[indexWindow.last()].isUpperCase()) scoreConfig.camelCaseMatch else 0 val separator = if (neighbour == ' ' || neighbour == '_') scoreConfig.separatorMatch else 0 - val unmatched = (text.length - indices.size) * scoreConfig.unmatchedLetter + val unmatched = (indices.lastOrNull() ?: text.length) * scoreConfig.unmatchedLetter firstLetter + consecutive + camelCase + separator + unmatched }.sum() ).sum() } -} +} \ No newline at end of file