use fuzzykot library
This commit is contained in:
+4
-16
@@ -1,5 +1,6 @@
|
||||
package ru.shadowsparky.vbox.backend.data
|
||||
|
||||
import com.github.terrakok.fuzzykot.Levenshtein
|
||||
import org.koin.core.annotation.Single
|
||||
import ru.shadowsparky.vbox.shared.domain.model.VideoItem
|
||||
|
||||
@@ -12,9 +13,9 @@ class FuzzyMovieSearchFilter {
|
||||
return original.map { movie ->
|
||||
val titleNormalized = movie.title.normalize()
|
||||
if (titleNormalized.contains(query)) {
|
||||
return@map Pair(movie, 1.0)
|
||||
return@map Pair(movie, 100)
|
||||
}
|
||||
val score = titleNormalized.fuzzySimilarity(query)
|
||||
val score = Levenshtein.tokenSortRatio(titleNormalized, query)
|
||||
Pair(movie, score)
|
||||
}.filter { it.second >= THRESHOLD }.sortedByDescending { it.second }.map { it.first }
|
||||
}
|
||||
@@ -47,20 +48,7 @@ class FuzzyMovieSearchFilter {
|
||||
}
|
||||
}
|
||||
|
||||
private fun String.fuzzySimilarity(other: String): Double {
|
||||
if (this == other) return 1.0
|
||||
if (this.isBlank() || other.isBlank()) return 0.0
|
||||
val s1Pairs = this.windowed(2)
|
||||
val s2Pairs = other.windowed(2).toMutableList()
|
||||
if (s1Pairs.isEmpty() || s2Pairs.isEmpty()) return 0.0
|
||||
var matches = 0
|
||||
for (pair in s1Pairs) {
|
||||
if (s2Pairs.remove(pair)) { matches++ }
|
||||
}
|
||||
return (2.0 * matches) / (s1Pairs.size + other.windowed(2).size)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val THRESHOLD: Double = 0.45
|
||||
const val THRESHOLD = 55
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user