Skip to content

Commit

Permalink
Updated scoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Lundez committed Dec 11, 2019
1 parent 08e20d4 commit 0b84541
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.londogard"
version = "1.0-beta"
version = "1.0.1-beta"

repositories {
mavenCentral()
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/com/londogard/fuzzymatch/FuzzyMatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)!!
}
}
Expand All @@ -76,10 +76,10 @@ class FuzzyMatcher(private val scoreConfig: ScoreConfig = ScoreConfig()) {
*/
fun fuzzyMatch(texts: List<String>, pattern: String, topN: Int = 20): List<Result> =
texts
.asSequence()
.map { fuzzyMatchFunc(it, pattern) }
.filter { it.score > 0 }
.sortedByDescending { it.score }
.take(topN).toList()
.take(topN)

private fun scoringFunction(indices: List<Int>, text: String): Int {
return listOf(
Expand All @@ -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()
}
}
}

0 comments on commit 0b84541

Please sign in to comment.