| |
| """ |
| Section-Aware Chunker V6 for CMIP6 RAG Pipeline |
| ================================================ |
| Reads Docling .docling.json files (raw JSON, no Pydantic) and produces |
| semanticly coherent chunks with rich metadata. |
| |
| V2: Major quality overhaul based on Gemini review of 6,938 chunks. |
| Fixes: HTML noise, URN placeholders, figure-axis gibberish, author |
| fragments, excessive overlap, URL-only captions. |
| V5: Data cleaning overhaul based on manual review of 4,567 chunks. |
| Fixes: OCR 'ris' stripping restoration, stronger reference-list |
| filtering, caption merging, content-hash dedup, section path validation. |
| V6: Sanitizer based on Gemini 3.1 Pro Preview audit of 4,060 chunks. |
| Fixes: context-dependent k/e/Pa OCR fixes, +→C PDF glyph fix, |
| digit-density garbage filter, strengthened ref/boilerplate filtering, |
| expanded section path validation. |
| |
| Output: rag/chunks.jsonl (one JSON object per line) |
| """ |
|
|
| import json |
| import re |
| import sys |
| import argparse |
| import hashlib |
| from pathlib import Path |
| from typing import Optional |
|
|
| import tiktoken |
|
|
| |
| PARSED_DIR = Path(__file__).parent / "parsed" |
| PARSED_LEVANTE_DIR = Path(__file__).parent / "parsed_levante" |
| OUTPUT_FILE = Path(__file__).parent / "chunks.jsonl" |
| OUTPUT_LEVANTE_FILE = Path(__file__).parent / "chunks_levante.jsonl" |
|
|
| MAX_TOKENS = 1000 |
| TABLE_MAX_TOKENS = 1200 |
| MIN_TOKENS = 80 |
| MIN_QUALITY_TOKENS = 30 |
| OVERLAP_RATIO = 0.05 |
| ABSTRACT_SOLO = True |
|
|
| |
| EXCLUDE_SECTIONS = { |
| |
| "references", "bibliography", "acknowledgements", "acknowledgments", |
| "author information", "authors and affiliations", "contributions", |
| "author contributions", "corresponding author", "corresponding authors", |
| "ethics declarations", "competing interests", "conflict of interest", |
| "additional information", "supplementary information", "supplementary material", |
| "supplementary materials", "supplementary data", |
| "rights and permissions", "about this article", "cite this article", |
| "this article is cited by", "data availability", "data availability statement", |
| "code availability", "code and data availability", "declarations", "funding", |
| "financial support", "review statement", "disclaimer", |
| "change history", "ethics and inclusion statement", |
| "information & authors", "submission history", "notes", |
| "peer review information", "peer review", "publisher's note", |
| |
| "lead authors", "contributing authors", "how to cite", |
| "table of contents", "contents", "orcidids", "orcids", |
| "correspondence", "correspondence to", "edited by", "reviewed by", |
| "copyright", "open access", "license", |
| "author affiliations", "affiliations", |
| "create a new account", |
| |
| "general rights", "take down policy", "preamble", |
| "citation for published version", "citation for published version:", |
| "fair data use statement", |
| "resources", |
| |
| "explore content", "about the journal", "publish with us", |
| "search", "quick links", "nature.com sitemap", |
| "about nature portfolio", "discover content", "publishing policies", |
| "author & researcher services", "libraries & institutions", |
| "advertising & partnerships", "professional development", |
| "regional websites", "similar content being viewed by others", |
| "access options", "additional access options:", "subjects", |
| "access through your institution", "buy or subscribe", |
| |
| "citing literature", "article metrics", "related articles", |
| "connect with wiley", "change password", "forgot your password?", |
| "request username", "login", "login / register", |
| |
| "sign up for pnas alerts", "metrics", |
| |
| "links to ncbi databases", "cited by other articles", |
| |
| "you may also like", |
| |
| "privacy preference center", "manage consent preferences", |
| "essential cookies", "performance cookies", "functional cookies", |
| "cookie list", "reviewpaper", |
| } |
|
|
| |
| NOISE_PATTERNS = [ |
| r"^skip to (?:main )?content$", |
| r"^skip to article$", |
| r"^thank you for visiting nature\.com", |
| r"^you are using a browser version", |
| r"^the best experience", |
| r"^internet explorer\b", |
| r"^this site uses cookies", |
| r"^we use cookies", |
| r"^accept all cookies", |
| r"^jump to content$", |
| r"^page not found$", |
| r"^accessibility links$", |
| r"^advertisement$", |
| r"^log in$", |
| r"^sign up$", |
| r"^sign in$", |
| r"^subscribe$", |
| r"^view all", |
| |
| r"^privacy policy$", |
| r"^terms of use$", |
| r"^about cookies$", |
| r"^manage cookies$", |
| r"^accessibility$", |
| r"^wiley online library$", |
| r"^publication (?:award|policies|ethics)", |
| r"^submit a paper$", |
| r"^usage statistics$", |
| r"^scientific ethics$", |
| r"^copyright ©", |
| r"^© \d{4}", |
| r"^volume \d+.*issue \d+", |
| r"^\d+ pages?$", |
| r"^open access$", |
| r"^full access$", |
| r"^free access$", |
| r"^download pdf$", |
| r"^share$", |
| r"^cite$", |
| r"^figures?$", |
| r"^tables?$", |
| r"^related$", |
| r"^information$", |
| r"^metrics$", |
| r"^\s*doi:\s*$", |
| |
| r"^download (?:xlsx|pdf|csv|print version)$", |
| r"^open in figure viewer$", |
| r"^check for updates", |
| r"^verify currency and authenticity", |
| r"^crossmark$", |
| r"^scite metrics$", |
| r"^share qr code$", |
| r"^altmetric", |
| r"^article has an altmetric score", |
| r"^forgot your password", |
| r"^request username$", |
| r"^change password$", |
| r"^congrats!$", |
| r"^your password must have", |
| r"^a lower case character", |
| r"^an upper case character", |
| r"^a special character", |
| r"^or a digit$", |
| r"^send email$", |
| r"^recipient\(s\) will receive", |
| r"^article activity alert$", |
| r"^\d+ publications? \d+ supporting", |
| r"^this article also appears in", |
| r"^multiple terms:", |
| r"^we are sorry, but your search", |
| r"^turn mathjax on$", |
| r"^creative commons attribution", |
| r"^data protection$", |
| r"^full-text xml$", |
| r"^bibtex$", |
| r"^ris$", |
| r"^endnote$", |
| r"^sciencedirect$", |
| r"^journals & books$", |
| r"^my account$", |
| r"^dieses dialogfeld schlie", |
| r"^datenschutzrichtlinie", |
| r"^geoscientific model development", |
| r"^pnas nexus$", |
| |
| r"^urn:x-wiley:", |
| |
| r"^https?://", |
| |
| r"^nature climate change$", |
| r"^nature communications$", |
| r"^nature food$", |
| r"^nature reviews", |
| r"^scientific data$", |
| r"^communications earth", |
| ] |
| _noise_re = [re.compile(p, re.IGNORECASE) for p in NOISE_PATTERNS] |
|
|
| |
| _noise_exact = { |
| "pdf", "xml", "ris", "abstract", "figures", "references", "related", |
| "information", "metrics", "share", "cite", "tables", "home", |
| "about", "help", "contact", "feedback", "search", |
| "open menu", "close menu", "sections", "tools", |
| "download", "save", "print", "export", "alerts", |
| "sign in", "register", "my account", "cart", |
| "back to top", "next", "previous", "show more", |
| "supplementary data", "supplementary materials", |
| "view article", "crossref", "google scholar", |
| "pubmed", "web of science", |
| } |
|
|
| |
| SKIP_LABELS = {"page_header", "page_footer", "footnote"} |
|
|
| |
| |
| |
| |
|
|
| _OCR_RIS_CORRECTIONS = { |
| |
| r"\bcompaon\b": "comparison", |
| r"\bcompaons\b": "comparisons", |
| r"\bcharactetics\b": "characteristics", |
| r"\bcharactetic\b": "characteristic", |
| r"\bcharacteing\b": "characterising", |
| r"\bcharacteed\b": "characterised", |
| r"\bcharactee\b": "characterise", |
| r"\bcharactees\b": "characterises", |
| r"\bcharacteze\b": "characterize", |
| r"\bcharactezed\b": "characterized", |
| r"\bcharactezes\b": "characterizes", |
| r"\bcharactezing\b": "characterizing", |
| r"\bcharactezation\b": "characterization", |
| r"\bparameteation\b": "parameterisation", |
| r"\bparameteations\b": "parameterisations", |
| r"\bparameteize\b": "parameterize", |
| r"\bparameteized\b": "parameterized", |
| r"\bparameteizes\b": "parameterizes", |
| r"\bparameteizing\b": "parameterizing", |
| r"\bparameteization\b": "parameterization", |
| r"\bparameteizations\b": "parameterizations", |
| r"\bcompe\b": "comprise", |
| r"\bcomped\b": "comprised", |
| r"\bcompes\b": "comprises", |
| r"\bcomping\b": "comprising", |
| r"\bsurpe\b": "surprise", |
| r"\bsurped\b": "surprised", |
| r"\bsurpes\b": "surprises", |
| r"\bsurping\b": "surprising", |
| r"\bsurpingly\b": "surprisingly", |
| r"\bheutic\b": "heuristic", |
| r"\bheutics\b": "heuristics", |
| r"\bptine\b": "pristine", |
| r"\btoum\b": "tourism", |
| r"\btout\b": "tourist", |
| r"\btouts\b": "tourists", |
| r"\bnouhment\b": "nourishment", |
| r"\bdeb\b": "debris", |
| r"\ben\b": "risen", |
| r"\baes\b": "arises", |
| r"\baing\b": "arising", |
| r"\bsummaed\b": "summarised", |
| r"\bsummaes\b": "summarises", |
| r"\bsummae\b": "summarise", |
| r"\bregulaed\b": "regularised", |
| r"\bregulae\b": "regularise", |
| r"\bautherise\b": "authorise", |
| r"\bautheed\b": "authorised", |
| |
| r"\bChtian\b": "Christian", |
| r"\bChtians\b": "Christians", |
| r"\bChtianity\b": "Christianity", |
| r"\bChtopher\b": "Christopher", |
| r"\bChtensen\b": "Christensen", |
| r"\bChtoffersen\b": "Christoffersen", |
| r"\bChtoph\b": "Christoph", |
| r"\bChtophe\b": "Christophe", |
| r"\bBbane\b": "Brisbane", |
| r"\bKtie\b": "Katie", |
| r"\bKtjansson\b": "Kristjansson", |
| r"\bKtensen\b": "Kristensen", |
| |
| r"\bgives e to\b": "gives rise to", |
| r"\bgave e to\b": "gave rise to", |
| r"\bgiven e to\b": "given rise to", |
| r"\bgiving e to\b": "giving rise to", |
| r"\bat k\b": "at risk", |
| r"\bat k of\b": "at risk of", |
| r"\bthe e of\b": "the rise of", |
| r"\bthe e in\b": "the rise in", |
| r"\bon the e\b": "on the rise", |
| r"\btemperature e\b": "temperature rise", |
| r"\bsea level e\b": "sea level rise", |
| r"\bsea-level e\b": "sea-level rise", |
| r"\bhigh k\b": "high risk", |
| r"\blow k\b": "low risk", |
| r"\bk assessment\b": "risk assessment", |
| r"\bk assessments\b": "risk assessments", |
| r"\bk factor\b": "risk factor", |
| r"\bk factors\b": "risk factors", |
| r"\bk management\b": "risk management", |
| r"\bk of\b": "risk of", |
| |
| r"\bk reduction\b": "risk reduction", |
| r"\bk tolerance\b": "risk tolerance", |
| r"\bk Information\b": "risk Information", |
| r"\bflood k\b": "flood risk", |
| r"\bfire k\b": "fire risk", |
| r"\bmortality k\b": "mortality risk", |
| r"\benvironmental k\b": "environmental risk", |
| r"\bdisaster k\b": "disaster risk", |
| r"\bclimate k\b": "climate risk", |
| r"\bcompound ks\b": "compound risks", |
| r"\bincreasing ks\b": "increasing risks", |
| r"\bmeteo-hydrological ks\b": "meteo-hydrological risks", |
| r"\bks ae\b": "risks arise", |
| r"\bks from\b": "risks from", |
| r"\bks and\b": "risks and", |
| r"\bks to\b": "risks to", |
| r"\bks of\b": "risks of", |
| |
| r"\bGMSL e\b": "GMSL rise", |
| r"\bing sea level\b": "rising sea level", |
| r"\bing temperatures\b": "rising temperatures", |
| r"\bing CO2\b": "rising CO2", |
| r"\bing food prices\b": "rising food prices", |
| r"\bmarked e\b": "marked rise", |
| r"\brapid e\b": "rapid rise", |
| r"\bglobal e\b": "global rise", |
| |
| r"\bPa Agreement\b": "Paris Agreement", |
| r"\bglobal climate cis\b": "global climate crisis", |
| |
| r"\bHistorical C SSP\b": "Historical + SSP", |
| } |
|
|
| |
| |
| |
| _OCR_SUBSTR_CORRECTIONS = { |
| "compaon": "comparison", |
| "charactetic": "characteristic", |
| "characteing": "characterising", |
| "characteed": "characterised", |
| "characteze": "characterize", |
| "charactezed": "characterized", |
| "charactezing": "characterizing", |
| "charactezation": "characterization", |
| "parameteation": "parameterisation", |
| "parameteization": "parameterization", |
| "parameteize": "parameterize", |
| "parameteized": "parameterized", |
| |
| "enterpe": "enterprise", |
| "polaing": "polarising", |
| "Ctofanelli": "Cristofanelli", |
| "Pco": "Prisco", |
| "Kmer": "Krismer", |
| " fi ": " fi", |
| " fl ": " fl", |
| " fi\n": " fi\n", |
| } |
|
|
| |
| |
| |
| _OCR_LIGATURE_PATTERNS = [ |
| (re.compile(r'\b(\w+)\s+fi\s+(\w+)\b'), r'\1fi\2'), |
| (re.compile(r'\b(\w+)\s+fl\s+(\w+)\b'), r'\1fl\2'), |
| (re.compile(r'\b(\w+)\s+ff\s+(\w+)\b'), r'\1ff\2'), |
| ] |
|
|
| |
| _ocr_ris_compiled = [(re.compile(pat), repl) for pat, repl in _OCR_RIS_CORRECTIONS.items()] |
|
|
| |
| _ocr_substr_safe = [ |
| ("compaon", "comparison"), |
| ("charactetic", "characteristic"), |
| ("characteing", "characterising"), |
| ("characteed", "characterised"), |
| ("characteze", "characterize"), |
| ("charactezed", "characterized"), |
| ("charactezing", "characterizing"), |
| ("charactezation", "characterization"), |
| ("parameteation", "parameterisation"), |
| ("parameteization", "parameterization"), |
| ("parameteize", "parameterize"), |
| ("parameteized", "parameterized"), |
| ] |
|
|
|
|
| def fix_ocr_ris_stripping(text: str) -> str: |
| """Fix systematic OCR corruption where 'ris' ligature is stripped from words. |
| |
| Three-pass approach: |
| 1. Word-boundary regex for isolated corrupted words |
| 2. Substring replacement for compound words (intercompaon, etc.) |
| 3. Ligature rejoining for split fi/fl/ff ligatures |
| |
| Safe: will never turn a valid word into something wrong. |
| """ |
| |
| for pattern, replacement in _ocr_ris_compiled: |
| text = pattern.sub(replacement, text) |
| |
| |
| for old, new in _ocr_substr_safe: |
| if old in text: |
| text = text.replace(old, new) |
| |
| |
| for pattern, replacement in _OCR_LIGATURE_PATTERNS: |
| text = pattern.sub(replacement, text) |
| |
| return text |
|
|
|
|
| |
|
|
| _JOURNAL_NAME_SECTIONS = { |
| "journal of advances in modeling earth systems", |
| "reviews of geophysics", |
| "geoscientific model development", |
| "earth system dynamics", |
| "earth system science data", |
| "nature climate change", |
| "nature communications", |
| "nature geoscience", |
| "nature food", |
| "scientific data", |
| "communications earth & environment", |
| "environmental research letters", |
| "global change biology", |
| "journal of climate", |
| "pnas", |
| "pnas nexus", |
| "science advances", |
| "science", |
| } |
|
|
| _garbage_section_re = re.compile( |
| r"^-?\d+\.?\d*\s+\d+\.?\d*[°ºÅ]?$" |
| r"|^\d{1,4}$" |
| r"|^[°ºÅ\d\s.,-]+$" |
| r"|^\w{1,3}$" |
| |
| r"|^PUBLISHED$" |
| r"|^Data:?$" |
| r"|^[A-Z]{2,4}\s+[A-Z]{2,4}$" |
| r"|^\d+\|" |
| r"|^Figures?\s*\d" |
| r"|^Supplementary\s+Table\b" |
| r"|^NPP:\s*" |
| ) |
|
|
|
|
| def is_garbage_section_path(section_name: str) -> bool: |
| """Detect section paths that are garbled coordinates, journal names, or gibberish.""" |
| s = section_name.strip() |
| if not s: |
| return True |
| sl = s.lower() |
| if sl in _JOURNAL_NAME_SECTIONS: |
| return True |
| if _garbage_section_re.match(s): |
| return True |
| return False |
|
|
| |
|
|
| _urn_re = re.compile(r"urn:x-wiley:", re.IGNORECASE) |
| _url_only_re = re.compile(r"^\s*https?://\S+\s*$") |
| _affiliation_re = re.compile( |
| r"(?:department|school|university|institute|laboratory|center|centre|faculty)" |
| r"|(?:@[a-z0-9.-]+\.[a-z]{2,})" |
| r"|(?:orcid\.org)" |
| r"|(?:\d{4}-\d{4}-\d{4}-\d{3}[0-9X])", |
| re.IGNORECASE |
| ) |
| _degree_coord_re = re.compile(r"[°ºÅ][NSEW]", re.IGNORECASE) |
| _ui_button_re = re.compile( |
| r"(?:Download|Open in figure viewer|PowerPoint|Print Version|Download XLSX" |
| r"|Download PDF|Download CSV|Full-text XML|BibTeX|EndNote|RIS)", |
| re.IGNORECASE |
| ) |
|
|
|
|
| def is_urn_placeholder(text: str) -> bool: |
| """Check if text is mostly Wiley URN placeholders.""" |
| lines = text.strip().split("\n") |
| urn_lines = sum(1 for l in lines if _urn_re.search(l)) |
| return urn_lines > len(lines) * 0.5 |
|
|
|
|
| def is_url_only(text: str) -> bool: |
| """Check if text is just a URL.""" |
| return bool(_url_only_re.match(text.strip())) |
|
|
|
|
| def is_affiliation_fragment(text: str) -> bool: |
| """Check if text is a standalone author affiliation chunk.""" |
| t = text.strip() |
| |
| if len(t) > 500: |
| return False |
| matches = len(_affiliation_re.findall(t)) |
| words = len(t.split()) |
| if words < 3: |
| return False |
| |
| return matches >= 2 and matches / max(words, 1) > 0.05 |
|
|
|
|
| def is_figure_axis_gibberish(text: str) -> bool: |
| """Detect text extracted from figure axes/legends (word-salad).""" |
| t = text.strip() |
| if len(t) < 30: |
| return False |
| |
| |
| coord_matches = len(_degree_coord_re.findall(t)) |
| if coord_matches > 5: |
| return True |
| |
| |
| lines = [l.strip() for l in t.split("\n") if l.strip()] |
| if len(lines) > 5: |
| short_lines = sum(1 for l in lines if len(l.split()) <= 3) |
| if short_lines / len(lines) > 0.7: |
| return True |
| |
| |
| words = t.split() |
| if len(words) > 20: |
| |
| tiny = sum(1 for w in words if len(w) <= 2 and not w.isalpha()) |
| if tiny / len(words) > 0.3: |
| return True |
| |
| |
| if len(lines) > 8: |
| |
| verbless = 0 |
| for line in lines: |
| wds = line.split() |
| has_verb_like = any( |
| w.lower().endswith(('ing', 'tion', 'ted', 'tes', 'ses', 'ize', 'ise', 'ate')) |
| for w in wds if len(w) > 3 |
| ) |
| if not has_verb_like and len(wds) <= 5: |
| verbless += 1 |
| if verbless / len(lines) > 0.6: |
| return True |
| |
| return False |
|
|
|
|
| def is_digit_heavy_garbage(text: str, threshold: float = 0.30) -> bool: |
| """V6: Detect chunks that are mostly numbers/symbols (axis data, coordinates). |
| |
| If >30% of characters are digits and special symbols, this is likely |
| visual garbage from chart axes, coordinate grids, or figure annotations. |
| """ |
| t = text.strip() |
| if len(t) < 50: |
| return False |
| symbols = sum(1 for c in t if c.isdigit() or c in "°±+-.,|<>[]{}()=×÷") |
| return symbols / len(t) > threshold |
|
|
|
|
| def is_boilerplate_noise(text: str) -> bool: |
| """V6: Detect publisher boilerplate, copyright text, and preprint disclaimers.""" |
| t = text.strip().lower() |
| boilerplate_markers = [ |
| "copyright and moral rights", |
| "research square preprints are preliminary", |
| "in the format provided by the authors and unedited", |
| "take down policy", |
| "general rights", |
| "citation for published version", |
| "verify currency and authenticity", |
| "version of record", |
| ] |
| matches = sum(1 for m in boilerplate_markers if m in t) |
| return matches >= 2 or (matches >= 1 and len(t) < 300) |
|
|
|
|
| |
| _ref_pattern = re.compile( |
| r"(?:[A-Z][a-z]+(?:,\s*[A-Z]\.?)+\s*(?:,|&|and)\s*){2,}" |
| r"|(?:\(\d{4}[a-z]?\))" |
| r"|(?:et\s+al\.?,\s*\d{4})" |
| r"|(?:doi:\s*10\.\d{4,})", |
| re.IGNORECASE |
| ) |
| _doi_re_v5 = re.compile(r"10\.\d{4,9}/\S+") |
| _year_bracket_re = re.compile(r"\(\d{4}[a-z]?\)") |
|
|
| def is_reference_block(text: str) -> bool: |
| """Detect if text is mostly bibliographic references. |
| |
| V5: Added DOI density and year-bracket density checks. |
| V6: Lowered thresholds (DOI 5→3, years 8→6), added "et al." density. |
| """ |
| t = text.strip() |
| if len(t) < 100: |
| return False |
| |
| |
| doi_count = len(_doi_re_v5.findall(t)) |
| if doi_count >= 3: |
| return True |
| |
| |
| year_count = len(_year_bracket_re.findall(t)) |
| if year_count >= 6: |
| return True |
| |
| |
| et_al_count = t.lower().count("et al") |
| if et_al_count >= 6: |
| return True |
| |
| |
| lines = [l.strip() for l in t.split("\n") if l.strip()] |
| if len(lines) < 3: |
| return False |
| ref_lines = sum(1 for l in lines if _ref_pattern.search(l)) |
| |
| return ref_lines / len(lines) > 0.6 |
|
|
|
|
| def has_repeating_loop(text: str, min_repeat: int = 3) -> bool: |
| """Detect text with repeating string loops (e.g. 'Coral: Bard...' x10).""" |
| t = text.strip() |
| if len(t) < 200: |
| return False |
| |
| for length in (50, 30, 20): |
| for start in range(0, min(len(t) - length, 500), 10): |
| substr = t[start:start + length] |
| if t.count(substr) >= min_repeat: |
| return True |
| return False |
|
|
|
|
| _line_noise_re = re.compile( |
| r"^(?:" |
| r"urn:x-wiley:|" |
| r"https?://\S+$|" |
| r"Dieses Dialogfeld|" |
| r"Datenschutzrichtlinie|" |
| r"This site uses cookies|" |
| r"We use cookies|" |
| r"Accept all cookies|" |
| r"Forgot your password|" |
| r"Request Username|" |
| r"Change Password|" |
| r"Your password must have|" |
| r"a lower case character|" |
| r"an upper case character|" |
| r"a special character|" |
| r"or a digit|" |
| r"Congrats!|" |
| r"Login / Register|" |
| r"Scite metrics|" |
| r"Share QR Code|" |
| r"Access through your institution|" |
| r"Buy or subscribe|" |
| r"Check for updates|" |
| r"Open in figure viewer|" |
| r"Wiley Online Library|" |
| r"Article Activity Alert|" |
| r"Send Email|" |
| r"Recipient\(s\) will receive|" |
| r"Article has an altmetric score|" |
| r"\d+ publications? \d+ supporting|" |
| r"Download & links|" |
| r"Full-text XML|" |
| r"BibTeX|" |
| r"Multiple terms:|" |
| r"We are sorry, but your search|" |
| r"Turn MathJax on|" |
| r"Creative Commons|" |
| r"Connect with Wiley|" |
| r"Privacy Preference Center|" |
| r"Manage Consent Preferences|" |
| r"Essential cookies|" |
| r"Performance cookies|" |
| r"Functional cookies|" |
| r"Cookie List|" |
| |
| r"Correspondence to:|" |
| r"Received:\s+\d|" |
| r"Accepted:\s+\d|" |
| r"Published:\s+\d|" |
| r"Edited by:|" |
| r"Reviewed by:|" |
| r"Lead Authors?:|" |
| r"Contributing Authors?:|" |
| r"How to cite|" |
| r"Crown copyright|" |
| r"Attribution \d\.\d License|" |
| r"An official website of the United States|" |
| r"Search PMC|" |
| r"Sorry, we could not find|" |
| r"Go to Figure|" |
| r"Open all in viewer|" |
| r"There are no results for" |
| r")", |
| re.IGNORECASE |
| ) |
|
|
|
|
| def clean_ui_from_text(text: str) -> str: |
| """Strip embedded UI elements and noise lines from text.""" |
| |
| text = _ui_button_re.sub("", text) |
| |
| text = re.sub(r"Check for updates\.?\s*Verify currency and authenticity via CrossMark\.?", "", text) |
|
|
| |
| lines = text.split("\n") |
| clean_lines = [] |
| for line in lines: |
| stripped = line.strip() |
| if not stripped: |
| clean_lines.append(line) |
| continue |
| if _line_noise_re.match(stripped): |
| continue |
| |
| if stripped.startswith("urn:"): |
| continue |
| clean_lines.append(line) |
|
|
| text = "\n".join(clean_lines) |
|
|
| |
| text = re.sub(r"\n{3,}", "\n\n", text) |
| return text.strip() |
|
|
| |
| _enc = tiktoken.get_encoding("cl100k_base") |
|
|
|
|
| def count_tokens(text: str) -> int: |
| """Count tokens using cl100k_base (≈same as Gemini tokenization).""" |
| return len(_enc.encode(text)) |
|
|
|
|
| |
|
|
| def is_noise_text(text: str) -> bool: |
| """Return True if text is HTML navigation / cookie banner noise.""" |
| t = text.strip() |
| if len(t) < 3: |
| return True |
| tl = t.lower() |
| if tl in _noise_exact: |
| return True |
| for pat in _noise_re: |
| if pat.search(t): |
| return True |
| |
| if is_urn_placeholder(t): |
| return True |
| |
| if is_url_only(t): |
| return True |
| return False |
|
|
|
|
| def is_excluded_section(section_name: str) -> bool: |
| """Return True if section should be excluded from RAG.""" |
| return section_name.strip().lower() in EXCLUDE_SECTIONS |
|
|
|
|
| |
|
|
| class DocNode: |
| """A node in the document tree.""" |
| __slots__ = ("label", "text", "children", "ref", "table_data") |
|
|
| def __init__(self, label: str, text: str = "", ref: str = "", |
| table_data: Optional[dict] = None): |
| self.label = label |
| self.text = text |
| self.children: list["DocNode"] = [] |
| self.ref = ref |
| self.table_data = table_data |
|
|
|
|
| def build_item_index(doc: dict) -> dict: |
| """Build a ref -> raw item lookup from the docling document.""" |
| items = {} |
| for collection in ("texts", "groups", "tables", "pictures", |
| "key_value_items", "form_items"): |
| for item in doc.get(collection, []): |
| ref = item.get("self_ref", "") |
| if ref: |
| items[ref] = item |
| return items |
|
|
|
|
| def resolve_ref(child_ptr: dict) -> str: |
| """Extract the reference string from a child pointer.""" |
| return child_ptr.get("cref", child_ptr.get("$ref", "")) |
|
|
|
|
| def build_tree(doc: dict) -> DocNode: |
| """Build a tree of DocNodes from the docling document body.""" |
| items = build_item_index(doc) |
| root = DocNode(label="root", text="document") |
|
|
| def _build(children_list: list) -> list[DocNode]: |
| nodes = [] |
| for child_ptr in children_list: |
| ref = resolve_ref(child_ptr) |
| raw = items.get(ref, {}) |
| label = raw.get("label", "unknown") |
| text = raw.get("text", "") |
|
|
| |
| table_data = None |
| if label == "table" or "tables" in ref: |
| table_data = raw |
|
|
| node = DocNode(label=label, text=text, ref=ref, |
| table_data=table_data) |
|
|
| |
| if "children" in raw: |
| node.children = _build(raw["children"]) |
|
|
| nodes.append(node) |
| return nodes |
|
|
| body = doc.get("body", {}) |
| root.children = _build(body.get("children", [])) |
| return root |
|
|
|
|
| |
|
|
| class Section: |
| """A logical section of the document.""" |
| __slots__ = ("name", "path", "paragraphs", "tables", "captions", "level") |
|
|
| def __init__(self, name: str, path: str, level: int = 0): |
| self.name = name |
| self.path = path |
| self.level = level |
| self.paragraphs: list[str] = [] |
| self.tables: list[dict] = [] |
| self.captions: list[str] = [] |
|
|
|
|
| def collect_sections(root: DocNode) -> list[Section]: |
| """Walk the document tree and collect sections with their content.""" |
| sections: list[Section] = [] |
| current_section = Section(name="Preamble", path="Preamble", level=0) |
|
|
| def _walk(node: DocNode, section_path_parts: list[str], depth: int): |
| nonlocal current_section |
|
|
| |
| if node.label in SKIP_LABELS: |
| return |
| if node.label in ("text", "list_item") and is_noise_text(node.text): |
| return |
|
|
| |
| if node.label == "section_header": |
| header_text = node.text.strip() |
| if not header_text: |
| return |
|
|
| |
| if is_excluded_section(header_text): |
| |
| excluded = Section(name=header_text, |
| path=" > ".join(section_path_parts + [header_text]), |
| level=depth) |
| excluded.paragraphs.append("__EXCLUDED__") |
| sections.append(excluded) |
| return |
|
|
| |
| if current_section.paragraphs or current_section.tables or current_section.captions: |
| sections.append(current_section) |
|
|
| new_path = section_path_parts + [header_text] |
| current_section = Section( |
| name=header_text, |
| path=" > ".join(new_path), |
| level=depth, |
| ) |
| |
| for child in node.children: |
| _walk(child, new_path, depth + 1) |
| return |
|
|
| |
| if node.label in ("text", "list_item"): |
| text = node.text.strip() |
| if text and len(text) > 5: |
| current_section.paragraphs.append(text) |
|
|
| |
| elif node.label == "caption": |
| text = node.text.strip() |
| if text: |
| current_section.captions.append(text) |
|
|
| |
| elif node.label == "table" or node.table_data: |
| current_section.tables.append(node.table_data or {"text": node.text}) |
|
|
| |
| for child in node.children: |
| _walk(child, section_path_parts, depth) |
|
|
| for child in root.children: |
| _walk(child, [], 0) |
|
|
| |
| if current_section.paragraphs or current_section.tables or current_section.captions: |
| sections.append(current_section) |
|
|
| return sections |
|
|
|
|
| |
|
|
| _md_header_re = re.compile(r'^(#{1,6})\s+(.+)$', re.MULTILINE) |
| _md_table_line_re = re.compile(r'^\|.*\|\s*$') |
| _md_figure_caption_re = re.compile( |
| r'^(?:Figure|Fig\.|Table|Plate|Scheme)\s+\d+', |
| re.IGNORECASE |
| ) |
| _md_image_ref_re = re.compile(r'^!\[.*\]\(.*\)$') |
|
|
|
|
| def parse_markdown_sections(md_text: str) -> list["Section"]: |
| """Parse MinerU VLM markdown into Section objects using # headers. |
| |
| Handles: |
| - # / ## / ### headers → section boundaries |
| - Markdown tables (| col | col |) → table entries |
| - Figure/Table captions → caption entries |
| - Image references  → skipped |
| - Everything else → text paragraphs |
| """ |
| sections: list[Section] = [] |
| current_section = Section(name="Preamble", path="Preamble", level=0) |
| section_stack: list[tuple[int, str]] = [] |
| |
| lines = md_text.split('\n') |
| i = 0 |
| |
| while i < len(lines): |
| line = lines[i] |
| stripped = line.strip() |
| |
| |
| if not stripped: |
| i += 1 |
| continue |
| |
| |
| if _md_image_ref_re.match(stripped): |
| i += 1 |
| continue |
| |
| |
| header_match = _md_header_re.match(stripped) |
| if header_match: |
| level = len(header_match.group(1)) |
| header_text = header_match.group(2).strip() |
| |
| if not header_text: |
| i += 1 |
| continue |
| |
| |
| if current_section.paragraphs or current_section.tables or current_section.captions: |
| sections.append(current_section) |
| |
| |
| while section_stack and section_stack[-1][0] >= level: |
| section_stack.pop() |
| section_stack.append((level, header_text)) |
| |
| |
| path_parts = [name for _, name in section_stack] |
| section_path = " > ".join(path_parts) |
| |
| |
| if is_excluded_section(header_text): |
| current_section = Section(name=header_text, path=section_path, level=level) |
| current_section.paragraphs.append("__EXCLUDED__") |
| sections.append(current_section) |
| |
| i += 1 |
| while i < len(lines): |
| next_match = _md_header_re.match(lines[i].strip()) |
| if next_match and len(next_match.group(1)) <= level: |
| break |
| i += 1 |
| current_section = Section(name="[continued]", path="[continued]", level=0) |
| continue |
| |
| current_section = Section(name=header_text, path=section_path, level=level) |
| i += 1 |
| continue |
| |
| |
| if _md_table_line_re.match(stripped): |
| table_lines = [] |
| while i < len(lines) and _md_table_line_re.match(lines[i].strip()): |
| table_lines.append(lines[i].strip()) |
| i += 1 |
| if table_lines: |
| table_text = '\n'.join(table_lines) |
| current_section.tables.append({"text": table_text}) |
| continue |
| |
| |
| if _md_figure_caption_re.match(stripped): |
| |
| caption_lines = [stripped] |
| i += 1 |
| while i < len(lines) and lines[i].strip() and not _md_header_re.match(lines[i].strip()): |
| if _md_table_line_re.match(lines[i].strip()): |
| break |
| if _md_image_ref_re.match(lines[i].strip()): |
| i += 1 |
| continue |
| if _md_figure_caption_re.match(lines[i].strip()): |
| break |
| caption_lines.append(lines[i].strip()) |
| i += 1 |
| current_section.captions.append(' '.join(caption_lines)) |
| continue |
| |
| |
| if is_noise_text(stripped): |
| i += 1 |
| continue |
| |
| |
| para_lines = [stripped] |
| i += 1 |
| while i < len(lines): |
| next_stripped = lines[i].strip() |
| if not next_stripped: |
| break |
| if _md_header_re.match(next_stripped): |
| break |
| if _md_table_line_re.match(next_stripped): |
| break |
| if _md_image_ref_re.match(next_stripped): |
| i += 1 |
| continue |
| if is_noise_text(next_stripped): |
| i += 1 |
| continue |
| para_lines.append(next_stripped) |
| i += 1 |
| |
| para_text = ' '.join(para_lines) |
| if len(para_text) > 5: |
| current_section.paragraphs.append(para_text) |
| |
| |
| if current_section.paragraphs or current_section.tables or current_section.captions: |
| sections.append(current_section) |
| |
| return sections |
|
|
|
|
| def chunk_markdown_document(md_path: Path, paper_id: str) -> list[dict]: |
| """Chunk a MinerU VLM markdown file using the same pipeline as docling. |
| |
| Reuses: all filters, OCR fixes, overlap, token budget, dedup, etc. |
| """ |
| with open(md_path, 'r', encoding='utf-8', errors='replace') as f: |
| md_text = f.read() |
| |
| meta_path = md_path.with_suffix(".meta.json") |
| meta = {} |
| if meta_path.exists(): |
| with open(meta_path, 'r', encoding='utf-8') as mf: |
| try: |
| meta = json.load(mf) |
| except Exception: |
| pass |
|
|
| |
| doi = meta.get("doi") |
| if not doi: |
| parts = paper_id.split('_', 2) |
| if len(parts) >= 3 and parts[0] == '10': |
| doi = f"10.{parts[1]}/{parts[2].replace('_', '.')}" |
| else: |
| doi = paper_id.replace('_', '/') |
| |
| title = meta.get("title", "") |
| year = meta.get("year", "") |
| journal = meta.get("journal", "") |
| tier = meta.get("tier", "UNKNOWN") |
| |
| |
| sections = parse_markdown_sections(md_text) |
| |
| |
| chunks_out = [] |
| chunk_counter = 0 |
| seen_hashes = set() |
| |
| for section in sections: |
| if section.paragraphs == ["__EXCLUDED__"]: |
| continue |
| |
| section_path = section.path |
| if is_garbage_section_path(section.name): |
| section_path = "[section unknown]" |
| |
| |
| if section.paragraphs: |
| full_text = "\n\n".join(section.paragraphs) |
| full_text = fix_ocr_ris_stripping(full_text) |
| text_tokens = count_tokens(full_text) |
| |
| is_abstract = section.name.strip().lower() == "abstract" |
| chunk_type = "abstract" if is_abstract else "text" |
| |
| if text_tokens <= MAX_TOKENS: |
| raw_chunks = [full_text] |
| else: |
| raw_chunks = chunk_text_block(full_text, MAX_TOKENS) |
| |
| if len(raw_chunks) > 1: |
| raw_chunks = add_overlap(raw_chunks, OVERLAP_RATIO) |
| |
| capped = [] |
| for rc in raw_chunks: |
| if count_tokens(rc) > MAX_TOKENS + 50: |
| capped.extend(chunk_text_block(rc, MAX_TOKENS)) |
| else: |
| capped.append(rc) |
| raw_chunks = capped |
| |
| for i_chunk, chunk_text in enumerate(raw_chunks): |
| ct = count_tokens(chunk_text) |
| if ct < MIN_QUALITY_TOKENS: |
| continue |
| chunk_text = clean_ui_from_text(chunk_text) |
| if not chunk_text or count_tokens(chunk_text) < MIN_QUALITY_TOKENS: |
| continue |
| if is_figure_axis_gibberish(chunk_text): |
| continue |
| if is_digit_heavy_garbage(chunk_text): |
| continue |
| if is_boilerplate_noise(chunk_text): |
| continue |
| if is_affiliation_fragment(chunk_text): |
| continue |
| if is_reference_block(chunk_text): |
| continue |
| if has_repeating_loop(chunk_text): |
| continue |
| |
| content_hash = hashlib.md5(chunk_text.encode()).hexdigest() |
| if content_hash in seen_hashes: |
| continue |
| seen_hashes.add(content_hash) |
| |
| prefix = f'Paper: "{title}"' if title else f'Paper: {paper_id}' |
| if year: |
| prefix += f" ({year}" |
| if journal: |
| prefix += f", {journal}" |
| prefix += ")" |
| elif journal: |
| prefix += f" ({journal})" |
| prefix += f"\nDOI: {doi}" |
| prefix += f"\nSection: {section_path}" |
| prefix += "\n---\n" |
| |
| text_with_prefix = prefix + chunk_text |
| chunk_id = f"{paper_id}__{content_hash[:12]}" |
| |
| chunks_out.append({ |
| "chunk_id": chunk_id, |
| "paper_id": doi, |
| "doi": doi, |
| "title": title, |
| "year": int(year) if str(year).isdigit() else year, |
| "journal": journal, |
| "tier": tier, |
| "section_path": section_path, |
| "section_name": section.name, |
| "chunk_type": chunk_type, |
| "chunk_index": chunk_counter, |
| "token_count": count_tokens(text_with_prefix), |
| "text_with_prefix": text_with_prefix, |
| "text_raw": chunk_text, |
| }) |
| chunk_counter += 1 |
| |
| |
| for j, tbl in enumerate(section.tables): |
| tbl_text = tbl.get("text", "") |
| if not tbl_text or count_tokens(tbl_text) < 10: |
| continue |
| |
| caption = "" |
| if j < len(section.captions): |
| caption = section.captions[j] |
| |
| context = f"[TABLE in section: {section_path}]" |
| if caption: |
| context += f"\nCaption: {caption}" |
| full_table = context + "\n\n" + tbl_text |
| |
| prefix = f'Paper: "{title}"' if title else f'Paper: {paper_id}' |
| if year: |
| prefix += f" ({year}" |
| if journal: |
| prefix += f", {journal}" |
| prefix += ")" |
| prefix += f"\nDOI: {doi}" |
| prefix += f"\nSection: {section_path}" |
| prefix += "\n---\n" |
| |
| text_with_prefix = prefix + full_table |
| chunk_id_raw = f"{doi}__table__{section_path}__{j}" |
| chunk_id = hashlib.md5(chunk_id_raw.encode()).hexdigest()[:12] |
| chunk_id = f"{paper_id}__tbl_{chunk_id}" |
| |
| |
| if count_tokens(tbl_text) > TABLE_MAX_TOKENS: |
| tbl_lines = tbl_text.split('\n') |
| kept = [] |
| tok_count = 0 |
| for tl in tbl_lines: |
| lt = count_tokens(tl) |
| if tok_count + lt > TABLE_MAX_TOKENS - 20: |
| break |
| kept.append(tl) |
| tok_count += lt |
| tbl_text = '\n'.join(kept) + f"\n[... TABLE TRUNCATED ...]" |
| full_table = context + "\n\n" + tbl_text |
| text_with_prefix = prefix + full_table |
| |
| chunks_out.append({ |
| "chunk_id": chunk_id, |
| "paper_id": doi, |
| "doi": doi, |
| "title": title, |
| "year": int(year) if str(year).isdigit() else year, |
| "journal": journal, |
| "tier": tier, |
| "section_path": section_path, |
| "section_name": section.name, |
| "chunk_type": "table", |
| "chunk_index": chunk_counter, |
| "token_count": count_tokens(text_with_prefix), |
| "text_with_prefix": text_with_prefix, |
| "text_raw": full_table, |
| }) |
| chunk_counter += 1 |
| |
| |
| unpaired = section.captions[len(section.tables):] |
| for k, cap in enumerate(unpaired): |
| if count_tokens(cap) < 10: |
| continue |
| if is_url_only(cap): |
| continue |
| cap = clean_ui_from_text(cap) |
| if not cap or count_tokens(cap) < 10: |
| continue |
| cap = fix_ocr_ris_stripping(cap) |
| |
| prefix = f'Paper: "{title}"' if title else f'Paper: {paper_id}' |
| if year: |
| prefix += f" ({year}" |
| if journal: |
| prefix += f", {journal}" |
| prefix += ")" |
| prefix += f"\nDOI: {doi}" |
| prefix += f"\nSection: {section_path}" |
| prefix += "\n---\n" |
| |
| text_with_prefix = prefix + f"[FIGURE CAPTION]\n{cap}" |
| chunk_id_raw = f"{doi}__caption__{section_path}__{k}" |
| chunk_id = hashlib.md5(chunk_id_raw.encode()).hexdigest()[:12] |
| chunk_id = f"{paper_id}__cap_{chunk_id}" |
| |
| chunks_out.append({ |
| "chunk_id": chunk_id, |
| "paper_id": doi, |
| "doi": doi, |
| "title": title, |
| "year": int(year) if str(year).isdigit() else year, |
| "journal": journal, |
| "tier": tier, |
| "section_path": section_path, |
| "section_name": section.name, |
| "chunk_type": "caption", |
| "chunk_index": chunk_counter, |
| "token_count": count_tokens(text_with_prefix), |
| "text_with_prefix": text_with_prefix, |
| "text_raw": cap, |
| }) |
| chunk_counter += 1 |
| |
| |
| MIN_CAPTION_STANDALONE = 150 |
| merged = [] |
| ii = 0 |
| while ii < len(chunks_out): |
| chunk = chunks_out[ii] |
| if (chunk["token_count"] < MIN_TOKENS and |
| chunk["chunk_type"] == "text" and |
| ii + 1 < len(chunks_out) and |
| chunks_out[ii + 1]["section_path"] == chunk["section_path"] and |
| chunks_out[ii + 1]["chunk_type"] == "text"): |
| next_chunk = chunks_out[ii + 1] |
| merged_text = chunk["text_raw"] + "\n\n" + next_chunk["text_raw"] |
| merged_prefix = chunk["text_with_prefix"].split("---\n", 1)[0] + "---\n" + merged_text |
| next_chunk["text_raw"] = merged_text |
| next_chunk["text_with_prefix"] = merged_prefix |
| next_chunk["token_count"] = count_tokens(merged_prefix) |
| ii += 1 |
| elif (chunk["chunk_type"] == "caption" and |
| chunk["token_count"] < MIN_CAPTION_STANDALONE and |
| merged and |
| merged[-1]["section_path"] == chunk["section_path"] and |
| merged[-1]["chunk_type"] == "text"): |
| prev = merged[-1] |
| appended_text = prev["text_raw"] + "\n\n[Figure caption: " + chunk["text_raw"] + "]" |
| appended_prefix = prev["text_with_prefix"].split("---\n", 1)[0] + "---\n" + appended_text |
| prev["text_raw"] = appended_text |
| prev["text_with_prefix"] = appended_prefix |
| prev["token_count"] = count_tokens(appended_prefix) |
| ii += 1 |
| else: |
| merged.append(chunk) |
| ii += 1 |
| |
| for idx, c in enumerate(merged): |
| c["chunk_index"] = idx |
| |
| return merged |
|
|
|
|
| |
|
|
| _sent_re = re.compile( |
| r'(?<=[.!?])\s+(?=[A-Z])' |
| r'|(?<=[.!?])\s*\n' |
| ) |
|
|
|
|
| def split_sentences(text: str) -> list[str]: |
| """Split text into sentences (conservative).""" |
| parts = _sent_re.split(text) |
| return [s.strip() for s in parts if s.strip()] |
|
|
|
|
| |
|
|
| def chunk_text_block(text: str, max_tokens: int = MAX_TOKENS) -> list[str]: |
| """Split a text block into chunks respecting sentence boundaries.""" |
| sentences = split_sentences(text) |
| if not sentences: |
| return [text] if text.strip() else [] |
|
|
| chunks = [] |
| current = [] |
| current_tokens = 0 |
|
|
| for sent in sentences: |
| sent_tokens = count_tokens(sent) |
|
|
| |
| if sent_tokens > max_tokens: |
| if current: |
| chunks.append(" ".join(current)) |
| current = [] |
| current_tokens = 0 |
| words = sent.split() |
| buf = [] |
| buf_tokens = 0 |
| for w in words: |
| wt = count_tokens(w + " ") |
| if buf_tokens + wt > max_tokens and buf: |
| chunks.append(" ".join(buf)) |
| buf = [] |
| buf_tokens = 0 |
| buf.append(w) |
| buf_tokens += wt |
| if buf: |
| chunks.append(" ".join(buf)) |
| continue |
|
|
| if current_tokens + sent_tokens > max_tokens and current: |
| chunks.append(" ".join(current)) |
| current = [] |
| current_tokens = 0 |
|
|
| current.append(sent) |
| current_tokens += sent_tokens |
|
|
| if current: |
| chunks.append(" ".join(current)) |
|
|
| return chunks |
|
|
|
|
| def add_overlap(chunks: list[str], ratio: float = OVERLAP_RATIO) -> list[str]: |
| """Add sentence-level overlap between consecutive chunks.""" |
| if len(chunks) <= 1: |
| return chunks |
|
|
| result = [chunks[0]] |
| for i in range(1, len(chunks)): |
| prev_sents = split_sentences(chunks[i - 1]) |
| if not prev_sents: |
| result.append(chunks[i]) |
| continue |
|
|
| |
| overlap_tokens_target = int(count_tokens(chunks[i - 1]) * ratio) |
| overlap_sents = [] |
| overlap_tokens = 0 |
| for s in reversed(prev_sents): |
| st = count_tokens(s) |
| if overlap_tokens + st > overlap_tokens_target and overlap_sents: |
| break |
| overlap_sents.insert(0, s) |
| overlap_tokens += st |
|
|
| overlap_text = " ".join(overlap_sents) |
| result.append(overlap_text + " " + chunks[i]) |
|
|
| return result |
|
|
|
|
| def table_to_text(table_data: dict, max_tokens: int = TABLE_MAX_TOKENS) -> str: |
| """Convert a Docling table to text representation, truncating if needed.""" |
| if not table_data: |
| return "" |
|
|
| raw = "" |
| |
| grid = table_data.get("data", {}).get("grid", []) |
| if grid: |
| lines = [] |
| for row in grid: |
| cells = [] |
| for cell in row: |
| text = cell.get("text", "") |
| cells.append(text) |
| lines.append(" | ".join(cells)) |
| raw = "\n".join(lines) |
| else: |
| |
| raw = table_data.get("text", "") |
|
|
| if not raw: |
| return "[Table content not extractable]" |
|
|
| |
| if count_tokens(raw) > max_tokens: |
| |
| lines = raw.split("\n") |
| kept = [] |
| tok_count = 0 |
| for line in lines: |
| lt = count_tokens(line) |
| if tok_count + lt > max_tokens - 20: |
| break |
| kept.append(line) |
| tok_count += lt |
| raw = "\n".join(kept) + f"\n[... TABLE TRUNCATED, {len(lines) - len(kept)} more rows ...]" |
|
|
| return raw |
|
|
|
|
| |
|
|
| def chunk_document(docling_path: Path, meta_path: Path) -> list[dict]: |
| """Chunk a single document into retrieval-ready pieces.""" |
| |
| with open(docling_path) as f: |
| doc = json.load(f) |
|
|
| |
| meta = {} |
| if meta_path.exists(): |
| with open(meta_path) as f: |
| meta = json.load(f) |
|
|
| doi = meta.get("doi", docling_path.stem.replace("_", "/", 1) |
| .replace("_", ".", 1)) |
| title = meta.get("title", doc.get("name", "Unknown")) |
| year = meta.get("year", "") |
| journal = meta.get("journal", "") |
| tier = meta.get("tier", "CORE") |
| paper_id = doi |
|
|
| |
| tree = build_tree(doc) |
| sections = collect_sections(tree) |
|
|
| chunks_out = [] |
| chunk_counter = 0 |
| seen_hashes = set() |
|
|
| for section in sections: |
| |
| if section.paragraphs == ["__EXCLUDED__"]: |
| continue |
|
|
| section_path = section.path |
| |
| if is_garbage_section_path(section.name): |
| section_path = f"[section unknown]" |
|
|
| |
| if section.paragraphs: |
| full_text = "\n\n".join(section.paragraphs) |
| |
| full_text = fix_ocr_ris_stripping(full_text) |
| text_tokens = count_tokens(full_text) |
|
|
| |
| is_abstract = section.name.strip().lower() == "abstract" |
| chunk_type = "abstract" if is_abstract else "text" |
|
|
| if text_tokens <= MAX_TOKENS: |
| |
| raw_chunks = [full_text] |
| else: |
| |
| raw_chunks = chunk_text_block(full_text, MAX_TOKENS) |
|
|
| |
| if len(raw_chunks) > 1: |
| raw_chunks = add_overlap(raw_chunks, OVERLAP_RATIO) |
|
|
| |
| capped = [] |
| for rc in raw_chunks: |
| if count_tokens(rc) > MAX_TOKENS + 50: |
| capped.extend(chunk_text_block(rc, MAX_TOKENS)) |
| else: |
| capped.append(rc) |
| raw_chunks = capped |
|
|
| for i, chunk_text in enumerate(raw_chunks): |
| ct = count_tokens(chunk_text) |
| if ct < MIN_QUALITY_TOKENS: |
| continue |
|
|
| |
| chunk_text = clean_ui_from_text(chunk_text) |
| if not chunk_text or count_tokens(chunk_text) < MIN_QUALITY_TOKENS: |
| continue |
|
|
| |
| if is_figure_axis_gibberish(chunk_text): |
| continue |
|
|
| |
| if is_digit_heavy_garbage(chunk_text): |
| continue |
|
|
| |
| if is_boilerplate_noise(chunk_text): |
| continue |
|
|
| |
| if is_affiliation_fragment(chunk_text): |
| continue |
|
|
| |
| if is_reference_block(chunk_text): |
| continue |
|
|
| |
| if has_repeating_loop(chunk_text): |
| continue |
|
|
| |
| content_hash = hashlib.md5(chunk_text.encode()).hexdigest() |
| if content_hash in seen_hashes: |
| continue |
| seen_hashes.add(content_hash) |
|
|
| |
| prefix = f'Paper: "{title}"' |
| if year: |
| prefix += f" ({year}" |
| if journal: |
| prefix += f", {journal}" |
| prefix += ")" |
| elif journal: |
| prefix += f" ({journal})" |
| prefix += f"\nDOI: {doi}" |
| prefix += f"\nSection: {section_path}" |
| prefix += "\n---\n" |
|
|
| text_with_prefix = prefix + chunk_text |
|
|
| |
| chunk_id = f"{doi.replace('/', '_').replace('.', '_')}__{content_hash[:12]}" |
|
|
| chunks_out.append({ |
| "chunk_id": chunk_id, |
| "paper_id": paper_id, |
| "doi": doi, |
| "title": title, |
| "year": int(year) if str(year).isdigit() else year, |
| "journal": journal, |
| "tier": tier, |
| "section_path": section_path, |
| "section_name": section.name, |
| "chunk_type": chunk_type, |
| "chunk_index": chunk_counter, |
| "token_count": count_tokens(text_with_prefix), |
| "text_with_prefix": text_with_prefix, |
| "text_raw": chunk_text, |
| }) |
| chunk_counter += 1 |
|
|
| |
| for j, tbl in enumerate(section.tables): |
| tbl_text = table_to_text(tbl) |
| if not tbl_text or count_tokens(tbl_text) < 10: |
| continue |
|
|
| |
| caption = "" |
| if j < len(section.captions): |
| caption = section.captions[j] |
|
|
| context = f"[TABLE in section: {section_path}]" |
| if caption: |
| context += f"\nCaption: {caption}" |
| full_table = context + "\n\n" + tbl_text |
|
|
| prefix = f'Paper: "{title}"' |
| if year: |
| prefix += f" ({year}" |
| if journal: |
| prefix += f", {journal}" |
| prefix += ")" |
| prefix += f"\nDOI: {doi}" |
| prefix += f"\nSection: {section_path}" |
| prefix += "\n---\n" |
|
|
| text_with_prefix = prefix + full_table |
| chunk_id_raw = f"{paper_id}__table__{section_path}__{j}" |
| chunk_id = hashlib.md5(chunk_id_raw.encode()).hexdigest()[:12] |
| chunk_id = f"{doi.replace('/', '_').replace('.', '_')}__tbl_{chunk_id}" |
|
|
| chunks_out.append({ |
| "chunk_id": chunk_id, |
| "paper_id": paper_id, |
| "doi": doi, |
| "title": title, |
| "year": int(year) if str(year).isdigit() else year, |
| "journal": journal, |
| "tier": tier, |
| "section_path": section_path, |
| "section_name": section.name, |
| "chunk_type": "table", |
| "chunk_index": chunk_counter, |
| "token_count": count_tokens(text_with_prefix), |
| "text_with_prefix": text_with_prefix, |
| "text_raw": full_table, |
| }) |
| chunk_counter += 1 |
|
|
| |
| unpaired_captions = section.captions[len(section.tables):] |
| for k, cap in enumerate(unpaired_captions): |
| if count_tokens(cap) < 10: |
| continue |
|
|
| |
| if is_url_only(cap): |
| continue |
|
|
| |
| cap = clean_ui_from_text(cap) |
| if not cap or count_tokens(cap) < 10: |
| continue |
|
|
| |
| cap = fix_ocr_ris_stripping(cap) |
|
|
| prefix = f'Paper: "{title}"' |
| if year: |
| prefix += f" ({year}" |
| if journal: |
| prefix += f", {journal}" |
| prefix += ")" |
| prefix += f"\nDOI: {doi}" |
| prefix += f"\nSection: {section_path}" |
| prefix += "\n---\n" |
|
|
| text_with_prefix = prefix + f"[FIGURE CAPTION]\n{cap}" |
| chunk_id_raw = f"{paper_id}__caption__{section_path}__{k}" |
| chunk_id = hashlib.md5(chunk_id_raw.encode()).hexdigest()[:12] |
| chunk_id = f"{doi.replace('/', '_').replace('.', '_')}__cap_{chunk_id}" |
|
|
| chunks_out.append({ |
| "chunk_id": chunk_id, |
| "paper_id": paper_id, |
| "doi": doi, |
| "title": title, |
| "year": int(year) if str(year).isdigit() else year, |
| "journal": journal, |
| "tier": tier, |
| "section_path": section_path, |
| "section_name": section.name, |
| "chunk_type": "caption", |
| "chunk_index": chunk_counter, |
| "token_count": count_tokens(text_with_prefix), |
| "text_with_prefix": text_with_prefix, |
| "text_raw": cap, |
| }) |
| chunk_counter += 1 |
|
|
| |
| |
| MIN_CAPTION_STANDALONE = 150 |
| merged = [] |
| i = 0 |
| while i < len(chunks_out): |
| chunk = chunks_out[i] |
| |
| if (chunk["token_count"] < MIN_TOKENS and |
| chunk["chunk_type"] == "text" and |
| i + 1 < len(chunks_out) and |
| chunks_out[i + 1]["section_path"] == chunk["section_path"] and |
| chunks_out[i + 1]["chunk_type"] == "text"): |
| next_chunk = chunks_out[i + 1] |
| merged_text = chunk["text_raw"] + "\n\n" + next_chunk["text_raw"] |
| merged_prefix = chunk["text_with_prefix"].split("---\n", 1)[0] + "---\n" + merged_text |
| next_chunk["text_raw"] = merged_text |
| next_chunk["text_with_prefix"] = merged_prefix |
| next_chunk["token_count"] = count_tokens(merged_prefix) |
| i += 1 |
| |
| elif (chunk["chunk_type"] == "caption" and |
| chunk["token_count"] < MIN_CAPTION_STANDALONE and |
| merged and |
| merged[-1]["section_path"] == chunk["section_path"] and |
| merged[-1]["chunk_type"] == "text"): |
| prev = merged[-1] |
| appended_text = prev["text_raw"] + "\n\n[Figure caption: " + chunk["text_raw"] + "]" |
| appended_prefix = prev["text_with_prefix"].split("---\n", 1)[0] + "---\n" + appended_text |
| prev["text_raw"] = appended_text |
| prev["text_with_prefix"] = appended_prefix |
| prev["token_count"] = count_tokens(appended_prefix) |
| i += 1 |
| else: |
| merged.append(chunk) |
| i += 1 |
|
|
| |
| for idx, c in enumerate(merged): |
| c["chunk_index"] = idx |
|
|
| return merged |
|
|
|
|
| |
|
|
| def find_markdown_papers(input_dir: Path) -> list[tuple[str, Path]]: |
| """Find all MinerU VLM markdown files in parsed_levante or cleaned_levante structure. |
| |
| Structure 1 (parsed): {input_dir}/{DOI_folder}/vlm/{DOI_folder}.md |
| Structure 2 (cleaned): {input_dir}/{DOI_filename}.md |
| Returns: list of (paper_id, md_path) tuples |
| """ |
| papers = [] |
| for item in sorted(input_dir.iterdir()): |
| if item.is_dir(): |
| paper_id = item.name |
| |
| vlm_dir = item / "vlm" |
| if vlm_dir.is_dir(): |
| md_files = list(vlm_dir.glob("*.md")) |
| if md_files: |
| papers.append((paper_id, md_files[0])) |
| else: |
| |
| md_files = list(item.glob("*.md")) |
| if md_files: |
| papers.append((paper_id, md_files[0])) |
| elif item.is_file() and item.suffix == ".md": |
| |
| paper_id = item.stem |
| papers.append((paper_id, item)) |
| return papers |
|
|
|
|
| def print_summary(all_chunks: list, total_papers: int, errors: list, output_path: Path): |
| """Print chunking summary statistics.""" |
| print(f"\n{'='*60}") |
| print(f"CHUNKING COMPLETE") |
| print(f"{'='*60}") |
| print(f"Papers processed: {total_papers - len(errors)}/{total_papers}") |
| print(f"Errors: {len(errors)}") |
| print(f"Total chunks: {len(all_chunks)}") |
|
|
| if all_chunks: |
| tokens = [c["token_count"] for c in all_chunks] |
| print(f"Token range: {min(tokens)}-{max(tokens)}") |
| print(f"Mean tokens: {sum(tokens)/len(tokens):.0f}") |
| print(f"Median tokens: {sorted(tokens)[len(tokens)//2]}") |
|
|
| buckets = {"<100": 0, "100-200": 0, "200-500": 0, "500-1000": 0, |
| "1000-1200": 0, ">1200": 0} |
| for t in tokens: |
| if t < 100: buckets["<100"] += 1 |
| elif t < 200: buckets["100-200"] += 1 |
| elif t < 500: buckets["200-500"] += 1 |
| elif t < 1000: buckets["500-1000"] += 1 |
| elif t < 1200: buckets["1000-1200"] += 1 |
| else: buckets[">1200"] += 1 |
| print(f"\nToken distribution:") |
| for k, v in buckets.items(): |
| pct = v / len(tokens) * 100 |
| bar = "█" * int(pct / 2) |
| print(f" {k:>10}: {v:5d} ({pct:5.1f}%) {bar}") |
|
|
| types = {} |
| for c in all_chunks: |
| ct = c["chunk_type"] |
| types[ct] = types.get(ct, 0) + 1 |
| print(f"\nChunk types:") |
| for ct, count in sorted(types.items()): |
| print(f" {ct}: {count}") |
|
|
| papers = set(c["paper_id"] for c in all_chunks) |
| print(f"\nUnique papers: {len(papers)}") |
|
|
| if errors: |
| print(f"\nFailed papers ({len(errors)}):") |
| for stem, err in errors[:20]: |
| print(f" {stem}: {err}") |
| if len(errors) > 20: |
| print(f" ... and {len(errors) - 20} more") |
|
|
| print(f"\nOutput: {output_path}") |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description="CMIP6 RAG Chunker V6") |
| parser.add_argument("--input-dir", type=str, default=None, |
| help="Input directory (auto-detects docling vs markdown)") |
| parser.add_argument("--output", type=str, default=None, |
| help="Output JSONL file path") |
| parser.add_argument("--max-papers", type=int, default=None, |
| help="Max papers to process (for testing)") |
| args = parser.parse_args() |
|
|
| input_dir = Path(args.input_dir) if args.input_dir else None |
|
|
| |
| use_markdown = False |
| if input_dir: |
| |
| test_dirs = [d for d in input_dir.iterdir() if d.is_dir()][:5] |
| has_vlm = any((d / "vlm").is_dir() for d in test_dirs) |
| has_md_nested = any(list(d.glob("*.md")) for d in test_dirs if d.is_dir()) |
| has_md_flat = any(list(input_dir.glob("*.md"))[:1]) |
| has_docling = any(list(input_dir.glob("*.docling.json"))[:1]) |
|
|
| if has_vlm or (has_md_nested and not has_docling) or (has_md_flat and not has_docling): |
| use_markdown = True |
| else: |
| |
| if PARSED_LEVANTE_DIR.exists() and any(PARSED_LEVANTE_DIR.iterdir()): |
| input_dir = PARSED_LEVANTE_DIR |
| use_markdown = True |
| else: |
| input_dir = PARSED_DIR |
| use_markdown = False |
|
|
| if use_markdown: |
| |
| output_path = Path(args.output) if args.output else OUTPUT_LEVANTE_FILE |
| papers = find_markdown_papers(input_dir) |
| if args.max_papers: |
| papers = papers[:args.max_papers] |
| print(f"Mode: MinerU VLM Markdown") |
| print(f"Found {len(papers)} papers in {input_dir}") |
| print(f"Output: {output_path}") |
| print() |
|
|
| if not papers: |
| print("ERROR: No markdown files found!") |
| return |
|
|
| all_chunks = [] |
| errors = [] |
|
|
| for i, (paper_id, md_path) in enumerate(papers): |
| try: |
| chunks = chunk_markdown_document(md_path, paper_id) |
| all_chunks.extend(chunks) |
| tokens = [c["token_count"] for c in chunks] |
| avg_t = sum(tokens) / len(tokens) if tokens else 0 |
| if (i + 1) % 100 == 0 or (i + 1) == len(papers) or i < 5: |
| print(f"[{i+1:5d}/{len(papers)}] {paper_id}: " |
| f"{len(chunks)} chunks, " |
| f"avg {avg_t:.0f} tokens") |
| except Exception as e: |
| print(f"[{i+1:5d}/{len(papers)}] ERROR {paper_id}: {e}") |
| errors.append((paper_id, str(e))) |
|
|
| with open(output_path, "w") as f: |
| for chunk in all_chunks: |
| f.write(json.dumps(chunk, ensure_ascii=False) + "\n") |
|
|
| print_summary(all_chunks, len(papers), errors, output_path) |
|
|
| else: |
| |
| output_path = Path(args.output) if args.output else OUTPUT_FILE |
| docling_files = sorted(input_dir.glob("*.docling.json")) |
| if args.max_papers: |
| docling_files = docling_files[:args.max_papers] |
| print(f"Mode: Docling JSON") |
| print(f"Found {len(docling_files)} docling files in {input_dir}") |
|
|
| if not docling_files: |
| print("ERROR: No .docling.json files found!") |
| return |
|
|
| all_chunks = [] |
| errors = [] |
|
|
| for i, dp in enumerate(docling_files): |
| stem = dp.name.replace(".docling.json", "") |
| meta_path = input_dir / f"{stem}.json" |
|
|
| try: |
| chunks = chunk_document(dp, meta_path) |
| all_chunks.extend(chunks) |
| tokens = [c["token_count"] for c in chunks] |
| avg_t = sum(tokens) / len(tokens) if tokens else 0 |
| print(f"[{i+1:3d}/{len(docling_files)}] {stem}: " |
| f"{len(chunks)} chunks, " |
| f"avg {avg_t:.0f} tokens, " |
| f"range [{min(tokens) if tokens else 0}-{max(tokens) if tokens else 0}]") |
| except Exception as e: |
| print(f"[{i+1:3d}/{len(docling_files)}] ERROR {stem}: {e}") |
| errors.append((stem, str(e))) |
|
|
| with open(output_path, "w") as f: |
| for chunk in all_chunks: |
| f.write(json.dumps(chunk, ensure_ascii=False) + "\n") |
|
|
| print_summary(all_chunks, len(docling_files), errors, output_path) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|