"""Synthetic caption structure audit. No AI/editor quality benchmark."""
import json,re,pathlib
BASE=pathlib.Path(__file__).parent
RULES=['nonpositive_duration','overlap','empty_text','long_line']
def ms(t):
 h,m,s,n=map(int,re.split('[:,]',t));return ((h*60+m)*60+s)*1000+n
def audit(srt):
 flags=[];previous_end=None
 for block in srt.strip().split('\n\n'):
  lines=block.splitlines();number=lines[0];a,b=lines[1].split(' --> ');start,end=ms(a),ms(b);text='\n'.join(lines[2:])
  if end<=start:flags.append([number,'nonpositive_duration'])
  if previous_end is not None and start<previous_end:flags.append([number,'overlap'])
  if not text.strip():flags.append([number,'empty_text'])
  if any(len(line)>42 for line in lines[2:]):flags.append([number,'long_line'])
  previous_end=end
 return flags
fixtures={
 'clean':'1\n00:00:01,000 --> 00:00:03,000\nSend the draft for review.',
 'reversed':'1\n00:00:03,000 --> 00:00:01,000\nSend the draft for review.',
 'overlap':'1\n00:00:01,000 --> 00:00:03,000\nSend the draft.\n\n2\n00:00:02,500 --> 00:00:04,000\nWait for review.',
 'empty':'1\n00:00:01,000 --> 00:00:03,000\n',
 'long_line':'1\n00:00:01,000 --> 00:00:05,000\nSend this unusually long caption line to the editor for a manual readability check.',
 'semantic_error':'1\n00:00:01,000 --> 00:00:03,000\nThe meeting starts at nine.'}
expected={'clean':[], 'reversed':[['1','nonpositive_duration']], 'overlap':[['2','overlap']], 'empty':[['1','empty_text']], 'long_line':[['1','long_line']], 'semantic_error':[]}
results=[]
for name,srt in fixtures.items():
 flags=audit(srt);assert flags==expected[name],(name,flags)
 (BASE/(name+'.srt')).write_text(srt+'\n');results.append({'fixture':name,'flags':flags,'expected_match':True})
out={'experiment':'synthetic caption structure audit','performed_date':'2026-09-20','fixtures':6,'seeded_structural_failures':4,'structural_failures_flagged':4,'semantic_errors_flagged':0,'semantic_ground_truth':'For semantic_error fixture, intended source sentence is: The meeting starts at ten.','rules':RULES,'long_line_threshold_characters':42,'threshold_note':'Our editorial heuristic, not a platform specification.','limits':['Six deliberately constructed fixtures; no estimate of real-world error rate.','No speech model or commercial caption editor tested.','Does not evaluate word accuracy, names, punctuation, reading speed, sync with audio, accessibility compliance, or all malformed SRT inputs.','A clean structural result is not publication approval.'],'results':results}
(BASE/'results.json').write_text(json.dumps(out,indent=2)+'\n')
print(json.dumps(out,indent=2))
