From b2f44ff76a152cb4dc16fae421b03569130dc313 Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 30 Nov 2015 20:33:27 +0100 Subject: [PATCH] Improve chapter recognition --- .../mangafeed/util/ChapterRecognition.java | 29 +++++++++++++++---- .../mangafeed/ChapterRecognitionTest.java | 21 ++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/kanade/mangafeed/util/ChapterRecognition.java b/app/src/main/java/eu/kanade/mangafeed/util/ChapterRecognition.java index c052245ff8..90ed609ee5 100644 --- a/app/src/main/java/eu/kanade/mangafeed/util/ChapterRecognition.java +++ b/app/src/main/java/eu/kanade/mangafeed/util/ChapterRecognition.java @@ -12,6 +12,7 @@ public class ChapterRecognition { private static Pattern p1 = Pattern.compile("ch.?(\\d+[\\.,]?\\d*)"); private static Pattern p2 = Pattern.compile("(\\d+[\\.,]?\\d*)"); + private static Pattern p3 = Pattern.compile("(\\d+[\\.,]?\\d*:)"); public static void parseChapterNumber(Chapter chapter, Manga manga) { if (chapter.chapter_number != -1) @@ -28,9 +29,19 @@ public class ChapterRecognition { return; } + List occurences; + // If there's only one number, use it matcher = p2.matcher(name); - List occurences = getAllOccurrences(matcher); + occurences = getAllOccurrences(matcher); + if (occurences.size() == 1) { + chapter.chapter_number = occurences.get(0); + return; + } + + // If it has a colon, the chapter number should be that one + matcher = p3.matcher(name); + occurences = getAllOccurrences(matcher); if (occurences.size() == 1) { chapter.chapter_number = occurences.get(0); return; @@ -55,11 +66,17 @@ public class ChapterRecognition { public static List getAllOccurrences(Matcher matcher) { List occurences = new ArrayList<>(); while (matcher.find()) { - try { - float value = Float.parseFloat(matcher.group()); - if (!occurences.contains(value)) - occurences.add(value); - } catch (NumberFormatException e) { /* Do nothing */ } + // Match again to get only numbers from the captured text + String text = matcher.group(); + Matcher m = p2.matcher(text); + if (m.find()) { + try { + Float value = Float.parseFloat(m.group()); + if (!occurences.contains(value)) { + occurences.add(value); + } + } catch (NumberFormatException e) { /* Do nothing */ } + } } return occurences; } diff --git a/app/src/test/java/eu/kanade/mangafeed/ChapterRecognitionTest.java b/app/src/test/java/eu/kanade/mangafeed/ChapterRecognitionTest.java index 1c46b63323..192d7bb504 100644 --- a/app/src/test/java/eu/kanade/mangafeed/ChapterRecognitionTest.java +++ b/app/src/test/java/eu/kanade/mangafeed/ChapterRecognitionTest.java @@ -84,4 +84,25 @@ public class ChapterRecognitionTest { assertThat(c.chapter_number, is(96f)); } + @Test + public void testWithColonAtTheEnd() { + Chapter c = createChapter("Chapter 5: 365 days"); + ChapterRecognition.parseChapterNumber(c, randomManga); + assertThat(c.chapter_number, is(5f)); + } + + @Test + public void testWithZeros() { + Chapter c = createChapter("Vol.001 Ch.003: Kaguya Doesn't Know Much"); + ChapterRecognition.parseChapterNumber(c, randomManga); + assertThat(c.chapter_number, is(3f)); + } + + @Test + public void testRange() { + Chapter c = createChapter("Ch.191-200 Read Online"); + ChapterRecognition.parseChapterNumber(c, randomManga); + assertThat(c.chapter_number, is(191f)); + } + }