diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/ChapterRecognition.java b/app/src/main/java/eu/kanade/tachiyomi/util/ChapterRecognition.java index 26d505f69..69c3b98ac 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/ChapterRecognition.java +++ b/app/src/main/java/eu/kanade/tachiyomi/util/ChapterRecognition.java @@ -10,9 +10,11 @@ import eu.kanade.tachiyomi.data.database.models.Manga; public class ChapterRecognition { - private static final Pattern p1 = Pattern.compile("ch[^0-9]?\\s*(\\d+[\\.,]?\\d*)"); - private static final Pattern p2 = Pattern.compile("(\\d+[\\.,]?\\d*)"); - private static final Pattern p3 = Pattern.compile("(\\d+[\\.,]?\\d*\\s*:)"); + private static final Pattern cleanWithToken = Pattern.compile("ch[^0-9]?\\s*(\\d+[\\.,]?\\d+)($|\\b)"); + private static final Pattern uncleanWithToken = Pattern.compile("ch[^0-9]?\\s*(\\d+[\\.,]?\\d*)"); + private static final Pattern cleanNumber = Pattern.compile("(\\d+[\\.,]?\\d+)($|\\b)"); + private static final Pattern uncleanNumber = Pattern.compile("(\\d+[\\.,]?\\d*)"); + private static final Pattern withColon = Pattern.compile("(\\d+[\\.,]?\\d*\\s*:)"); private static final Pattern pUnwanted = Pattern.compile("(\\b|\\d)(v|ver|vol|version|volume)\\.?\\s*\\d+\\b"); @@ -24,8 +26,15 @@ public class ChapterRecognition { String name = chapter.name.toLowerCase(); Matcher matcher; - // Safest option, the chapter has a token prepended - matcher = p1.matcher(name); + // Safest option, the chapter has a token prepended and nothing at the end of the number + matcher = cleanWithToken.matcher(name); + if (matcher.find()) { + chapter.chapter_number = Float.parseFloat(matcher.group(1)); + return; + } + + // the chapter has a token prepended and something at the end of the number + matcher = uncleanWithToken.matcher(name); if (matcher.find()) { chapter.chapter_number = Float.parseFloat(matcher.group(1)); return; @@ -37,7 +46,7 @@ public class ChapterRecognition { List occurrences; // If there's only one number, use it - matcher = p2.matcher(name); + matcher = uncleanNumber.matcher(name); occurrences = getAllOccurrences(matcher); if (occurrences.size() == 1) { chapter.chapter_number = occurrences.get(0); @@ -45,7 +54,15 @@ public class ChapterRecognition { } // If it has a colon, the chapter number should be that one - matcher = p3.matcher(name); + matcher = withColon.matcher(name); + occurrences = getAllOccurrences(matcher); + if (occurrences.size() == 1) { + chapter.chapter_number = occurrences.get(0); + return; + } + + // Prefer numbers without anything appended + matcher = cleanNumber.matcher(name); occurrences = getAllOccurrences(matcher); if (occurrences.size() == 1) { chapter.chapter_number = occurrences.get(0); @@ -59,7 +76,7 @@ public class ChapterRecognition { String mangaName = replaceIrrelevantCharacters(manga.title); String nameWithoutManga = difference(mangaName, name); if (!nameWithoutManga.isEmpty()) { - matcher = p2.matcher(nameWithoutManga); + matcher = uncleanNumber.matcher(nameWithoutManga); occurrences = getAllOccurrences(matcher); if (occurrences.size() == 1) { chapter.chapter_number = occurrences.get(0); @@ -76,7 +93,7 @@ public class ChapterRecognition { while (matcher.find()) { // Match again to get only numbers from the captured text String text = matcher.group(); - Matcher m = p2.matcher(text); + Matcher m = uncleanNumber.matcher(text); if (m.find()) { try { Float value = Float.parseFloat(m.group(1)); diff --git a/app/src/test/java/eu/kanade/tachiyomi/ChapterRecognitionTest.java b/app/src/test/java/eu/kanade/tachiyomi/ChapterRecognitionTest.java index 8626d8bb6..e8e8a7eac 100644 --- a/app/src/test/java/eu/kanade/tachiyomi/ChapterRecognitionTest.java +++ b/app/src/test/java/eu/kanade/tachiyomi/ChapterRecognitionTest.java @@ -141,4 +141,11 @@ public class ChapterRecognitionTest { ChapterRecognition.parseChapterNumber(c, randomManga); assertThat(c.chapter_number).isEqualTo(11f); } + + @Test + public void testWithNumberInChapterTitle() { + Chapter c = createChapter("Ansatsu Kyoushitsu 099 Present Time - 2nd Hour"); + ChapterRecognition.parseChapterNumber(c, randomManga); + assertThat(c.chapter_number).isEqualTo(99f); + } }