-
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 #404 from VikParuchuri/dev-mose/blockquote-processor
Add Blockquote Processor
- Loading branch information
Showing
5 changed files
with
76 additions
and
11 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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.block_type in self.block_types and 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 | ||
else: | ||
next_block.blockquote = len(next_block.structure) >= 2 and (x_indent and y_indent) | ||
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