from pathlib import Path
import re,json
out=Path(__file__).parent
draft="Mira Vale waits on Harbor Lane.\nAt 14:03 she counts seven trains.\nShe keeps the blue note out of rain.\nBy 14:10 she heads home again."
good="Mira Vale stands on Harbor Lane.\nAt 14:03 she counts seven trains.\nShe shields a blue note from the rain.\nBy 14:10 she heads home again."
protected=['Mira Vale','Harbor Lane','14:03','seven','14:10']
cases={'preserved':good,'changed_count':good.replace('seven','six'),'shortened_name':good.replace('Mira Vale','Mira'),'extra_time':good+'\nThe sign still reads 14:03.','swapped_times':good.replace('14:03','TEMP').replace('14:10','14:03').replace('TEMP','14:10')}
def count(text,token):return len(re.findall(r'(?<!\w)'+re.escape(token)+r'(?!\w)',text))
rows=[]
for name,text in cases.items():
    checks=[{'token':t,'expected_count':count(draft,t),'actual_count':count(text,t)} for t in protected]
    rows.append({'case':name,'text':text,'checks':checks,'literal_count_pass':all(r['expected_count']==r['actual_count'] for r in checks),'meaning_verified':False,'sung_accuracy_verified':False,'basis':'Authored fictional text; case-sensitive exact tokens and occurrence counts only'})
(out/'lyric-fact-results.json').write_text(json.dumps({'draft':draft,'protected_tokens':protected,'cases':rows},indent=2))
assert [r['literal_count_pass'] for r in rows]==[True,False,False,False,True]
print(json.dumps([{'case':r['case'],'literal_count_pass':r['literal_count_pass']} for r in rows]))
