← Lesson
Chunking
Joeven
Run
Reset
Python loads on first run
DOC = """# Runbook ## Runner OOM Raise the worker memory limit. Then replay the DLQ. ## Refunds Refunds for INV-* take 5-7 days. Never refund in cash. ## Security Never paste API keys into tickets. """ def chunk_by_size(text, size, overlap): chunks = [] i = 0 step = max(size - overlap, 1) while i < len(text): chunks.append(text[i:i + size]) i += step return chunks def chunk_by_heading(text): chunks = [] heading = "(top)" buf = [] for line in text.splitlines(): if line.startswith("## "): if buf: chunks.append({"heading": heading, "text": " ".join(buf).strip()}) heading = line[3:].strip() buf = [line] else: buf.append(line) if buf: chunks.append({"heading": heading, "text": " ".join(buf).strip()}) return [c for c in chunks if c["text"]] print("SIZE window cuts:") for i, c in enumerate(chunk_by_size(DOC, 80, 20)): print(i, c.replace("\n", " / ")[:70]) print("HEADING keeps policy:") for c in chunk_by_heading(DOC): print("-", c["heading"], ":", c["text"][:70])
Run to execute this in your browser. Nothing is sent to a server.