#!/usr/bin/env python3
from pathlib import Path
import json,re
root=Path(__file__).resolve().parents[1];text=(root/'script/script_water_wrinkling.txt').read_text();low=text.lower()
def any_of(*xs):return any(x.lower() in low for x in xs)
groups={
'water_wrinkling':any_of('nhăn khi ngâm nước','ngón tay nhăn','đầu ngón tay nhăn'),
'stratum_corneum':any_of('lớp sừng','stratum corneum') and any_of('hút nước','ngậm nước') and any_of('không đủ','chưa đủ','không giải thích hết'),
'autonomic':any_of('hệ thần kinh tự chủ','thần kinh tự động') and any_of('giao cảm','sympathetic'),
'vasoconstriction':any_of('co mạch','vasoconstriction') and any_of('giảm thể tích','co lại','ít máu hơn'),
'denervation':any_of('mất phân bố thần kinh','denervation','tổn thương thần kinh','mất hoặc suy giảm chi phối thần kinh giao cảm','suy giảm chi phối thần kinh giao cảm') and any_of('giảm','mất','không nhăn','không xuất hiện'),
'variation':any_of('nhiệt độ','ấm','lạnh') and any_of('nước mặn','nước ngọt','môi trường nước') and any_of('khác nhau','khác biệt cá nhân','tùy người'),
'wet_grip':any_of('cầm vật ướt','bám vật ướt','wet grip','cầm nắm trong môi trường ướt','thao tác vật ướt','xử lý vật ướt') and any_of('rãnh thoát nước','thoát nước','lốp xe'),
'mixed_evidence':any_of('không phải lúc nào','không tìm thấy','kết quả trái ngược','bằng chứng hỗn hợp','chưa ngã ngũ','chưa kết luận'),
'normal_recovery':any_of('lau khô','khô lại') and any_of('ấm lên','làm ấm','trở lại bình thường'),
'practical':any_of('hàng rào da','dưỡng ẩm','barrier') and any_of('găng','găng tay') and any_of('không dùng','đừng dùng','không nên cố biến','không biến hiện tượng','tuyệt đối không biến') and any_of('tự kiểm tra thần kinh','tự test thần kinh','chẩn đoán thần kinh','bài kiểm tra tại nhà','bài tự kiểm tra thần kinh'),
'aquagenic':any_of('aquagenic wrinkling','nhăn lòng bàn tay do nước') and any_of('quá nhanh','rất nhanh','đau','ngứa'),
'safety':any_of('một bên','một tay','bất đối xứng','bên còn lại') and any_of('kéo dài','đã khô','sau khi da đã khô') and any_of('đau','tê') and any_of('yếu','mất cảm giác','đổi màu'),
'no_self_diagnosis':any_of('không tự chẩn đoán','đừng tự chẩn đoán','không đủ để tự chẩn đoán','không đủ để bạn tự kết luận','không đủ để tự kết luận') and any_of('xơ nang','cystic fibrosis'),
'closing_echo':any_of('miếng bọt biển') and any_of('hệ thần kinh tự chủ') and any_of('mạch máu dưới da','mạch máu'),
}
def source_context(name_terms, claim_terms, radius=700):
    positions=[]
    for term in name_terms:
        start=0
        while True:
            index=low.find(term.lower(),start)
            if index<0:break
            positions.append(index);start=index+1
    return any(any(claim.lower() in low[max(0,index-radius):index+radius] for claim in claim_terms) for index in positions)
sources={
'Mark Changizi':source_context(('mark changizi',),('rãnh thoát nước','drainage network','mạng lưới thoát nước')),
'Kareklas/Smulders':source_context(('kyriacos kareklas','tom smulders'),('vật ướt','wet objects','thao tác vật ướt','cầm vật ướt')),
'negative_replication':source_context(('german và','german et al','nghiên cứu german','nghiên cứu sau'),('không tìm thấy','không cải thiện','không phải lúc nào','không thấy lợi thế')),
}
forbidden={x:bool(re.search(rf'(?<![\wÀ-ỹ]){x}(?![\wÀ-ỹ])',low)) for x in ['tôi','chúng ta','chúng tôi']}
ok=all(groups.values()) and all(sources.values()) and not any(forbidden.values());print(json.dumps({'groups':groups,'sources':sources,'forbidden_narrator_pronouns':forbidden,'status':'PASS' if ok else 'FAIL'},ensure_ascii=False));raise SystemExit(0 if ok else 1)
