-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #403 from VikParuchuri/dev
Marker Improvements and Bugfixes
- Loading branch information
Showing
39 changed files
with
408 additions
and
144 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,49 @@ | ||
from marker.processors import BaseProcessor | ||
from marker.schema import BlockTypes | ||
from marker.schema.document import Document | ||
|
||
|
||
class BlockquoteProcessor(BaseProcessor): | ||
""" | ||
A processor for tagging blockquotes | ||
""" | ||
block_types = (BlockTypes.Text, BlockTypes.TextInlineMath) | ||
min_x_indent = 0.05 # % of block width | ||
x_start_tolerance = 0.01 # % of block width | ||
x_end_tolerance = 0.01 # % of block width | ||
|
||
def __init__(self, config): | ||
super().__init__(config) | ||
|
||
def __call__(self, document: Document): | ||
for page in document.pages: | ||
for block in page.contained_blocks(document, self.block_types): | ||
if block.structure is None: | ||
continue | ||
|
||
if not len(block.structure) >= 2: | ||
continue | ||
|
||
next_block = page.get_next_block(block) | ||
if next_block is None: | ||
continue | ||
if next_block.block_type not in self.block_types: | ||
continue | ||
if next_block.structure is None: | ||
continue | ||
if next_block.ignore_for_output: | ||
continue | ||
|
||
matching_x_end = abs(next_block.polygon.x_end - block.polygon.x_end) < self.x_end_tolerance * block.polygon.width | ||
matching_x_start = abs(next_block.polygon.x_start - block.polygon.x_start) < self.x_start_tolerance * block.polygon.width | ||
x_indent = next_block.polygon.x_start > block.polygon.x_start + (self.min_x_indent * block.polygon.width) | ||
y_indent = next_block.polygon.y_start > block.polygon.y_end | ||
|
||
if block.blockquote: | ||
next_block.blockquote = (matching_x_end and matching_x_start) or (x_indent and y_indent) | ||
next_block.blockquote_level = block.blockquote_level | ||
if (x_indent and y_indent): | ||
next_block.blockquote_level += 1 | ||
elif len(next_block.structure) >= 2 and (x_indent and y_indent): | ||
next_block.blockquote = True | ||
next_block.blockquote_level = 1 |
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,90 @@ | ||
from typing import List | ||
|
||
from marker.processors import BaseProcessor | ||
from marker.schema import BlockTypes | ||
from marker.schema.blocks import ListItem | ||
from marker.schema.document import Document | ||
|
||
|
||
class ListProcessor(BaseProcessor): | ||
""" | ||
A processor for merging lists across pages and columns | ||
""" | ||
block_types = (BlockTypes.ListGroup,) | ||
ignored_block_types = (BlockTypes.PageHeader, BlockTypes.PageFooter) | ||
min_x_indent = 0.01 # % of page width | ||
|
||
def __init__(self, config): | ||
super().__init__(config) | ||
|
||
def __call__(self, document: Document): | ||
self.list_group_continuation(document) | ||
self.list_group_indentation(document) | ||
|
||
def list_group_continuation(self, document: Document): | ||
for page in document.pages: | ||
for block in page.contained_blocks(document, self.block_types): | ||
next_block = document.get_next_block(block, self.ignored_block_types) | ||
if next_block is None: | ||
continue | ||
if next_block.block_type not in self.block_types: | ||
continue | ||
if next_block.structure is None: | ||
continue | ||
if next_block.ignore_for_output: | ||
continue | ||
|
||
column_break, page_break = False, False | ||
next_block_in_first_quadrant = False | ||
|
||
if next_block.page_id == block.page_id: # block on the same page | ||
# we check for a column break | ||
column_break = next_block.polygon.y_start <= block.polygon.y_end | ||
else: | ||
page_break = True | ||
next_page = document.get_page(next_block.page_id) | ||
next_block_in_first_quadrant = (next_block.polygon.x_start < next_page.polygon.width // 2) and \ | ||
(next_block.polygon.y_start < next_page.polygon.height // 2) | ||
|
||
block.has_continuation = column_break or (page_break and next_block_in_first_quadrant) | ||
|
||
def list_group_indentation(self, document: Document): | ||
for page in document.pages: | ||
for block in page.contained_blocks(document, self.block_types): | ||
if block.structure is None: | ||
continue | ||
if block.ignore_for_output: | ||
continue | ||
|
||
stack: List[ListItem] = [block.get_next_block(page, None)] | ||
for list_item_id in block.structure: | ||
list_item_block: ListItem = page.get_block(list_item_id) | ||
|
||
while stack and list_item_block.polygon.x_start <= stack[-1].polygon.x_start + (self.min_x_indent * page.polygon.width): | ||
stack.pop() | ||
|
||
if stack and list_item_block.polygon.y_start > stack[-1].polygon.y_start: | ||
list_item_block.list_indent_level = stack[-1].list_indent_level | ||
if list_item_block.polygon.x_start > stack[-1].polygon.x_start + (self.min_x_indent * page.polygon.width): | ||
list_item_block.list_indent_level += 1 | ||
|
||
next_list_item_block = block.get_next_block(page, list_item_block) | ||
if next_list_item_block is not None and next_list_item_block.polygon.x_start > list_item_block.polygon.x_end: | ||
stack = [next_list_item_block] # reset stack on column breaks | ||
else: | ||
stack.append(list_item_block) | ||
|
||
stack: List[ListItem] = [block.get_next_block(page, None)] | ||
for list_item_id in block.structure.copy(): | ||
list_item_block: ListItem = page.get_block(list_item_id) | ||
|
||
while stack and list_item_block.list_indent_level <= stack[-1].list_indent_level: | ||
stack.pop() | ||
|
||
if stack: | ||
current_parent = stack[-1] | ||
current_parent.add_structure(list_item_block) | ||
current_parent.polygon = current_parent.polygon.merge([list_item_block.polygon]) | ||
|
||
block.remove_structure_items([list_item_id]) | ||
stack.append(list_item_block) |
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
Oops, something went wrong.