Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use same base type for all components #10

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions docling_core/types/doc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,37 +128,28 @@ class GlmTableCell(TableCell):
)


class Table(AliasModel):
"""Table."""
class BaseCell(AliasModel):
"""Base cell."""

num_cols: int = Field(alias="#-cols")
num_rows: int = Field(alias="#-rows")
bounding_box: Optional[BoundingBoxContainer] = Field(
default=None, alias="bounding-box", json_schema_extra=es_field(suppress=True)
)
data: Optional[list[list[Union[GlmTableCell, TableCell]]]] = None
model: Optional[str] = None
prov: Optional[list[Prov]] = None
text: Optional[str] = Field(
default=None, json_schema_extra=es_field(term_vector="with_positions_offsets")
)
obj_type: str = Field(
alias="type",
json_schema_extra=es_field(type="keyword", ignore_above=8191),
alias="type", json_schema_extra=es_field(type="keyword", ignore_above=8191)
)


class BaseCell(AliasModel):
"""Base cell."""
class Table(BaseCell):
"""Table."""

bounding_box: Optional[BoundingBoxContainer] = Field(
default=None, alias="bounding-box", json_schema_extra=es_field(suppress=True)
)
prov: Optional[list[Prov]] = None
text: Optional[str] = None
obj_type: str = Field(
alias="type", json_schema_extra=es_field(type="keyword", ignore_above=8191)
)
num_cols: int = Field(alias="#-cols")
num_rows: int = Field(alias="#-rows")
data: Optional[list[list[Union[GlmTableCell, TableCell]]]] = None
model: Optional[str] = None


class BaseText(AliasModel):
Expand Down
22 changes: 18 additions & 4 deletions docling_core/types/doc/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,28 @@ def from_dict(cls, data):

return data

def _resolve_ref(self, item: Ref) -> Optional[Table]:
"""Return the resolved reference in case of table reference, otherwise None."""
result: Optional[Table] = None
def _resolve_ref(self, item: Ref) -> Optional[Union[BaseCell, BaseText]]:
"""Return the resolved reference.

# NOTE: currently only resolves table refs & makes assumptions on ref parts
Resolved the Ref object within the document.
If the object is not found, None is returned.
"""
result: Optional[Union[BaseCell, BaseText]] = None

# NOTE: currently only resolves refs explicitely, such that we can make
# assumptions on ref parts
if item.obj_type == "table" and self.tables:
parts = item.ref.split("/")
result = self.tables[int(parts[2])]
elif item.obj_type == "figure" and self.figures:
parts = item.ref.split("/")
result = self.figures[int(parts[2])]
elif item.obj_type == "equation" and self.equations:
parts = item.ref.split("/")
result = self.equations[int(parts[2])]
elif item.obj_type == "footnote" and self.footnotes:
parts = item.ref.split("/")
result = self.footnotes[int(parts[2])]

return result

Expand Down
77 changes: 39 additions & 38 deletions docs/Document.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@
}
],
"default": null,
"title": "Text"
"title": "Text",
"x-es-term_vector": "with_positions_offsets"
},
"type": {
"title": "Type",
Expand Down Expand Up @@ -1571,14 +1572,6 @@
"Table": {
"description": "Table.",
"properties": {
"#-cols": {
"title": "#-Cols",
"type": "integer"
},
"#-rows": {
"title": "#-Rows",
"type": "integer"
},
"bounding-box": {
"anyOf": [
{
Expand All @@ -1591,21 +1584,11 @@
"default": null,
"x-es-suppress": true
},
"data": {
"prov": {
"anyOf": [
{
"items": {
"items": {
"anyOf": [
{
"$ref": "#/$defs/GlmTableCell"
},
{
"$ref": "#/$defs/TableCell"
}
]
},
"type": "array"
"$ref": "#/$defs/Prov"
},
"type": "array"
},
Expand All @@ -1614,9 +1597,9 @@
}
],
"default": null,
"title": "Data"
"title": "Prov"
},
"model": {
"text": {
"anyOf": [
{
"type": "string"
Expand All @@ -1626,13 +1609,38 @@
}
],
"default": null,
"title": "Model"
"title": "Text",
"x-es-term_vector": "with_positions_offsets"
},
"prov": {
"type": {
"title": "Type",
"type": "string",
"x-es-ignore_above": 8191,
"x-es-type": "keyword"
},
"#-cols": {
"title": "#-Cols",
"type": "integer"
},
"#-rows": {
"title": "#-Rows",
"type": "integer"
},
"data": {
"anyOf": [
{
"items": {
"$ref": "#/$defs/Prov"
"items": {
"anyOf": [
{
"$ref": "#/$defs/GlmTableCell"
},
{
"$ref": "#/$defs/TableCell"
}
]
},
"type": "array"
},
"type": "array"
},
Expand All @@ -1641,9 +1649,9 @@
}
],
"default": null,
"title": "Prov"
"title": "Data"
},
"text": {
"model": {
"anyOf": [
{
"type": "string"
Expand All @@ -1653,20 +1661,13 @@
}
],
"default": null,
"title": "Text",
"x-es-term_vector": "with_positions_offsets"
},
"type": {
"title": "Type",
"type": "string",
"x-es-ignore_above": 8191,
"x-es-type": "keyword"
"title": "Model"
}
},
"required": [
"type",
"#-cols",
"#-rows",
"type"
"#-rows"
],
"title": "Table",
"type": "object"
Expand Down
Loading
Loading