-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hierarchical chunker (#18)
Signed-off-by: Panos Vagenas <[email protected]>
- Loading branch information
Showing
10 changed files
with
1,259 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# | ||
# Copyright IBM Corp. 2024 - 2024 | ||
# SPDX-License-Identifier: MIT | ||
# | ||
|
||
"""Data transformations package.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# | ||
# Copyright IBM Corp. 2024 - 2024 | ||
# SPDX-License-Identifier: MIT | ||
# | ||
|
||
"""Define the chunker types.""" | ||
|
||
from docling_core.transforms.chunker.base import ( # noqa | ||
BaseChunker, | ||
Chunk, | ||
ChunkWithMetadata, | ||
) | ||
from docling_core.transforms.chunker.hierarchical_chunker import ( # noqa | ||
HierarchicalChunker, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# | ||
# Copyright IBM Corp. 2024 - 2024 | ||
# SPDX-License-Identifier: MIT | ||
# | ||
|
||
"""Define base classes for chunking.""" | ||
from abc import ABC, abstractmethod | ||
from typing import Iterator, Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
from docling_core.types import BoundingBox, Document | ||
|
||
|
||
class Chunk(BaseModel): | ||
"""Data model for Chunk.""" | ||
|
||
path: str | ||
text: str | ||
|
||
|
||
class ChunkWithMetadata(Chunk): | ||
"""Data model for Chunk including metadata.""" | ||
|
||
page: Optional[int] | ||
bbox: Optional[BoundingBox] | ||
|
||
|
||
class BaseChunker(BaseModel, ABC): | ||
"""Base class for Chunker.""" | ||
|
||
@abstractmethod | ||
def chunk(self, dl_doc: Document, **kwargs) -> Iterator[Chunk]: | ||
"""Chunk the provided document. | ||
Args: | ||
dl_doc (Document): document to chunk | ||
Raises: | ||
NotImplementedError: in this abstract implementation | ||
Yields: | ||
Iterator[Chunk]: iterator over extracted chunks | ||
""" | ||
raise NotImplementedError() |
Oops, something went wrong.