-
Notifications
You must be signed in to change notification settings - Fork 117
fix: improve doc item typing #105
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -593,6 +593,21 @@ def get_image(self, doc: "DoclingDocument") -> Optional[PILImage.Image]: | |
| class TextItem(DocItem): | ||
| """TextItem.""" | ||
|
|
||
| label: typing.Literal[ | ||
| DocItemLabel.CAPTION, | ||
| DocItemLabel.CHECKBOX_SELECTED, | ||
| DocItemLabel.CHECKBOX_UNSELECTED, | ||
| DocItemLabel.CODE, | ||
| DocItemLabel.FOOTNOTE, | ||
| DocItemLabel.FORMULA, | ||
| DocItemLabel.PAGE_FOOTER, | ||
| DocItemLabel.PAGE_HEADER, | ||
| DocItemLabel.PARAGRAPH, | ||
| DocItemLabel.REFERENCE, | ||
| DocItemLabel.TEXT, | ||
| DocItemLabel.TITLE, | ||
| ] | ||
|
|
||
| orig: str # untreated representation | ||
| text: str # sanitized representation | ||
|
|
||
|
|
@@ -644,8 +659,10 @@ def export_to_document_tokens( | |
| class SectionHeaderItem(TextItem): | ||
| """SectionItem.""" | ||
|
|
||
| label: typing.Literal[DocItemLabel.SECTION_HEADER] = DocItemLabel.SECTION_HEADER | ||
| level: LevelNumber | ||
| label: typing.Literal[DocItemLabel.SECTION_HEADER] = ( | ||
| DocItemLabel.SECTION_HEADER # type: ignore[assignment] | ||
| ) | ||
| level: LevelNumber = 1 | ||
|
|
||
| def export_to_document_tokens( | ||
| self, | ||
|
|
@@ -695,9 +712,11 @@ def export_to_document_tokens( | |
| class ListItem(TextItem): | ||
| """SectionItem.""" | ||
|
|
||
| label: typing.Literal[DocItemLabel.LIST_ITEM] = DocItemLabel.LIST_ITEM | ||
| label: typing.Literal[DocItemLabel.LIST_ITEM] = ( | ||
| DocItemLabel.LIST_ITEM # type: ignore[assignment] | ||
| ) | ||
| enumerated: bool = False | ||
| marker: str # The bullet or number symbol that prefixes this list item | ||
| marker: str = "-" # The bullet or number symbol that prefixes this list item | ||
|
|
||
|
|
||
| class FloatingItem(DocItem): | ||
|
|
@@ -923,7 +942,10 @@ class TableItem(FloatingItem): | |
| """TableItem.""" | ||
|
|
||
| data: TableData | ||
| label: typing.Literal[DocItemLabel.TABLE] = DocItemLabel.TABLE | ||
| label: typing.Literal[ | ||
| DocItemLabel.DOCUMENT_INDEX, | ||
| DocItemLabel.TABLE, | ||
| ] = DocItemLabel.TABLE | ||
|
|
||
| def export_to_dataframe(self) -> pd.DataFrame: | ||
| """Export the table as a Pandas DataFrame.""" | ||
|
|
@@ -1272,9 +1294,25 @@ def export_to_document_tokens( | |
| class KeyValueItem(DocItem): | ||
| """KeyValueItem.""" | ||
|
|
||
| label: typing.Literal[DocItemLabel.KEY_VALUE_REGION] = DocItemLabel.KEY_VALUE_REGION | ||
|
|
||
|
|
||
| ContentItem = Union[ | ||
| TextItem, SectionHeaderItem, ListItem, PictureItem, TableItem, KeyValueItem | ||
| class FormItem(DocItem): | ||
|
||
| """FormItem.""" | ||
|
|
||
| label: typing.Literal[DocItemLabel.FORM] = DocItemLabel.FORM | ||
|
|
||
|
|
||
| ContentItem = Annotated[ | ||
| Union[ | ||
| TextItem, | ||
| SectionHeaderItem, | ||
| ListItem, | ||
| PictureItem, | ||
| TableItem, | ||
| KeyValueItem, | ||
vagenas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ], | ||
| Field(discriminator="label"), | ||
| ] | ||
|
|
||
|
|
||
|
|
@@ -1505,6 +1543,7 @@ def add_table( | |
| caption: Optional[Union[TextItem, RefItem]] = None, # This is not cool yet. | ||
| prov: Optional[ProvenanceItem] = None, | ||
| parent: Optional[GroupItem] = None, | ||
| label: DocItemLabel = DocItemLabel.TABLE, | ||
| ): | ||
| """add_table. | ||
|
|
||
|
|
@@ -1522,7 +1561,7 @@ def add_table( | |
| cref = f"#/tables/{table_index}" | ||
|
|
||
| tbl_item = TableItem( | ||
| label=DocItemLabel.TABLE, data=data, self_ref=cref, parent=parent.get_ref() | ||
| label=label, data=data, self_ref=cref, parent=parent.get_ref() | ||
| ) | ||
| if prov: | ||
| tbl_item.prov.append(prov) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume the reason this works with serialization and deserialization, despite setting a level default, is becuase the label is now non-overlapping to the label literals in TextItem? If yes, that's great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
labelis now non-overlapping — and is actually used as the discriminator field inContentItemfurther below.