unify function for 3 levels

pull/6/head
Robert Sachunsky 5 years ago
parent f829015bb5
commit 9002606e1c

@ -6,6 +6,7 @@ from collections import Sequence
from ocrd import Processor from ocrd import Processor
from ocrd_modelfactory import page_from_file from ocrd_modelfactory import page_from_file
from ocrd_models.ocrd_page import ( from ocrd_models.ocrd_page import (
TextRegionType, TextLineType, WordType,
to_xml to_xml
) )
from ocrd_utils import ( from ocrd_utils import (
@ -49,7 +50,7 @@ class RepairInconsistencies(Processor):
page_id, region.id, textLineOrder) page_id, region.id, textLineOrder)
continue continue
_fix_lines(region, page_id, reverse=textLineOrder=='bottom-to-top') _fix_segment(region, page_id, reverse=textLineOrder=='bottom-to-top')
lines = region.get_TextLine() lines = region.get_TextLine()
for line in lines: for line in lines:
@ -65,7 +66,7 @@ class RepairInconsistencies(Processor):
page_id, line.id, readingDirection) page_id, line.id, readingDirection)
continue continue
_fix_words(line, page_id, reverse=readingDirection=='right-to-left') _fix_segment(line, page_id, reverse=readingDirection=='right-to-left')
words = line.get_Word() words = line.get_Word()
for word in words: for word in words:
@ -81,7 +82,7 @@ class RepairInconsistencies(Processor):
page_id, word.id, readingDirection) page_id, word.id, readingDirection)
continue continue
_fix_glyphs(word, page_id, reverse=readingDirection=='right-to-left') _fix_segment(word, page_id, reverse=readingDirection=='right-to-left')
file_id = input_file.ID.replace(self.input_file_grp, self.output_file_grp) file_id = input_file.ID.replace(self.input_file_grp, self.output_file_grp)
if file_id == input_file.ID: if file_id == input_file.ID:
@ -113,75 +114,48 @@ def get_text(thing, joiner=None):
return text return text
def _fix_words(line, page_id, reverse=False): def _fix_segment(segment, page_id, reverse=False):
"""Fix word order in a line""" """Fix order of child elements of (region/line/word) segment."""
words = line.get_Word() if isinstance(segment, TextRegionType):
if not words: joiner = '\n'
return sort_horizontal = False
line_text = get_text(line) children = segment.get_TextLine()
words_text = get_text(words, ' ') adoption = segment.set_TextLine
if line_text != words_text: elif isinstance(segment, TextLineType):
sorted_words = sorted(words, reverse=reverse, joiner = ' '
key=lambda w: Polygon(polygon_from_points(w.get_Coords().points)).centroid.x) sort_horizontal = True
sorted_words_text = get_text(sorted_words, ' ') children = segment.get_Word()
adoption = segment.set_Word
if (sorted_words_text == line_text or elif isinstance(segment, WordType):
sorted_words_text.replace(' ', '') == line_text.replace(' ', '')): joiner = ''
LOG.info('Fixing word order of page "%s" line "%s"', page_id, line.id) sort_horizontal = True
line.set_Word(sorted_words) children = segment.get_Glyph()
adoption = segment.set_Glyph
else: else:
LOG.debug('Resorting lines of page "%s" line "%s" from %s to %s does not suffice to turn "%s" into "%s"', raise Exception('invalid element type %s of segment to fix' % type(segment))
page_id, line.id, if not children:
str([word.id for word in words]),
str([word.id for word in sorted_words]),
words_text, line_text)
def _fix_glyphs(word, page_id, reverse=False):
"""Fix glyph order in a word"""
glyphs = word.get_Glyph()
if not glyphs:
return return
word_text = get_text(word) segment_text = get_text(segment)
glyphs_text = get_text(glyphs, '') concat_text = get_text(children, joiner)
if word_text != glyphs_text: if (segment_text != concat_text and
sorted_glyphs = sorted(glyphs, reverse=reverse, segment_text.replace(joiner, '') != concat_text.replace(joiner, '')):
key=lambda g: Polygon(polygon_from_points(g.get_Coords().points)).centroid.x) def polygon_position(child, horizontal=sort_horizontal):
sorted_glyphs_text = get_text(sorted_glyphs, '') polygon = Polygon(polygon_from_points(child.get_Coords().points))
if horizontal:
if sorted_glyphs_text == word_text: return polygon.centroid.x
LOG.info('Fixing glyph order of page "%s" word "%s"', page_id, word.id)
word.set_Glyph(sorted_glyphs)
else: else:
LOG.debug('Resorting glyphs of page "%s" word "%s" from %s to %s does not suffice to turn "%s" into "%s"', return polygon.centroid.y
page_id, word.id, sorted_children = sorted(children, reverse=reverse, key=polygon_position)
str([glyph.id for glyph in glyphs]), sorted_concat_text = get_text(sorted_children, joiner)
str([glyph.id for glyph in sorted_glyphs]),
glyphs_text, word_text) if (segment_text == sorted_concat_text or
segment_text.replace(joiner, '') == sorted_concat_text.replace(joiner, '')):
LOG.info('Fixing element order of page "%s" segment "%s"', page_id, segment.id)
def _fix_lines(region, page_id, reverse=False): adoption(sorted_children)
"""Fix line order in a region"""
lines = region.get_TextLine()
if not lines:
return
region_text = get_text(region)
lines_text = get_text(lines, '\n')
if region_text != lines_text:
sorted_lines = sorted(lines, reverse=reverse,
key=lambda l: Polygon(polygon_from_points(l.get_Coords().points)).centroid.y)
sorted_lines_text = get_text(sorted_lines, '\n')
if (sorted_lines_text == region_text or
sorted_lines_text.replace('\n', '') == region_text.replace('\n', '')):
LOG.info('Fixing line order of page "%s" region "%s"', page_id, region.id)
region.set_TextLine(sorted_lines)
else: else:
LOG.debug('Resorting lines of page "%s" region "%s" from %s to %s does not suffice to turn "%s" into "%s"', LOG.debug('Resorting children of page "%s" segment "%s" from %s to %s does not suffice to turn "%s" into "%s"',
page_id, region.id, page_id, segment.id,
str([line.id for line in lines]), str([seg.id for seg in children]),
str([line.id for line in sorted_lines]), str([seg.id for seg in sorted_children]),
lines_text, region_text) concat_text, segment_text)

Loading…
Cancel
Save