AI-Assisted Coding Lab · Chunk Documents for Search

40:00left of 40:00

Chunk Documents for Search

The help center's search indexes articles in overlapping chunks of words. The README has the rules for normalizing text and cutting chunks. Build chunks.py with the assistant, and check each window boundary as carefully as the tests do.

chunks.py1 of 3

Files

chunks.py

Python · Editable
import re
def normalize(text):
"""Lowercases the text and removes accents, so "Café" matches "cafe"."""
return text.lower()
def words(text):
"""The words in the normalized text, with punctuation dropped."""
return normalize(text).split()
def chunk(items, size, overlap):
"""Windows of size items. Each starts overlap items before the previous one ends, and the last may be shorter."""
step = size - overlap
raise NotImplementedError
def chunk_document(doc_id, text, size=200, overlap=40):
"""Returns [(chunk_id, chunk_text)] for the search index."""
pieces = chunk(words(text), size, overlap)
return [(f"{doc_id}:{i}", " ".join(piece)) for i, piece in enumerate(pieces)]

Test Output

No runs yet.

Assistant

Scripted
  1. Send the first prompt to start.

Next Prompt

normalize should remove accents as well as lowercasing.