-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c03ff98
commit 1054a4a
Showing
4 changed files
with
88 additions
and
71 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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from bs4 import BeautifulSoup | ||
|
||
from marker.schema import BlockTypes | ||
from marker.schema.groups import PageGroup | ||
from marker.schema.registry import get_block_class | ||
from marker.schema.text import Line | ||
|
||
|
||
def add_math_spans_to_line(corrected_text: str, text_line: Line, page: PageGroup): | ||
SpanClass = get_block_class(BlockTypes.Span) | ||
corrected_spans = text_to_spans(corrected_text) | ||
|
||
for span_idx, span in enumerate(corrected_spans): | ||
if span_idx == len(corrected_spans) - 1: | ||
span['content'] += "\n" | ||
|
||
span_block = page.add_full_block( | ||
SpanClass( | ||
polygon=text_line.polygon, | ||
text=span['content'], | ||
font='Unknown', | ||
font_weight=0, | ||
font_size=0, | ||
minimum_position=0, | ||
maximum_position=0, | ||
formats=[span['type']], | ||
url=span.get('url'), | ||
page_id=text_line.page_id, | ||
text_extraction_method="gemini", | ||
) | ||
) | ||
text_line.structure.append(span_block.id) | ||
|
||
|
||
def text_to_spans(text): | ||
soup = BeautifulSoup(text, 'html.parser') | ||
|
||
tag_types = { | ||
'b': 'bold', | ||
'i': 'italic', | ||
'math': 'math', | ||
} | ||
spans = [] | ||
|
||
for element in soup.descendants: | ||
if not len(list(element.parents)) == 1: | ||
continue | ||
|
||
url = element.attrs.get('href') if hasattr(element, 'attrs') else None | ||
|
||
if element.name in tag_types: | ||
spans.append({ | ||
'type': tag_types[element.name], | ||
'content': element.get_text(), | ||
'url': url | ||
}) | ||
elif element.string: | ||
spans.append({ | ||
'type': 'plain', | ||
'content': element.string, | ||
'url': url | ||
}) | ||
|
||
return spans |