# Score free-text answers against a RoadData.AI reward check: the sensors measured the impact, so the answer can be graded.
# usage: python score_answer.py records.json "Yes, it hits an SUV at about 5.5 s."
import json, re, sys
check = json.load(open(sys.argv[1]))["reward_check"]
measured = check["measured"]["impact_s"]                     # crash-pulse peak from the car's IMU
TOLERANCE_S = 0.5                                            # a collision time counts within half a second
for text in sys.argv[2:]:
    said_no = re.search(r"\bno\b", text, re.I) and not re.search(r"\byes\b", text, re.I)
    m = re.search(r"(\d+(?:\.\d+)?)\s*s\b", text)
    score = 0 if said_no or not m else int(abs(float(m.group(1)) - measured) <= TOLERANCE_S)
    print(f"{score}  {text}")
print(f"measured impact {measured} s ({check['measured']['source']}), tolerance {TOLERANCE_S} s")
