| from __future__ import annotations | |
| import re | |
| _WORD_PATTERN = re.compile(r"[A-Za-z][A-Za-z'-]{2,}") | |
| _STOP_WORDS = { | |
| "about", | |
| "after", | |
| "again", | |
| "and", | |
| "are", | |
| "because", | |
| "before", | |
| "being", | |
| "but", | |
| "can", | |
| "for", | |
| "from", | |
| "have", | |
| "help", | |
| "into", | |
| "need", | |
| "new", | |
| "next", | |
| "starting", | |
| "staying", | |
| "that", | |
| "the", | |
| "their", | |
| "them", | |
| "this", | |
| "with", | |
| "you", | |
| "your", | |
| } | |
| def extract_situation_terms(situation: str) -> set[str]: | |
| return { | |
| token.casefold() | |
| for token in _WORD_PATTERN.findall(situation) | |
| if token.casefold() not in _STOP_WORDS | |
| } | |
| def groundedness_score(line: str, situation: str) -> int: | |
| situation_terms = extract_situation_terms(situation) | |
| line_terms = {token.casefold() for token in _WORD_PATTERN.findall(line)} | |
| return len(situation_terms & line_terms) | |
| def is_situation_grounded(line: str, situation: str) -> bool: | |
| return groundedness_score(line, situation) >= 1 | |