> ## Documentation Index
> Fetch the complete documentation index at: https://ladygraph.io.vn/llms.txt
> Use this file to discover all available pages before exploring further.

# chunking

> Cut long, loosely structured text into passages.

Cutting a long, loosely structured text into passages that know their section path (see `ChunkingConfig` for the settings).

## `chunking.config`

The `chunking:` section of ladygraph.yaml: how a long, loosely structured text (an OCR'd document, a converted PDF) is cut into passages.

#### `ChunkingConfig`

| Field | Type | Default |
| - | - | - |
| `ranks` | `list[str]` | factory |
| `label_rank` | `bool` | `True` |
| `max_heading_chars` | `int` | `160` |
| `long_heading_ranks` | `list[int]` | `[]` |
| `formula_ranks` | `list[int]` | `[]` |
| `glue` | `str \| None` | `None` |
| `glued_bullets` | `str \| None` | `None` |
| `body_starts_at_rank` | `int \| None` | `None` |
| `appendix_rank` | `int \| None` | `0` |
| `end_pattern` | `str \| None` | `None` |
| `head_lines` | `int` | `0` |
| `stamp_patterns` | `list[str]` | `[]` |
| `replacements` | `dict[str, str]` | `{'&lt;': '<', '&gt;': '>', '&amp;': '&'}` |
| `min_letters` | `int` | `4` |
| `table_group_chars` | `int` | `600` |
| `repair_tables` | `bool` | `True` |
| `damaged_table` | `str` | `'(^\|\\n)(col\\d+\|\\d+(\\.\\d+)?): '` |
| `numeric_columns` | `str \| None` | `None` |
| `repairs` | `list[Repair]` | `[]` |
| `rejoin_broken_sentences` | `bool` | `True` |

#### `Repair`

| Field | Type | Default |
| - | - | - |
| `pattern` | `str` | required |
| `replace` | `str` | required |

#### `load_chunking_config`

```python theme={null}
load_chunking_config(path: pathlib.Path | str) -> ChunkingConfig | None
```

The `chunking:` section of a ladygraph.yaml, or None when the file has none.

## `chunking.flattener`

From a long text to passages: parse, clean, chunk, check. `chunk_document` is pure; `StructuredMarkdownFlattener` plugs it into a markdown source.

#### `ChunkOut`

| Field | Type | Default |
| - | - | - |
| `id` | `str` | required |
| `kind` | `str` | required |
| `path` | `str` | required |
| `section_type` | `str` | required |
| `text` | `str` | required |
| `translate` | `bool` | required |
| `failed` | `list[str]` | factory |
| `maybe_missed_heading` | `list[str]` | factory |
| `repaired` | `bool` | `False` |

#### `chunk_document`

```python theme={null}
chunk_document(doc: str, cfg: ChunkingConfig, embedder=None) -> tuple[list[ChunkOut], dict[str, int]]
```

(chunks in document order, counters).

## `chunking.quality`

Chunk quality checks. They flag a chunk for review; they never cut or change one.

#### `balanced`

```python theme={null}
balanced(text: str) -> bool
```

#### `failed`

```python theme={null}
failed(kind: str, text: str) -> list[str]
```

The checks a chunk fails, from "start", "end", "brackets".

#### `z_dips`

```python theme={null}
z_dips(vectors, sizes: Sequence[int], threshold: float = -1.5) -> list[list[int]]
```

`vectors`: unit-length vectors of every text block, in order; `sizes`: blocks per chunk.

## `chunking.structure`

Structure: a rank-based heading stack turns a long text into chunks that each know their section path.

#### `Chunk`

| Field | Type | Default |
| - | - | - |
| `kind` | `str` | required |
| `path` | `str` | required |
| `section_type` | `str` | required |
| `blocks` | `list` | factory |
| `stamps` | `int` | `0` |

#### `build`

```python theme={null}
build(blocks: list[tuple[str, object, int, 'int | None']], cfg: ChunkingConfig, stats: dict[str, int]) -> list[Chunk]
```

#### `clean`

```python theme={null}
clean(text: str, cfg: ChunkingConfig, stamps: set[str]) -> tuple[str, int]
```

#### `parse`

```python theme={null}
parse(doc: str, cfg: ChunkingConfig, stamps: set[str]) -> list[tuple[str, object, int, 'int | None']]
```

#### `stamp_words`

```python theme={null}
stamp_words(doc: str, cfg: ChunkingConfig) -> set[str]
```

ASCII words and dates in the header lines: signer names, codes, times.

## `chunking.tables`

Tables as text a model can read: every cell keeps its column name, and the damage a page break leaves is repaired.

#### `TableRepairer`

Keeps the good headers it has seen, in document order.

Construct with `TableRepairer(cfg: ChunkingConfig)`.

| Method | What it does |
| - | - |
| `parts(self, blocks: list[list[str]]) -> tuple[list[str], bool]` | — |

#### `fix_row`

```python theme={null}
fix_row(row: list[str], width: int) -> list[str]
```

#### `good`

```python theme={null}
good(header: list[str]) -> bool
```

A header has names: its first cell is not empty or a bare number, and at most one cell is empty.

#### `group`

```python theme={null}
group(lines: list[str], limit: int) -> list[list[int]]
```

#### `names`

```python theme={null}
names(header: list[str]) -> list[str]
```

#### `plain_rows`

```python theme={null}
plain_rows(header: list[str], rows: list[list[str]]) -> list[str]
```

#### `serialise`

```python theme={null}
serialise(header: list[str], rows: list[list[str]], width: int) -> str
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.