From 1611a274b9803efd368aba8f0601364e483a4be3 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 9 Feb 2016 20:57:26 +0100 Subject: [PATCH] differentiate subchapters denoted by an alpha prefix --- .../tachiyomi/util/ChapterRecognition.java | 16 ++++++++++++++++ .../kanade/tachiyomi/ChapterRecognitionTest.java | 10 ++++++++++ 2 files changed, 26 insertions(+) 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 69c3b98ac5..af4bfd7aaf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/ChapterRecognition.java +++ b/app/src/main/java/eu/kanade/tachiyomi/util/ChapterRecognition.java @@ -12,6 +12,7 @@ public class ChapterRecognition { 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 withAlphaPostfix = Pattern.compile("(\\d+[\\.,]?\\d*\\s*)([a-z])($|\\b)"); 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*:)"); @@ -33,6 +34,13 @@ public class ChapterRecognition { return; } + // a number with a single alpha prefix is parsed as sub-chapter + matcher = withAlphaPostfix.matcher(name); + if (matcher.find()) { + chapter.chapter_number = Float.parseFloat(matcher.group(1)) + parseAlphaPostFix(matcher.group(2)); + return; + } + // the chapter has a token prepended and something at the end of the number matcher = uncleanWithToken.matcher(name); if (matcher.find()) { @@ -88,6 +96,14 @@ public class ChapterRecognition { } + /** + * x.a -> x.1, x.b -> x.2, etc + */ + private static float parseAlphaPostFix(String postfix) { + char alpha = postfix.charAt(0); + return Float.parseFloat("0." + Integer.toString((int)alpha - 96)); + } + public static List getAllOccurrences(Matcher matcher) { List occurences = new ArrayList<>(); while (matcher.find()) { diff --git a/app/src/test/java/eu/kanade/tachiyomi/ChapterRecognitionTest.java b/app/src/test/java/eu/kanade/tachiyomi/ChapterRecognitionTest.java index e8e8a7eac1..0e33b5f179 100644 --- a/app/src/test/java/eu/kanade/tachiyomi/ChapterRecognitionTest.java +++ b/app/src/test/java/eu/kanade/tachiyomi/ChapterRecognitionTest.java @@ -148,4 +148,14 @@ public class ChapterRecognitionTest { ChapterRecognition.parseChapterNumber(c, randomManga); assertThat(c.chapter_number).isEqualTo(99f); } + + @Test + public void testAlphaSubChapters() { + Chapter c = createChapter("Asu No Yoichi 19a"); + ChapterRecognition.parseChapterNumber(c, randomManga); + assertThat(c.chapter_number).isEqualTo(19.1f); + c = createChapter("Asu No Yoichi 19b"); + ChapterRecognition.parseChapterNumber(c, randomManga); + assertThat(c.chapter_number).isEqualTo(19.2f); + } }