from pathlib import Path import sys,time ROOT=Path('/mnt/data/yuxu/datax-open');RUN=ROOT/'books-clean-full-20261205' sys.path.insert(0,str(ROOT/'books-build-20261205/vendor')) import orjson as j,hashlib,collections,datetime,sqlite3,re def now():return datetime.datetime.now(datetime.timezone.utc).isoformat() def main(): while not (RUN/'reports/final-filter-complete.json').exists(): if 'Traceback' in (RUN/'logs/final-filter.log').read_text():raise RuntimeError('final filter failed') time.sleep(10) m=j.loads((RUN/'reports/manifest.pending.json').read_bytes()) con=sqlite3.connect(f'file:{RUN/"documents.sqlite"}?mode=ro',uri=True) statuses=collections.Counter();kept={};activeq=0 for doc,raw in con.execute('SELECT doc,record FROM docs'): d=j.loads(raw);statuses[d['disposition']]+=1 if d['disposition']=='kept_strict_final':kept[doc]=d else:activeq+=1 assert len(kept)+activeq==149159 assert len(kept)==m['counts']['documents'] seen=set();stats=collections.Counter();sources=collections.defaultdict(collections.Counter);boundaries=collections.Counter() current=None;parts=[];end=idx=0;h=None def finish(): if current: body=''.join(parts);doc=current['document_id'] assert end==current['cleaned_body_chars'] and len(body.encode())==current['cleaned_body_bytes'] assert h.hexdigest()==current['cleaned_text_sha256']==kept[doc]['cleaned_text_sha256'] assert '\ufffd' not in body and '\x00' not in body stats['documents']+=1;stats['body_bytes']+=len(body.encode());sources[current['source_repo']]['documents']+=1 for f in m['files']: path=RUN/'books.ready'/Path(f['path']).name;fh=hashlib.sha256();n=tokens=0 with path.open('rb') as inp: for raw in inp: fh.update(raw);row=j.loads(raw.decode('utf-8',errors='strict'));meta=row['meta'];doc=meta['document_id'];text=row['text'] assert raw.endswith(b'\n') and set(row)=={'id','language','meta','text'} and row['language']=='en' assert doc in kept and meta['source_repo'] not in ['common-pile/doab_filtered','common-pile/library_of_congress_filtered','common-pile/pre_1929_books_filtered'] if current is None or doc!=current['document_id']: finish();assert doc not in seen;seen.add(doc);current=meta;parts=[];end=idx=0;h=hashlib.sha256() assert meta['slice_index']==idx and meta['body_char_start']==end assert row['id']==f"books_en_{doc.removeprefix('books_')}_{idx:05d}" assert 0