prefer numbers without anything appended when parsing chapter numbers

This commit is contained in:
Robin Appelman 2016-02-09 20:43:10 +01:00
parent 5977e9f47f
commit fa4a8204a4
2 changed files with 33 additions and 9 deletions

View File

@ -10,9 +10,11 @@ import eu.kanade.tachiyomi.data.database.models.Manga;
public class ChapterRecognition { public class ChapterRecognition {
private static final Pattern p1 = Pattern.compile("ch[^0-9]?\\s*(\\d+[\\.,]?\\d*)"); private static final Pattern cleanWithToken = Pattern.compile("ch[^0-9]?\\s*(\\d+[\\.,]?\\d+)($|\\b)");
private static final Pattern p2 = Pattern.compile("(\\d+[\\.,]?\\d*)"); private static final Pattern uncleanWithToken = Pattern.compile("ch[^0-9]?\\s*(\\d+[\\.,]?\\d*)");
private static final Pattern p3 = Pattern.compile("(\\d+[\\.,]?\\d*\\s*:)"); 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 = private static final Pattern pUnwanted =
Pattern.compile("(\\b|\\d)(v|ver|vol|version|volume)\\.?\\s*\\d+\\b"); Pattern.compile("(\\b|\\d)(v|ver|vol|version|volume)\\.?\\s*\\d+\\b");
@ -24,8 +26,15 @@ public class ChapterRecognition {
String name = chapter.name.toLowerCase(); String name = chapter.name.toLowerCase();
Matcher matcher; Matcher matcher;
// Safest option, the chapter has a token prepended // Safest option, the chapter has a token prepended and nothing at the end of the number
matcher = p1.matcher(name); 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()) { if (matcher.find()) {
chapter.chapter_number = Float.parseFloat(matcher.group(1)); chapter.chapter_number = Float.parseFloat(matcher.group(1));
return; return;
@ -37,7 +46,7 @@ public class ChapterRecognition {
List<Float> occurrences; List<Float> occurrences;
// If there's only one number, use it // If there's only one number, use it
matcher = p2.matcher(name); matcher = uncleanNumber.matcher(name);
occurrences = getAllOccurrences(matcher); occurrences = getAllOccurrences(matcher);
if (occurrences.size() == 1) { if (occurrences.size() == 1) {
chapter.chapter_number = occurrences.get(0); 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 // 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); occurrences = getAllOccurrences(matcher);
if (occurrences.size() == 1) { if (occurrences.size() == 1) {
chapter.chapter_number = occurrences.get(0); chapter.chapter_number = occurrences.get(0);
@ -59,7 +76,7 @@ public class ChapterRecognition {
String mangaName = replaceIrrelevantCharacters(manga.title); String mangaName = replaceIrrelevantCharacters(manga.title);
String nameWithoutManga = difference(mangaName, name); String nameWithoutManga = difference(mangaName, name);
if (!nameWithoutManga.isEmpty()) { if (!nameWithoutManga.isEmpty()) {
matcher = p2.matcher(nameWithoutManga); matcher = uncleanNumber.matcher(nameWithoutManga);
occurrences = getAllOccurrences(matcher); occurrences = getAllOccurrences(matcher);
if (occurrences.size() == 1) { if (occurrences.size() == 1) {
chapter.chapter_number = occurrences.get(0); chapter.chapter_number = occurrences.get(0);
@ -76,7 +93,7 @@ public class ChapterRecognition {
while (matcher.find()) { while (matcher.find()) {
// Match again to get only numbers from the captured text // Match again to get only numbers from the captured text
String text = matcher.group(); String text = matcher.group();
Matcher m = p2.matcher(text); Matcher m = uncleanNumber.matcher(text);
if (m.find()) { if (m.find()) {
try { try {
Float value = Float.parseFloat(m.group(1)); Float value = Float.parseFloat(m.group(1));

View File

@ -141,4 +141,11 @@ public class ChapterRecognitionTest {
ChapterRecognition.parseChapterNumber(c, randomManga); ChapterRecognition.parseChapterNumber(c, randomManga);
assertThat(c.chapter_number).isEqualTo(11f); 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);
}
} }