music: trim simple names after punct removal

Trim simple names once punctuation has been removed.

This prevents situations where album names like "& Yet & Yet" (a real
album by post-rock outfit Do Make Say Think) will have blank thumbs.
This probably isn't the best approach in general, but nothing about the
intelligent name system is a good approach.
This commit is contained in:
Alexander Capehart 2023-08-18 11:47:50 -06:00
parent 5cd46193d5
commit 2c2bd79ae2
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
3 changed files with 11 additions and 119 deletions

View file

@ -148,6 +148,8 @@ sealed interface Name : Comparable<Name> {
private val collator: Collator = Collator.getInstance().apply { strength = Collator.PRIMARY }
private val punctRegex by lazy { Regex("[\\p{Punct}+]") }
// TODO: Consider how you want to handle whitespace and "gaps" in names.
/**
* Plain [Name.Known] implementation that is internationalization-safe.
*
@ -159,7 +161,7 @@ private data class SimpleKnownName(override val raw: String, override val sort:
private fun parseToken(name: String): SortToken {
// Remove excess punctuation from the string, as those usually aren't considered in sorting.
val stripped = name.replace(punctRegex, "").ifEmpty { name }
val stripped = name.replace(punctRegex, "").trim().ifEmpty { name }
val collationKey = collator.getCollationKey(stripped)
// Always use lexicographic mode since we aren't parsing any numeric components
return SortToken(collationKey, SortToken.Type.LEXICOGRAPHIC)
@ -180,7 +182,8 @@ private data class IntelligentKnownName(override val raw: String, override val s
// optimize it
val stripped =
name
// Remove excess punctuation from the string, as those u
// Remove excess punctuation from the string, as those usually aren't
// considered in sorting.
.replace(punctRegex, "")
.ifEmpty { name }
.run {

View file

@ -1,119 +1,4 @@
/*
* Copyright (c) 2023 Auxio Project
* DateTest.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.music.info
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
class DateTest {
@Test
fun date_equals_varyingPrecision() {
assertTrue(
requireNotNull(Date.from("2016-08-16T00:01:02")) !=
requireNotNull(Date.from("2016-08-16")))
}
@Test
fun date_compareTo_dates() {
val a = requireNotNull(Date.from("2016-08-16T00:01:02"))
val b = requireNotNull(Date.from("2016-09-16T00:01:02"))
assertEquals(-1, a.compareTo(b))
}
@Test
fun date_compareTo_times() {
val a = requireNotNull(Date.from("2016-08-16T00:02:02"))
val b = requireNotNull(Date.from("2016-08-16T00:01:02"))
assertEquals(1, a.compareTo(b))
}
@Test
fun date_compareTo_varyingPrecision() {
val a = requireNotNull(Date.from("2016-08-16T00:01:02"))
val b = requireNotNull(Date.from("2016-08-16"))
assertEquals(
1,
a.compareTo(b),
)
}
@Test
fun date_from_values() {
assertEquals("2016", Date.from(2016).toString())
assertEquals("2016-08-16", Date.from(2016, 8, 16).toString())
assertEquals("2016-08-16T00:01Z", Date.from(2016, 8, 16, 0, 1).toString())
}
@Test
fun date_from_yearDate() {
assertEquals("2016-08-16", Date.from(20160816).toString())
assertEquals("2016-08-16", Date.from("20160816").toString())
}
@Test
fun date_from_timestamp() {
assertEquals("2016-08-16T00:01:02Z", Date.from("2016-08-16T00:01:02").toString())
assertEquals("2016-08-16T00:01:02Z", Date.from("2016-08-16 00:01:02").toString())
}
@Test
fun date_from_lesserPrecision() {
assertEquals("2016", Date.from("2016").toString())
assertEquals("2016-08", Date.from("2016-08").toString())
assertEquals("2016-08-16", Date.from("2016-08-16").toString())
assertEquals("2016-08-16T00:01Z", Date.from("2016-08-16T00:01").toString())
}
@Test
fun date_from_wack() {
assertEquals(null, Date.from(0))
assertEquals(null, Date.from(""))
assertEquals(null, Date.from("2016-08-16:00:01:02"))
assertEquals("2016-11", Date.from("2016-11-32 25:43:01").toString())
}
@Test
fun dateRange_from_correct() {
val range =
requireNotNull(
Date.Range.from(
listOf(
requireNotNull(Date.from("2016-08-16T00:01:02")),
requireNotNull(Date.from("2016-07-16")),
requireNotNull(Date.from("2014-03-12T00")),
requireNotNull(Date.from("2022-12-22T22:22:22")))))
assertEquals("2014-03-12T00Z", range.min.toString())
assertEquals("2022-12-22T22:22:22Z", range.max.toString())
}
@Test
fun dateRange_from_one() {
val range =
requireNotNull(
Date.Range.from(listOf(requireNotNull(Date.from("2016-08-16T00:01:02")))))
assertEquals("2016-08-16T00:01:02Z", range.min.toString())
assertEquals("2016-08-16T00:01:02Z", range.max.toString())
}
@Test
fun dateRange_from_none() {
assertEquals(null, Date.Range.from(listOf()))
}
}
class NameTest {
}

View file

@ -0,0 +1,4 @@
package org.oxycblt.auxio.music.info
class NameTest {
}