Neural Mastery
← Practice
Grounding Check via N-Gram Overlapmedium

Grounding Check via N-Gram Overlap

medium RAG & Retrieval 15 min

NumPy allowed · plain Python earns 10 bonus points

RAG step 1/4

A query retrieves context

Embedding similarity ranks the documents before generation.
Open lesson →
01query
02embed
03retrieve
04context
What is RAG?→ embed →top context
retrieval.md44%
rag-systems.md33%
agents.md24%

Cosine-like similarity scores ranked before context assembly

Implement a real, simple hallucination check: whether a generated sentence's words are actually supported by any retrieved source chunk.

Task

Implement is_grounded(answer_sentence, source_chunks, min_overlap). Tokenize answer_sentence into lowercase words (split on whitespace). For each chunk, compute the fraction of the sentence's words that also appear (as whole words, case-insensitive) in that chunk. Return True if ANY chunk's overlap fraction is >= min_overlap.

Function Signature

is_grounded(answer_sentence: str, source_chunks: list[str], min_overlap: float) -> bool

Examples

Example 1: well grounded sentence
Input: answer_sentence = 'the sky is blue' source_chunks = ['The sky is blue today.'] min_overlap = 0.5
Output: True
Example 2: ungrounded sentence
Input: answer_sentence = 'the moon is purple' source_chunks = ['The sky is blue today.'] min_overlap = 0.5
Output: True
Example 3: partial overlap below threshold
Input: answer_sentence = 'cats and dogs run fast' source_chunks = ['dogs run fast in parks'] min_overlap = 0.9
Output: False

Constraints

  • 0 <= min_overlap <= 1
  • answer_sentence non-empty
Python3Saved ✓
def is_grounded(answer_sentence, source_chunks, min_overlap):
# Your implementation here
pass

Case 1: well grounded sentence
Input: answer_sentence = 'the sky is blue' source_chunks = ['The sky is blue today.'] min_overlap = 0.5
Expected: True
Case 2: ungrounded sentence
Input: answer_sentence = 'the moon is purple' source_chunks = ['The sky is blue today.'] min_overlap = 0.5
Expected: True
Case 3: partial overlap below threshold
Input: answer_sentence = 'cats and dogs run fast' source_chunks = ['dogs run fast in parks'] min_overlap = 0.9
Expected: False