Rename member for clarity, fixed unit test

_textlines_alignments becomes _textline_to_alignments
pull/153/head
Frh 2020-04-25 17:15:16 -07:00
parent a0e46916e2
commit dbaab66e43
4 changed files with 12 additions and 12 deletions

View File

@ -216,7 +216,7 @@ class TextNetworks(TextAlignments):
super().__init__(ALL_ALIGNMENTS) super().__init__(ALL_ALIGNMENTS)
# For each textline, dictionary "alignment type" to # For each textline, dictionary "alignment type" to
# "number of textlines aligned" # "number of textlines aligned"
self._textlines_alignments = {} self._textline_to_alignments = {}
def _update_alignment(self, alignment, coord, textline): def _update_alignment(self, alignment, coord, textline):
alignment.register_aligned_textline(textline, coord) alignment.register_aligned_textline(textline, coord)
@ -236,11 +236,11 @@ class TextNetworks(TextAlignments):
for align_id, textedges in self._text_alignments.items(): for align_id, textedges in self._text_alignments.items():
for textedge in textedges: for textedge in textedges:
for textline in textedge.textlines: for textline in textedge.textlines:
alignments = self._textlines_alignments.get( alignments = self._textline_to_alignments.get(
textline, None) textline, None)
if alignments is None: if alignments is None:
alignments = AlignmentCounter() alignments = AlignmentCounter()
self._textlines_alignments[textline] = alignments self._textline_to_alignments[textline] = alignments
alignments[align_id] = textedge.textlines alignments[align_id] = textedge.textlines
def _calculate_gaps_thresholds(self, percentile=75): def _calculate_gaps_thresholds(self, percentile=75):
@ -301,12 +301,12 @@ class TextNetworks(TextAlignments):
for ta in textalignments: for ta in textalignments:
for i in range(len(ta.textlines) - 1, -1, -1): for i in range(len(ta.textlines) - 1, -1, -1):
tl = ta.textlines[i] tl = ta.textlines[i]
alignments = self._textlines_alignments[tl] alignments = self._textline_to_alignments[tl]
if alignments.max_h_count() <= 1 or \ if alignments.max_h_count() <= 1 or \
alignments.max_v_count() <= 1: alignments.max_v_count() <= 1:
del ta.textlines[i] del ta.textlines[i]
removed_singletons = True removed_singletons = True
self._textlines_alignments = {} self._textline_to_alignments = {}
self._compute_alignment_counts() self._compute_alignment_counts()
def most_connected_textline(self): def most_connected_textline(self):
@ -316,9 +316,9 @@ class TextNetworks(TextAlignments):
""" """
# Find the textline with the highest alignment score # Find the textline with the highest alignment score
return max( return max(
self._textlines_alignments.keys(), self._textline_to_alignments.keys(),
key=lambda textline: key=lambda textline:
self._textlines_alignments[textline].alignment_score(), self._textline_to_alignments[textline].alignment_score(),
default=None default=None
) )
@ -342,7 +342,7 @@ class TextNetworks(TextAlignments):
# Retrieve the list of textlines it's aligned with, across both # Retrieve the list of textlines it's aligned with, across both
# axis # axis
best_alignment = self._textlines_alignments[most_aligned_tl] best_alignment = self._textline_to_alignments[most_aligned_tl]
ref_h_alignment_id, ref_h_textlines = best_alignment.max_h() ref_h_alignment_id, ref_h_textlines = best_alignment.max_h()
ref_v_alignment_id, ref_v_textlines = best_alignment.max_v() ref_v_alignment_id, ref_v_textlines = best_alignment.max_v()
if len(ref_v_textlines) <= 1 or len(ref_h_textlines) <= 1: if len(ref_v_textlines) <= 1 or len(ref_h_textlines) <= 1:
@ -416,7 +416,7 @@ class TextNetworks(TextAlignments):
# For the body of the table, we only consider cells with alignments # For the body of the table, we only consider cells with alignments
# on both axis. # on both axis.
tls_search_space = list(self._textlines_alignments.keys()) tls_search_space = list(self._textline_to_alignments.keys())
# tls_search_space = [] # tls_search_space = []
tls_search_space.remove(most_aligned_tl) tls_search_space.remove(most_aligned_tl)
tls_in_bbox = [most_aligned_tl] tls_in_bbox = [most_aligned_tl]

View File

@ -306,7 +306,7 @@ class PlotMethods(object):
alpha=0.5 alpha=0.5
) )
) )
for tl, alignments in network._textlines_alignments.items(): for tl, alignments in network._textline_to_alignments.items():
coords = get_textline_coords(tl) coords = get_textline_coords(tl)
alignment_id_h, tls_h = alignments.max_v() alignment_id_h, tls_h = alignments.max_v()
alignment_id_v, tls_v = alignments.max_h() alignment_id_v, tls_v = alignments.max_h()
@ -325,7 +325,7 @@ class PlotMethods(object):
ax.plot( ax.plot(
[coords[alignment_id_h]] * len(ys), ys, [coords[alignment_id_h]] * len(ys), ys,
color="green", color="green",
linestyle="dashed", linestyle="solid",
linewidth=1, linewidth=1,
marker="o", marker="o",
markersize=3 markersize=3
@ -344,7 +344,7 @@ class PlotMethods(object):
ax.plot( ax.plot(
xs, [coords[alignment_id_v]] * len(xs), xs, [coords[alignment_id_v]] * len(xs),
color="blue", color="blue",
linestyle="dotted", linestyle="solid",
linewidth=1, linewidth=1,
marker="o", marker="o",
markersize=3 markersize=3

Binary file not shown.

Before

Width:  |  Height:  |  Size: 102 KiB

After

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 164 KiB

After

Width:  |  Height:  |  Size: 113 KiB