Skip to content

Commit

Permalink
Fix MediaListFilters.ContainsSubjectName, fix #1358
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Dec 22, 2024
1 parent 6f8a8ea commit 775e509
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,19 @@ object MediaListFilters {
fun exactlyContains() = originalTitle
.contains(subjectName, ignoreCase = true)

fun fuzzyMatches() = StringMatcher.calculateMatchRate(originalTitle, subjectName) >= 0.8
fun fuzzyMatches() = StringMatcher.calculateMatchRate(originalTitle, subjectName) >= 80

// println(
// when {
// exactlyContains() -> "'$originalTitle' included because exactlyContains()"
// fuzzyMatches() -> "'$originalTitle' included because fuzzyMatches() at " + StringMatcher.calculateMatchRate(
// originalTitle,
// subjectName,
// )
//
// else -> {}
// },
// )
exactlyContains() || fuzzyMatches()
}
}
Expand Down Expand Up @@ -77,7 +88,7 @@ object MediaListFilters {
put("", "1")
}
private val allNumbersRegex = numberMappings.keys.joinToString("|").toRegex()
private val specialCharRegex = Regex("""[~!@#$%^&*()_+{}\[\]\\|;':",.<>/?【】:~「」!]""")
private val toDelete = Regex("""[~!@#$%^&*()_+{}\[\]\\|;':",.<>/?【】:~「」!]""")
private val replaceWithWhitespace = Regex("""[。、,]""")
private val whitespaceRegex = Regex("""[ \s+]""")

Expand Down Expand Up @@ -118,7 +129,7 @@ object MediaListFilters {
deleteInfix("剧场版")
deletePrefix("OVA")
deleteInfix("OVA")
deleteMatches(specialCharRegex)
deleteMatches(toDelete)
if (replaceNumbers) {
replaceMatches(allNumbersRegex) { numberMappings.getValue(it.value) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

package me.him188.ani.app.domain.mediasource

import me.him188.ani.datasources.api.EpisodeSort
import me.him188.ani.datasources.api.topic.EpisodeRange
import me.him188.ani.test.TestFactory
import me.him188.ani.test.runDynamicTests
import kotlin.test.assertEquals
Expand Down Expand Up @@ -93,6 +95,47 @@ class MediaListFiltersTest {
}
}

@TestFactory
fun `test ContainsSubjectName`() = runDynamicTests {
fun case(title: String, subjectName: String, expected: Boolean) {
add("$title matches $subjectName") {
val context = MediaListFilterContext(
subjectNames = setOf(subjectName),
episodeSort = EpisodeSort(1),
null, null,
)
context.run {
assertEquals(
expected,
MediaListFilters.ContainsSubjectName.applyOn(
object : MediaListFilter.Candidate {
override val originalTitle: String get() = title
override val episodeRange: EpisodeRange? get() = null
},
),
)
}
}
}

// subject matches title
infix fun String.matches(title: String) {
case(title, this, true)
}

// subject matches title
infix fun String.mismatches(title: String) {
case(title, this, false)
}

"哥特萝莉侦探事件簿" matches "哥特萝莉侦探事件簿"
"哥特萝莉侦探事件薄" matches "哥特萝莉侦探事件簿"
"败犬女主太多了" matches "败犬女主太多啦"

"地狱少女第一季" mismatches "地。 ―关于地球的运动―"
"地狱少女" mismatches "地。"
}

private fun removeSpecials(
string: String,
removeWhitespace: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package me.him188.ani.app.domain.mediasource

import me.him188.ani.app.domain.mediasource.MediaListFilters.removeSpecials
import me.him188.ani.test.TestFactory
import me.him188.ani.test.runDynamicTests
import kotlin.test.Test
Expand Down Expand Up @@ -85,6 +86,16 @@ class StringMatcherTest {

// 此方法无法识别这种区别
"别当欧尼酱了" matches "别当哥哥了" at 50

"地狱少女第一季" matches "地。 ―关于地球的运动―" at 8
"地狱少女第一季" matches "地。" at 14
val chiRemoved = removeSpecials(
"地。 ―关于地球的运动―",
removeWhitespace = true,
replaceNumbers = true,
)
"地狱少女第一季" matches chiRemoved at 12
"地狱少女" matches "地。" at 25
}

@Test
Expand Down

0 comments on commit 775e509

Please sign in to comment.