INNER CODE UNIT · Python
group_chunks
kangarooking/cangjie-skill · scripts/build_chunks.py:115
def group_chunks(elements: list[dict], source_id: str, version_id: str, max_chars: int) -> list[dict]:
chunks: list[dict] = []
cur: list[dict] = []
cur_path: list[str] = []
def flush():
nonlocal cur
if not cur:
return
text = "\n\n".join(e["text"] for e in cur)
chunks.append({
"chunk_id": f"ck-{sha256_text(version_id + text)[:12]}",
"source_id": source_id,
"version_id": version_id,
"heading_path": cur[0]["heading_path"],
"element_ids": [e["element_id"] for e in cur],
"text": text,
"char_count": len(text),