xytpai's picture
Replace books with strict-cleaned quality-first subset; quarantine hard-to-repair source text
00b8e9c verified
Raw History Blame Contribute Delete
3.19 kB
import re,collections
REVISION='strict_full_source_gate_v2_20261205'
def reasons(text,title,repo):
out=[]
if re.search(r'<(?:IP_ADDRESS|EMAIL_ADDRESS|PHONE_NUMBER|URL|PERSON|ADDRESS)>',text):out.append('redaction_placeholder_or_extraction_artifact')
if re.search(r'(?im)^\s*(?:Book Contents Navigation|Quick Links|Toggle navigation|Main navigation)\s*$',text):out.append('navigation_only_or_residue')
if re.search(r'(?im)^\s*\d{1,3}[.:)]\s*\n\s*\n\s*\d{1,3}[.:)]\s*$',text):out.append('empty_numbered_exercise')
if re.search(r'(?i)catalogue of works|general catalogue|moby word lists|^Index of|type[- ]?book|professional development catalog',title):out.append('catalogue_or_wordlist_not_book_body')
# Preserve maths where self-contained; custom source macros cannot be understood without their definitions.
if re.search(r'\\(?:gas|allni|bpd|Delsub|st|dif|G_|ph|aph|cell|units)\b',text):out.append('undefined_source_math_macro')
if re.search(r'<footnote>',text,re.I):out.append('unconverted_footnote_markup')
lines=text.splitlines()
for i,line in enumerate(lines[:-1]):
line=line.strip();nxt=next((x.strip() for x in lines[i+1:i+4] if x.strip()),'')
if re.search(r'(?i)(?:the (?:formula|equation|expression|net torque|kinetic energy).{0,80}(?:is|gives)|kinematic expression|the net torque is thus|the kinetic energy is then|solving for.{0,50}gives|so that)\s*[:.,]?\s*$',line):
if nxt and not re.search(r'\\|\[latex\]|\$\$|[=∑∫]|^\|',nxt):
out.append('missing_equation_after_intro');break
for i,line in enumerate(lines):
if line.strip()=='|' or (line.lstrip().startswith('|') and line.count('|')<2):
out.append('broken_markdown_table_row');break
if 'gutenberg' in repo:
edges=text[:16000]+'\n'+text[-25000:]
if re.search(r'(?i)(?:\d+[ _]*s[_.]?(?:\s*\d+[ _]*d[_.]?)?|\d+/-)\s*(?:net|each)|\$\s*\d+\.\d+\s*,?\s*net|price\s*\d+[/_]|uniform with this volume|previous publications|already published|these publications can be obtained|of whom may be had volumes|one volume[., ]+cloth|publisher[’\']?s announcements|company[’\']?s publications',edges):
out.append('book_promotion_or_price_list_at_edges')
if re.search(r'(?im)^\s*(?:[A-Z][A-Z &.,]+[’\']S PUBLICATIONS|BY [A-Z][A-Z .-]{4,70})\s*\n',text[-5000:]):
# Allow a single author byline, reject only when followed by a list of many short catalogue-like lines.
tail=text[-5000:];m=re.search(r'(?im)^\s*BY [A-Z][A-Z .-]{4,70}\s*\n',tail)
if m and sum(bool(l.strip()) and len(l.strip())<85 for l in tail[m.end():].splitlines())>=10:
out.append('author_backlist_at_end')
if re.search(r'(?i)Note: Images of the original pages|produced from images available at|short list of bookmarks|instructions for\s+downloading this file|gutenberg\.(?:net|org)/donation',edges):
out.append('residual_digital_edition_packaging')
if re.search(r'(?i)^\s*.*(?:royalty|professional.*amateur).*',text[:4000]) and re.search(r'(?i)CAUTION:|rights of representation|performance rights',text[:5000]):
out.append('performance_notice_scope_requires_review')
if re.search(r'(?i)Printed and Published by.{0,160}sold by',text[-2000:],re.S):out.append('periodical_sales_wrapper')
return sorted(set(out))