Datasets:
File size: 2,892 Bytes
0b8a041 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import os,sys
os.environ['TOKENIZERS_PARALLELISM']='false'
from pathlib import Path
ROOT=Path('/mnt/data/yuxu/datax-open');RUN=ROOT/'books-markdown-20261205';BUILD=ROOT/'books-build-20261205'
sys.path.insert(0,str(BUILD/'vendor'));sys.path.insert(0,str(BUILD/'scripts'))
import orjson as j,build,format_markdown as fmt,pipeline
import gzip,collections,concurrent.futures,json,time
def init():build.initialize();build.boundary=pipeline.boundary
def work(job):
index,s,f=job;tag=f'{index:03d}'
candidates={}
with (RUN/'reports'/f'{tag}.documents.jsonl').open('rb') as inp:
for raw in inp:
d=j.loads(raw)
if any(k in d['markdown_changes'] for k in ['plain_headings','html_linebreaks','prose_hardwraps_joined','unicode_bullets','numbered_list_markers']):
candidates[d['source_line']]=d
dest=RUN/'fixes';path=dest/f'{tag}.jsonl.gz'
stat={'stage':tag,'candidates':len(candidates),'changed':0,'samples':[]}
with gzip.open(path,'wb',compresslevel=1) as out:
if candidates:
with gzip.open(REC/f['local_path'],'rt',encoding='utf-8') as inp:
for ln,line in enumerate(inp,1):
if ln not in candidates:continue
old=candidates[ln];d=j.loads(line);title=str(d.get('metadata',{}).get('title') or 'Untitled')
before,_=build.clean(d['text'],s['repo'])
assert build.sha(before.encode())==old['pre_markdown_text_sha256']
after,edits=fmt.transform(before,title);h=build.sha(after.encode())
if h==old['cleaned_text_sha256']:
assert edits==old['markdown_changes']
continue
stat['changed']+=1
doc={**old,'cleaned_text_sha256':h,'cleaned_body_chars':len(after),'cleaned_body_bytes':len(after.encode()),
'markdown_changes':edits,'markdown_revision':fmt.VERSION,'prior_v2_sha256':old['cleaned_text_sha256'],
'prior_v2_edits':old['markdown_changes']}
count=tokens=0
pieces=[]
for sl in build.slices(after,title):
pieces.append(sl);count+=1;tokens+=sl['token_count']
doc['slices']=count;doc['tokens']=tokens
out.write(j.dumps({'document':doc,'slices':pieces})+b'\n')
if len(stat['samples'])<3:stat['samples'].append({'id':old['document_id'],'title':title,'old':old['markdown_changes'],'new':edits})
(dest/f'{tag}.report.json').write_bytes(j.dumps(stat,option=j.OPT_INDENT_2))
return stat
REC=ROOT/'books-recovery'
def main():
(RUN/'fixes').mkdir(exist_ok=True)
results=[]
with concurrent.futures.ProcessPoolExecutor(24,initializer=init) as pool:
fs=[pool.submit(work,x) for x in build.jobs()]
for f in concurrent.futures.as_completed(fs):
d=f.result();results.append(d);print(json.dumps({k:v for k,v in d.items() if k!='samples'}),flush=True)
(RUN/'reports/fixes-ready.json').write_bytes(j.dumps({'complete':True,'changed_documents':sum(x['changed'] for x in results),'stages':results},option=j.OPT_INDENT_2))
print('FIXES READY',flush=True)
if __name__=='__main__':main()
|