From 2169ee7a5b2b0fe3dd16c8bf2db2b2c5d0613c9b Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 10 May 2023 19:16:17 +0300 Subject: [PATCH] Tune suggestions --- .../suggestions/ui/SuggestionsWorker.kt | 20 +++++++++++++++---- .../koitharu/kotatsu/utils/ext/StringExt.kt | 4 ++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/koitharu/kotatsu/suggestions/ui/SuggestionsWorker.kt b/app/src/main/java/org/koitharu/kotatsu/suggestions/ui/SuggestionsWorker.kt index 3f136eb26..77d1c8bd1 100644 --- a/app/src/main/java/org/koitharu/kotatsu/suggestions/ui/SuggestionsWorker.kt +++ b/app/src/main/java/org/koitharu/kotatsu/suggestions/ui/SuggestionsWorker.kt @@ -122,7 +122,7 @@ class SuggestionsWorker @AssistedInject constructor( if (seed.isEmpty() || sources.isEmpty()) { return 0 } - val tagsBlacklist = TagsBlacklist(appSettings.suggestionsTagsBlacklist, 0.3f) + val tagsBlacklist = TagsBlacklist(appSettings.suggestionsTagsBlacklist, TAG_EQ_THRESHOLD) val tags = seed.flatMap { it.tags.map { x -> x.title } }.takeMostFrequent(10) val producer = channelFlow { @@ -149,7 +149,9 @@ class SuggestionsWorker @AssistedInject constructor( val manga = suggestions[Random.nextInt(0, suggestions.size / 3)] val details = mangaRepositoryFactory.create(manga.manga.source) .getDetails(manga.manga) - showNotification(details) + if (details !in tagsBlacklist) { + showNotification(details) + } }.onFailure { it.printStackTraceDebug() } @@ -167,7 +169,7 @@ class SuggestionsWorker @AssistedInject constructor( val order = preferredSortOrders.first { it in availableOrders } val availableTags = repository.getTags() val tag = tags.firstNotNullOfOrNull { title -> - availableTags.find { x -> x.title.almostEquals(title, threshold = 0.3f) } + availableTags.find { x -> x.title.almostEquals(title, TAG_EQ_THRESHOLD) } } val list = repository.getList(0, setOfNotNull(tag), order).asArrayList() if (appSettings.isSuggestionsExcludeNsfw) { @@ -270,12 +272,21 @@ class SuggestionsWorker @AssistedInject constructor( private fun computeRelevance(mangaTags: Set, allTags: List): Float { val maxWeight = (allTags.size + allTags.size + 1 - mangaTags.size) * mangaTags.size / 2.0 val weight = mangaTags.sumOf { tag -> - val index = allTags.indexOf(tag.title) + val index = allTags.inexactIndexOf(tag.title, TAG_EQ_THRESHOLD) if (index < 0) 0 else allTags.size - index } return (weight / maxWeight).pow(2.0).toFloat() } + private fun Iterable.inexactIndexOf(element: String, threshold: Float): Int { + forEachIndexed { i, t -> + if (t.almostEquals(element, threshold)) { + return i + } + } + return -1 + } + companion object { private const val TAG = "suggestions" @@ -286,6 +297,7 @@ class SuggestionsWorker @AssistedInject constructor( private const val WORKER_NOTIFICATION_ID = 36 private const val MAX_RESULTS = 80 private const val MAX_RAW_RESULTS = 200 + private const val TAG_EQ_THRESHOLD = 0.4f private val preferredSortOrders = listOf( SortOrder.UPDATED, diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/ext/StringExt.kt b/app/src/main/java/org/koitharu/kotatsu/utils/ext/StringExt.kt index bfb763325..5e4056a5d 100644 --- a/app/src/main/java/org/koitharu/kotatsu/utils/ext/StringExt.kt +++ b/app/src/main/java/org/koitharu/kotatsu/utils/ext/StringExt.kt @@ -29,8 +29,8 @@ fun String.toUUIDOrNull(): UUID? = try { */ fun String.almostEquals(other: String, @FloatRange(from = 0.0) threshold: Float): Boolean { if (threshold == 0f) { - return equals(other) + return equals(other, ignoreCase = true) } - val diff = levenshteinDistance(other) / ((length + other.length) / 2f) + val diff = lowercase().levenshteinDistance(other.lowercase()) / ((length + other.length) / 2f) return diff < threshold }