"""Bounded generation recovery; raw drafts remain available for inspection.""" import copy,re from collections import Counter def repetition(text): words=re.findall(r"\w+",text.casefold()) tail=words[-100:] for n in (3,4,5,6): grams=[tuple(tail[i:i+n]) for i in range(len(tail)-n+1)] if grams and max(Counter(grams).values())>=4:return True compact=re.sub(r"\s+","",text)[-240:] return bool(re.search(r"(.{2,20})\1{5,}",compact)) def sentence_end(text): return bool(re.search(r'[.!?。!?؟]["”’\')\]]*$',text.strip())) def complete_prefix(text): # Keep only complete sentences before repetitive material starts. out='' for m in re.finditer(r'[\s\S]*?[.!?。!?؟](?:["”’\')\]]*)(?=\s|$)',text): candidate=text[:m.end()].strip() if repetition(candidate):break out=candidate return out def rewrite(original,generate,geometry,local,cancel,progress,max_chars=None): attempts=[];limit=max_chars if max_chars is not None else 2*len(original) for number in range(1): if cancel.is_set():break g=copy.deepcopy(geometry) progress('Generating with requested steering settings…') try:r=generate(g,number) except Exception as error: if cancel.is_set():raise InterruptedError('Stopped; completed passages saved.') from error attempts.append({'error':str(error),'attempt':number+1});break raw=r['text'];r['attempt']=number+1;r['effective_controls']=g['controls'] if local else None issues=[] if repetition(raw):issues.append('repetition') if not sentence_end(raw):issues.append('unfinished ending') if not raw.strip():issues.append('empty output') if len(raw)>limit:issues.append('length limit') r['quality_issues']=issues;attempts.append(r) if not issues: return {**r,'attempts':attempts,'selected_attempt':number+1,'recovery':number>0} if cancel.is_set():break # Never join a chopped word to the next passage. Preserve raw drafts in attempts. for r in reversed(attempts): prefix=complete_prefix(r.get('text','')[:limit]) if prefix: return {**r,'text':prefix,'attempts':attempts,'selected_attempt':r['attempt'],'warning':'Only complete sentences retained; inspect raw attempts for omitted material.','recovery':True} return {'text':original,'attempts':attempts,'original_retained':True,'error':'No complete non-repetitive draft; original retained. Raw attempts are in the report.'}