-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use new document output structure (#3)
* Added new models to represent the document with a new structure * Fixed boundingBox's model * Update README.md * Update README.md * Update README.md * Fixed code after merge request review (MR !2) * fixed a bug in `pdfact_to_document` * fixed a bug in `pdfact_to_document` * Added method that filters headings * Imports refactor --------- Co-authored-by: AnnaMarika01 <[email protected]> Co-authored-by: Andrea Ponti <[email protected]>
- Loading branch information
1 parent
546120a
commit e4dd97f
Showing
11 changed files
with
192 additions
and
104 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
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
from .chunk import Chunk | ||
from .color import Color | ||
from .document import Document | ||
from .document import Document, Attributes, BoundingBox, Content, NodeAttributes, Node | ||
from .font import Font | ||
from .paragraph import Paragraph, Metadata | ||
from .position import Position |
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 |
---|---|---|
@@ -1,13 +1,40 @@ | ||
from typing import List, Optional | ||
from typing import List, Union | ||
|
||
from pydantic import BaseModel | ||
from pydantic import BaseModel, Field | ||
from typing_extensions import TypedDict | ||
|
||
from text_extractor.models.color import Color | ||
from text_extractor.models.font import Font | ||
from text_extractor.models.paragraph import Paragraph | ||
from text_extractor.models.marks import Mark, TextStyleMark | ||
|
||
|
||
class BoundingBox(TypedDict): | ||
min_x: float | ||
min_y: float | ||
max_x: float | ||
max_y: float | ||
page: int | ||
|
||
|
||
class Attributes(BaseModel): | ||
bounding_box: List[BoundingBox] = [] | ||
|
||
|
||
class Content(BaseModel): | ||
role: str = "body" | ||
text: str | ||
marks: List[Union[Mark, TextStyleMark]] = [] | ||
attributes: Attributes = Attributes() | ||
|
||
|
||
class NodeAttributes(BaseModel): | ||
page: int | ||
|
||
|
||
class Node(BaseModel): | ||
category: str = Field("page") | ||
attributes: NodeAttributes | ||
content: List[Content] | ||
|
||
|
||
class Document(BaseModel): | ||
fonts: Optional[List[Font]] = None | ||
text: List[Paragraph] | ||
colors: Optional[List[Color]] = None | ||
type: str = Field("doc") | ||
content: List[Node] |
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
from pydantic import BaseModel, Field | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Font(BaseModel): | ||
name: str | ||
id: str | ||
is_bold: bool = Field(False, alias='is-bold') | ||
is_type3: bool = Field(False, alias='is-type3') | ||
is_italic: bool = Field(False, alias='is-italic') | ||
size: Optional[int] = None |
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,39 @@ | ||
from typing import Any | ||
from typing import Literal, Optional | ||
|
||
from pydantic import BaseModel, model_validator | ||
|
||
from text_extractor.models.color import Color | ||
from text_extractor.models.font import Font | ||
|
||
|
||
class Mark(BaseModel): | ||
category: Literal['bold', 'italic', 'textStyle', 'link'] | ||
|
||
@model_validator(mode='before') | ||
@classmethod | ||
def check_details(cls, data: Any) -> Any: | ||
mark_type = data.get('type') | ||
|
||
if mark_type == 'textStyle': | ||
if 'color' not in data and 'font' not in data: | ||
raise ValueError('color or font must be provided when type is textStyle') | ||
if 'url' in data: | ||
raise ValueError('url should not be provided when type is textStyle') | ||
|
||
elif mark_type == 'link': | ||
if 'url' not in data: | ||
raise ValueError('url must be provided when type is link') | ||
if 'textStyle' in data: | ||
raise ValueError('textStyle should not be provided when type is link') | ||
|
||
return data | ||
|
||
|
||
class TextStyleMark(Mark): | ||
color: Optional[Color] = None | ||
font: Optional[Font] = None | ||
|
||
|
||
class UrlMark(Mark): | ||
url: str |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1 +0,0 @@ | ||
|
||
Oops, something went wrong.