From f31a055135908f2d79efa4fd3c176a5a5e9e9a52 Mon Sep 17 00:00:00 2001 From: ldolse Date: Tue, 16 Nov 2010 14:39:53 +0800 Subject: [PATCH] work in progress on chapter detection improvements --- src/calibre/ebooks/conversion/utils.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/calibre/ebooks/conversion/utils.py b/src/calibre/ebooks/conversion/utils.py index 6002509013..724428fec0 100644 --- a/src/calibre/ebooks/conversion/utils.py +++ b/src/calibre/ebooks/conversion/utils.py @@ -165,6 +165,8 @@ class PreProcessor(object): title_header_close = ")\s*" title_line_close = "()?\s*()?\s*(]*>)?\s*" opt_title_close = ")?" + n_lookahead_open = "(?!=" + n_lookahead_close = ")" default_title = r"(\s*[\w\'\"-]+){1,5}?(?=<)" typical_chapters = r".?(Introduction|Synopsis|Acknowledgements|Chapter|Kapitel|Epilogue|Volume\s|Prologue|Book\s|Part\s|Dedication)\s*([\d\w-]+\:?\s*){0,4}" @@ -172,7 +174,11 @@ class PreProcessor(object): uppercase_chapters = r"\s*.?([A-Z#]+(\s|-){0,3}){1,5}\s*" numeric_titles = r".?(\d+\.?\s+([\d\w-]+\:?\'?-?\s?){0,5})\s*" - chapter_marker = lookahead+chapter_line_open+chapter_header_open+typical_chapters+chapter_header_close+chapter_line_close+blank_lines+opt_title_open+title_line_open+title_header_open+default_title+title_header_close+title_line_close+opt_title_close + full_chapter_line = chapter_line_open+chapter_header_open+typical_chapters+chapter_header_close+chapter_line_close + n_lookahead = re.sub("(out|inn|cha)", "l", full_chapter_line) + print "n_lookahead is " + n_lookahead + print "Chapter line is " + full_chapter_line + "\n\n" + chapter_marker = lookahead+full_chapter_line+blank_lines+n_lookahead_open+n_lookahead+n_lookahead_close+opt_title_open+title_line_open+title_header_open+default_title+title_header_close+title_line_close+opt_title_close print chapter_marker heading = re.compile(']*>', re.IGNORECASE) self.html_preprocess_sections = len(heading.findall(html)) @@ -184,21 +190,33 @@ class PreProcessor(object): html = chapdetect.sub(self.chapter_head, html) if self.html_preprocess_sections < 10: self.log("not enough chapters, only " + unicode(self.html_preprocess_sections) + ", trying numeric chapters") - chapter_marker = lookahead+chapter_line_open+chapter_header_open+numeric_chapters+chapter_header_close+chapter_line_close+blank_lines+opt_title_open+title_line_open+title_header_open+default_title+title_header_close+title_line_close+opt_title_close + full_chapter_line = chapter_line_open+chapter_header_open+numeric_chapters+chapter_header_close+chapter_line_close + n_lookahead = re.sub("(out|inn|cha)", "l", full_chapter_line) + print "n_lookahead is " + n_lookahead + print "Chapter line is " + full_chapter_line + "\n\n" + chapter_marker = lookahead+full_chapter_line+blank_lines+n_lookahead_open+n_lookahead+n_lookahead_close+opt_title_open+title_line_open+title_header_open+default_title+title_header_close+title_line_close+opt_title_close print chapter_marker chapdetect2 = re.compile(r'%s' % chapter_marker, re.IGNORECASE) html = chapdetect2.sub(self.chapter_head, html) if self.html_preprocess_sections < 10: self.log("not enough chapters, only " + unicode(self.html_preprocess_sections) + ", trying with uppercase words") - chapter_marker = lookahead+chapter_line_open+chapter_header_open+uppercase_chapters+chapter_header_close+chapter_line_close+blank_lines+opt_title_open+title_line_open+title_header_open+default_title+title_header_close+title_line_close+opt_title_close + full_chapter_line = chapter_line_open+chapter_header_open+uppercase_chapters+chapter_header_close+chapter_line_close + n_lookahead = re.sub("(out|inn|cha)", "l", full_chapter_line) + print "n_lookahead is " + n_lookahead + print "Chapter line is " + full_chapter_line + "\n\n" + chapter_marker = lookahead+full_chapter_line+blank_lines+n_lookahead_open+n_lookahead+n_lookahead_close+opt_title_open+title_line_open+title_header_open+default_title+title_header_close+title_line_close+opt_title_close print chapter_marker chapdetect2 = re.compile(r'%s' % chapter_marker, re.UNICODE) html = chapdetect2.sub(self.chapter_head, html) if self.html_preprocess_sections < 10: self.log("not enough chapters, only " + unicode(self.html_preprocess_sections) + ", trying numeric chapters with titles") - chapter_marker = lookahead+chapter_line_open+chapter_header_open+numeric_titles+chapter_header_close+chapter_line_close+blank_lines+opt_title_open+title_line_open+title_header_open+default_title+title_header_close+title_line_close+opt_title_close + full_chapter_line = chapter_line_open+chapter_header_open+numeric_titles+chapter_header_close+chapter_line_close + n_lookahead = re.sub("(out|inn|cha)", "l", full_chapter_line) + print "n_lookahead is " + n_lookahead + print "Chapter line is " + full_chapter_line + "\n\n" + chapter_marker = lookahead+full_chapter_line+blank_lines+n_lookahead_open+n_lookahead+n_lookahead_close+opt_title_open+title_line_open+title_header_open+default_title+title_header_close+title_line_close+opt_title_close print chapter_marker chapdetect2 = re.compile(r'%s' % chapter_marker, re.IGNORECASE) html = chapdetect2.sub(self.chapter_head, html)