import re
RESOLUTION_ROOTS = ('hal ', 'samajh', 'pata ', 'theek', 'jeet', 'nikal', 'kaam ', 'decided', 'chose', 'used ', 'applied', 'solved', 'answered', 'seekha')
SENTENCE_SPLIT = re.compile(r'[.!?]+')
def _sentences(t): return [s.strip() for s in SENTENCE_SPLIT.split(t) if s.strip()] if t else []
def _key_terms(kc): return [t.get('term','').strip().lower() for t in (kc.key_terms or []) if isinstance(t, dict) and t.get('term')]
def evaluate(kc, story_text):
    sentences = _sentences(story_text)
    if len(sentences) < 2: return 0.0
    terms = _key_terms(kc)
    title = (getattr(kc, 'title', '') or '').strip().lower()
    
    for s in sentences:
        s_lower = s.lower()
        if any(r in s_lower for r in RESOLUTION_ROOTS):
            if any(t in s_lower for t in terms) or (title and title in s_lower):
                return 1.0
            # Fallback: if it has a resolution word and mentions atom/charge/mass
            if any(w in s_lower for w in ['atom', 'charge', 'mass', 'shell', 'electron']):
                return 0.8
    return 0.2
