from pathlib import Path
import pdfplumber,json,re
from docx import Document
out=Path('/private/tmp/sense-assessments');data={}
for key,name in [('child','Children - english and hindi final.pdf'),('adult','Young Adult - english and hindi final.pdf')]:
 found={}
 with pdfplumber.open('/Users/vengurnagu/Downloads/'+name) as pdf:
  for page in pdf.pages:
   for t in page.extract_tables():
    for row in t:
     if len(row)>=7 and row[0] and re.fullmatch(r'\d+',row[0].strip()) and row[1]:
      n=int(row[0]);lines=row[1].splitlines();english=[]
      for line in lines:
       if re.search(r'[\u0900-\u097f]',line):break
       english.append(line)
      found[n]=' '.join(english).strip()
 if key=='child' and 6 not in found:found[6]='Does the child with Db/ MD express their emotional feelings more often by crying?'
 assert sorted(found)==list(range(1,38)),(key,sorted(found))
 data[key]=[found[i] for i in range(1,38)]
d=Document("/Users/vengurnagu/Downloads/SII Physiotherapy Assessment Tool-Jan'26.docx")
data['physioTables']=[[[c.text.strip() for c in row.cells] for row in t.rows] for t in d.tables]
s=Path('/private/tmp/sense-new-review/POSTURE & GAIT DEVIATION LIST.txt').read_text();a,b=s.split('GAIT DEVIATION LIST');data['postures']=[x.strip() for x in a.splitlines()[1:] if x.strip()];data['gaits']=[x.strip() for x in b.splitlines() if x.strip()]
(out/'data.json').write_text(json.dumps(data,ensure_ascii=False));print('Extracted 37 child and 37 adult questions; 13 physiotherapy tables; posture and gait lists.')
