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()
else: adoption = segment.set_Glyph
LOG.debug('Resorting lines of page "%s" line "%s" from %s to %s does not suffice to turn "%s" into "%s"', else:
page_id, line.id, raise Exception('invalid element type %s of segment to fix' % type(segment))
str([word.id for word in words]), if not children:
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
word_text = get_text(word)
glyphs_text = get_text(glyphs, '')
if word_text != glyphs_text:
sorted_glyphs = sorted(glyphs, reverse=reverse,
key=lambda g: Polygon(polygon_from_points(g.get_Coords().points)).centroid.x)
sorted_glyphs_text = get_text(sorted_glyphs, '')
if sorted_glyphs_text == word_text:
LOG.info('Fixing glyph order of page "%s" word "%s"', page_id, word.id)
word.set_Glyph(sorted_glyphs)
else:
LOG.debug('Resorting glyphs of page "%s" word "%s" from %s to %s does not suffice to turn "%s" into "%s"',
page_id, word.id,
str([glyph.id for glyph in glyphs]),
str([glyph.id for glyph in sorted_glyphs]),
glyphs_text, word_text)
def _fix_lines(region, page_id, reverse=False):
"""Fix line order in a region"""
lines = region.get_TextLine()
if not lines:
return return
region_text = get_text(region) segment_text = get_text(segment)
lines_text = get_text(lines, '\n') concat_text = get_text(children, joiner)
if region_text != lines_text: if (segment_text != concat_text and
sorted_lines = sorted(lines, reverse=reverse, segment_text.replace(joiner, '') != concat_text.replace(joiner, '')):
key=lambda l: Polygon(polygon_from_points(l.get_Coords().points)).centroid.y) def polygon_position(child, horizontal=sort_horizontal):
sorted_lines_text = get_text(sorted_lines, '\n') polygon = Polygon(polygon_from_points(child.get_Coords().points))
if horizontal:
if (sorted_lines_text == region_text or return polygon.centroid.x
sorted_lines_text.replace('\n', '') == region_text.replace('\n', '')): else:
LOG.info('Fixing line order of page "%s" region "%s"', page_id, region.id) return polygon.centroid.y
region.set_TextLine(sorted_lines) sorted_children = sorted(children, reverse=reverse, key=polygon_position)
sorted_concat_text = get_text(sorted_children, joiner)
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)
adoption(sorted_children)
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