mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-11-03 23:58:55 +01:00 
			
		
		
		
	Improve chapter recognition.
This commit is contained in:
		@@ -269,8 +269,10 @@ public class DatabaseHelper {
 | 
			
		||||
                if (!toAdd.isEmpty()) {
 | 
			
		||||
                    // 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.
 | 
			
		||||
                    long now = new Date().getTime();
 | 
			
		||||
 | 
			
		||||
                    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();
 | 
			
		||||
                }
 | 
			
		||||
 
 | 
			
		||||
@@ -44,7 +44,9 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
 | 
			
		||||
                this::getLibraryObservable,
 | 
			
		||||
                (view, pair) -> view.onNextLibraryUpdate(pair.first, pair.second));
 | 
			
		||||
 | 
			
		||||
        start(GET_LIBRARY);
 | 
			
		||||
        if (savedState == null) {
 | 
			
		||||
            start(GET_LIBRARY);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 
 | 
			
		||||
@@ -10,16 +10,18 @@ import eu.kanade.mangafeed.data.database.models.Manga;
 | 
			
		||||
 | 
			
		||||
public class ChapterRecognition {
 | 
			
		||||
 | 
			
		||||
    private static Pattern p1 = Pattern.compile("Ch[^0-9]?\\s*(\\d+[\\.,]?\\d*)");
 | 
			
		||||
    private static Pattern p2 = Pattern.compile("(\\d+[\\.,]?\\d*)");
 | 
			
		||||
    private static Pattern p3 = Pattern.compile("(\\d+[\\.,]?\\d*\\s*:)");
 | 
			
		||||
    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 pUnwanted =
 | 
			
		||||
            Pattern.compile("\\b(v|ver|vol|version|volume)\\.?\\s*\\d+\\b");
 | 
			
		||||
 | 
			
		||||
    public static void parseChapterNumber(Chapter chapter, Manga manga) {
 | 
			
		||||
        if (chapter.chapter_number != -1)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        // Remove spaces and convert to lower case
 | 
			
		||||
        String name = chapter.name;
 | 
			
		||||
        String name = chapter.name.toLowerCase();
 | 
			
		||||
        Matcher matcher;
 | 
			
		||||
 | 
			
		||||
        // Safest option, the chapter has a token prepended
 | 
			
		||||
@@ -29,34 +31,38 @@ public class ChapterRecognition {
 | 
			
		||||
            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
 | 
			
		||||
        matcher = p2.matcher(name);
 | 
			
		||||
        occurences = getAllOccurrences(matcher);
 | 
			
		||||
        if (occurences.size() == 1) {
 | 
			
		||||
            chapter.chapter_number =  occurences.get(0);
 | 
			
		||||
        occurrences = getAllOccurrences(matcher);
 | 
			
		||||
        if (occurrences.size() == 1) {
 | 
			
		||||
            chapter.chapter_number =  occurrences.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);
 | 
			
		||||
        occurrences = getAllOccurrences(matcher);
 | 
			
		||||
        if (occurrences.size() == 1) {
 | 
			
		||||
            chapter.chapter_number =  occurrences.get(0);
 | 
			
		||||
            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
 | 
			
		||||
        String mangaName = replaceIrrelevantCharacters(manga.title);
 | 
			
		||||
        String nameWithoutManga = difference(mangaName, name);
 | 
			
		||||
        if (!nameWithoutManga.isEmpty()) {
 | 
			
		||||
            matcher = p2.matcher(nameWithoutManga);
 | 
			
		||||
            occurences = getAllOccurrences(matcher);
 | 
			
		||||
            if (occurences.size() == 1) {
 | 
			
		||||
                chapter.chapter_number =  occurences.get(0);
 | 
			
		||||
            occurrences = getAllOccurrences(matcher);
 | 
			
		||||
            if (occurrences.size() == 1) {
 | 
			
		||||
                chapter.chapter_number =  occurrences.get(0);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -121,4 +121,18 @@ public class ChapterRecognitionTest {
 | 
			
		||||
        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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user