Tune suggestions
This commit is contained in:
@@ -122,7 +122,7 @@ class SuggestionsWorker @AssistedInject constructor(
|
|||||||
if (seed.isEmpty() || sources.isEmpty()) {
|
if (seed.isEmpty() || sources.isEmpty()) {
|
||||||
return 0
|
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 tags = seed.flatMap { it.tags.map { x -> x.title } }.takeMostFrequent(10)
|
||||||
|
|
||||||
val producer = channelFlow {
|
val producer = channelFlow {
|
||||||
@@ -149,7 +149,9 @@ class SuggestionsWorker @AssistedInject constructor(
|
|||||||
val manga = suggestions[Random.nextInt(0, suggestions.size / 3)]
|
val manga = suggestions[Random.nextInt(0, suggestions.size / 3)]
|
||||||
val details = mangaRepositoryFactory.create(manga.manga.source)
|
val details = mangaRepositoryFactory.create(manga.manga.source)
|
||||||
.getDetails(manga.manga)
|
.getDetails(manga.manga)
|
||||||
showNotification(details)
|
if (details !in tagsBlacklist) {
|
||||||
|
showNotification(details)
|
||||||
|
}
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
it.printStackTraceDebug()
|
it.printStackTraceDebug()
|
||||||
}
|
}
|
||||||
@@ -167,7 +169,7 @@ class SuggestionsWorker @AssistedInject constructor(
|
|||||||
val order = preferredSortOrders.first { it in availableOrders }
|
val order = preferredSortOrders.first { it in availableOrders }
|
||||||
val availableTags = repository.getTags()
|
val availableTags = repository.getTags()
|
||||||
val tag = tags.firstNotNullOfOrNull { title ->
|
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()
|
val list = repository.getList(0, setOfNotNull(tag), order).asArrayList()
|
||||||
if (appSettings.isSuggestionsExcludeNsfw) {
|
if (appSettings.isSuggestionsExcludeNsfw) {
|
||||||
@@ -270,12 +272,21 @@ class SuggestionsWorker @AssistedInject constructor(
|
|||||||
private fun computeRelevance(mangaTags: Set<MangaTag>, allTags: List<String>): Float {
|
private fun computeRelevance(mangaTags: Set<MangaTag>, allTags: List<String>): Float {
|
||||||
val maxWeight = (allTags.size + allTags.size + 1 - mangaTags.size) * mangaTags.size / 2.0
|
val maxWeight = (allTags.size + allTags.size + 1 - mangaTags.size) * mangaTags.size / 2.0
|
||||||
val weight = mangaTags.sumOf { tag ->
|
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
|
if (index < 0) 0 else allTags.size - index
|
||||||
}
|
}
|
||||||
return (weight / maxWeight).pow(2.0).toFloat()
|
return (weight / maxWeight).pow(2.0).toFloat()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun Iterable<String>.inexactIndexOf(element: String, threshold: Float): Int {
|
||||||
|
forEachIndexed { i, t ->
|
||||||
|
if (t.almostEquals(element, threshold)) {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
private const val TAG = "suggestions"
|
private const val TAG = "suggestions"
|
||||||
@@ -286,6 +297,7 @@ class SuggestionsWorker @AssistedInject constructor(
|
|||||||
private const val WORKER_NOTIFICATION_ID = 36
|
private const val WORKER_NOTIFICATION_ID = 36
|
||||||
private const val MAX_RESULTS = 80
|
private const val MAX_RESULTS = 80
|
||||||
private const val MAX_RAW_RESULTS = 200
|
private const val MAX_RAW_RESULTS = 200
|
||||||
|
private const val TAG_EQ_THRESHOLD = 0.4f
|
||||||
|
|
||||||
private val preferredSortOrders = listOf(
|
private val preferredSortOrders = listOf(
|
||||||
SortOrder.UPDATED,
|
SortOrder.UPDATED,
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ fun String.toUUIDOrNull(): UUID? = try {
|
|||||||
*/
|
*/
|
||||||
fun String.almostEquals(other: String, @FloatRange(from = 0.0) threshold: Float): Boolean {
|
fun String.almostEquals(other: String, @FloatRange(from = 0.0) threshold: Float): Boolean {
|
||||||
if (threshold == 0f) {
|
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
|
return diff < threshold
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user