Improve chapter recognition.

This commit is contained in:
inorichi 2016-01-14 17:45:32 +01:00
parent 4ee95140e6
commit 24a0a3b96f
4 changed files with 42 additions and 18 deletions

View File

@ -269,8 +269,10 @@ public class DatabaseHelper {
if (!toAdd.isEmpty()) { if (!toAdd.isEmpty()) {
// Set the date fetch for new items in reverse order to allow another sorting method. // Set the date fetch for new items in reverse order to allow another sorting method.
// Sources MUST return the chapters from most to less recent, which is common. // Sources MUST return the chapters from most to less recent, which is common.
long now = new Date().getTime();
for (int i = toAdd.size() - 1; i >= 0; i--) { for (int i = toAdd.size() - 1; i >= 0; i--) {
toAdd.get(i).date_fetch = new Date().getTime(); toAdd.get(i).date_fetch = now++;
} }
added = insertChapters(toAdd).executeAsBlocking().numberOfInserts(); added = insertChapters(toAdd).executeAsBlocking().numberOfInserts();
} }

View File

@ -44,7 +44,9 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
this::getLibraryObservable, this::getLibraryObservable,
(view, pair) -> view.onNextLibraryUpdate(pair.first, pair.second)); (view, pair) -> view.onNextLibraryUpdate(pair.first, pair.second));
start(GET_LIBRARY); if (savedState == null) {
start(GET_LIBRARY);
}
} }
@Override @Override

View File

@ -10,16 +10,18 @@ import eu.kanade.mangafeed.data.database.models.Manga;
public class ChapterRecognition { public class ChapterRecognition {
private static Pattern p1 = Pattern.compile("Ch[^0-9]?\\s*(\\d+[\\.,]?\\d*)"); private static final Pattern p1 = Pattern.compile("ch[^0-9]?\\s*(\\d+[\\.,]?\\d*)");
private static Pattern p2 = Pattern.compile("(\\d+[\\.,]?\\d*)"); private static final Pattern p2 = Pattern.compile("(\\d+[\\.,]?\\d*)");
private static Pattern p3 = Pattern.compile("(\\d+[\\.,]?\\d*\\s*:)"); private static final Pattern p3 = Pattern.compile("(\\d+[\\.,]?\\d*\\s*:)");
private static final Pattern pUnwanted =
Pattern.compile("\\b(v|ver|vol|version|volume)\\.?\\s*\\d+\\b");
public static void parseChapterNumber(Chapter chapter, Manga manga) { public static void parseChapterNumber(Chapter chapter, Manga manga) {
if (chapter.chapter_number != -1) if (chapter.chapter_number != -1)
return; return;
// Remove spaces and convert to lower case String name = chapter.name.toLowerCase();
String name = chapter.name;
Matcher matcher; Matcher matcher;
// Safest option, the chapter has a token prepended // Safest option, the chapter has a token prepended
@ -29,34 +31,38 @@ public class ChapterRecognition {
return; return;
} }
List<Float> occurences; // Remove anything related to the volume or version
name = pUnwanted.matcher(name).replaceAll("");
List<Float> occurrences;
// If there's only one number, use it // If there's only one number, use it
matcher = p2.matcher(name); matcher = p2.matcher(name);
occurences = getAllOccurrences(matcher); occurrences = getAllOccurrences(matcher);
if (occurences.size() == 1) { if (occurrences.size() == 1) {
chapter.chapter_number = occurences.get(0); chapter.chapter_number = occurrences.get(0);
return; return;
} }
// 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 = p3.matcher(name);
occurences = getAllOccurrences(matcher); occurrences = getAllOccurrences(matcher);
if (occurences.size() == 1) { if (occurrences.size() == 1) {
chapter.chapter_number = occurences.get(0); chapter.chapter_number = occurrences.get(0);
return; return;
} }
name = replaceIrrelevantCharacters(name); // This can lead to issues if two numbers are separated by an space
name = name.replaceAll("\\s+", "");
// Try to remove the manga name from the chapter, and try again // Try to remove the manga name from the chapter, and try again
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 = p2.matcher(nameWithoutManga);
occurences = getAllOccurrences(matcher); occurrences = getAllOccurrences(matcher);
if (occurences.size() == 1) { if (occurrences.size() == 1) {
chapter.chapter_number = occurences.get(0); chapter.chapter_number = occurrences.get(0);
return; return;
} }
} }

View File

@ -121,4 +121,18 @@ public class ChapterRecognitionTest {
assertThat(c.chapter_number).isEqualTo(84f); assertThat(c.chapter_number).isEqualTo(84f);
} }
@Test
public void testWithVersionBeforeAndAnotherNumber() {
Chapter c = createChapter("Onepunch-Man Punch Ver002 086 : Creeping Darkness [3]");
ChapterRecognition.parseChapterNumber(c, randomManga);
assertThat(c.chapter_number).isEqualTo(86f);
}
@Test
public void testWithVolumeAfterChapter() {
Chapter c = createChapter("Solanin 028 Vol. 2");
ChapterRecognition.parseChapterNumber(c, randomManga);
assertThat(c.chapter_number).isEqualTo(28f);
}
} }