ó
    À‘´j:  ã                  ód  • S r SSKJr  SSKJrJrJrJrJrJ	r	  SSK
JrJr  SSKJr  SSKJr  SSKJrJrJrJr  SSKJrJr  SS	KJr  SS
KJr  SSKJrJrJ r           SS jr!                SS jr"SS jr#          SS jr$SS jr%SS jr&      SS jr'SS jr(SS jr)S S jr*g)!u�  Resolve figure footnotes from two sources and carry them into the document.

The correction agent reads each scanned page's image and reports both ends of
the association: the markers printed on the figures' labels and the note lines
printed on the page. The correction sweep in `quber.core.figures.correct`
records them on the pictures. This module ties marker to note and writes the
result where a reader finds it.

Two sources answer each marker. The agent's image read is one; the parse's own
footnote-labelled text on the page â€” retained by the track's graft â€” is the
other. Both are keyed by the same canonical marker, so `(1)`, `1` and a
superscript one meet. Agreement corroborates the note. Disagreement, an
unresolved marker, and a note no marker points at all become review flags on
the run, never silent drops. A marker the agent judged a cross-reference to a
named section resolves to a heading pointer, the same way a table's does.

Notes are pooled across a page's figures because one printed note block serves
several charts; the marker on each figure decides which notes are its.

The document carries the result the way it does for tables: each resolved note
is inserted as a footnote node right after the picture, so a marker inside a
chart and its note at the page foot are linked in the document itself. The
parse's own footnote text stays where the page prints it.

Placement then goes to the value, not just the chart. The agent reports which
printed label carries each marker, and that label is matched against the
figure's value table; the note attaches to the matching cells' records in the
digest. A marker whose label matches no value is flagged unplaced, and the note
still sits after the picture. No flag is raised when the figure has no digest
record or no value table, or when none of its labels keeps a word other than
a stop word once the marker is stripped. Such a note is left unplaced silently.
é    )Úannotations)ÚAnyÚDictÚListÚOptionalÚSequenceÚTuple)ÚDoclingDocumentÚPictureItem)ÚDocItemLabel)Úlogger)ÚDigestedFigureÚ
PageDigestÚ
PlacedNoteÚResolvedNote)ÚFOOTNOTE_MARKS_FIELDÚFOOTNOTES_FIELD)ÚPageScan)Úlabel_words)Úcanonical_markerÚresolve_footnotesÚsplit_leading_markerc                óŒ  • / n[        U 5      nU R                   Vs0 s H  oUR                  U_M     nnU GHÿ  nUR                  (       d  M  UR                  n[        X(5       V	s0 s H"  o™R                  (       d  M  U	R                  U	_M$     n
n	[        X5      n[        Xv5      u  pÍ[        5       n0 nUR                   GH(  nUR                  UR                  =(       d    S5      nUc  M-  [        UR                  UR                  / 5      5      nU
R                  UR                  =(       d    S5      n	0 n0 n[        UR                  [         S5      =(       d    /  GH  n[#        UR                  S5      =(       d    S5      n[%        U5      nU(       d  M;  UR'                  U5        UU;  aR  [)        UU[#        UR                  S5      =(       d    S5      UUUU5      u  nnU(       a  UR+                  U5        UUU'   UU   nUc  M®  UU;  a&  UR-                  SS9UU'   UR+                  UU   5        UR/                  U/ 5      R+                  [#        UR                  S5      =(       d    S5      5        GM     UR1                  5        H2  u  nnUR3                  [5        UUR                  U/ 5      X˜5      5        M4     U(       d  GM  [7        XU5        U	c  GM!  UU	l        GM+     UR1                  5        H3  u  nu  nnU(       d  M  UU;  d  M  UR+                  S	U S
U< S35        M5     GM     U$ s  snf s  sn	f )u  Resolve every scanned page's figure markers and write the results.

Mutates `document` â€” each picture's resolved notes are inserted after it â€”
and the digests, whose figures receive their resolutions and whose value
cells receive the notes placed on them. Returns the review flags.
Ú NÚmarkerÚkindÚfootnoteT)ÚdeepÚlabelúpage z: the page prints note z* and no marker on its figures points at it)Ú	_headingsÚpicturesÚself_refÚfiguresÚpageÚ_page_figuresÚnode_idÚ_document_notesÚ_agent_notesÚsetÚgetÚpicture_refÚlistÚchunk_idÚgetattrÚmetar   Ústrr   ÚaddÚ_resolveÚappendÚ
model_copyÚ
setdefaultÚitemsÚextendÚ_place_on_valuesÚ_insert_notesÚ	footnotes)ÚdocumentÚscansÚdigestsÚflagsÚheadingsÚpictureÚby_refÚscanr%   ÚfigureÚdigest_figuresÚ
page_notesÚagent_notesÚgeneral_by_refÚ
referencedÚresolutionsÚrecordÚresolvedÚmineÚlabelsÚmarkr   ÚkeyÚnoteÚflagÚtemplateÚ_texts                              Ú?/home/mande/repo/quber/src/quber/core/figures/dpt3/footnotes.pyÚresolve_figure_footnotesrV   1   só  € ð €EÜ˜Ó"€HØ7?×7HÒ7HÓIÒ7H¨G×Ñ Ò'Ñ7H€FÐIäˆØ�|�|ÙØ�y‰yˆÜ?LÈWÔ?[ÓnÒ?[°V×_mÕ_mÓ0˜&Ÿ.™.¨&Ò0Ñ?[ˆÐnô % XÓ4ˆ
Ü&2°4Ó&@Ñ#ˆÜ"›uˆ
ð
 :<ˆà—l•lˆFØ—j‘j ×!3Ñ!3×!9°rÓ:ˆGØ‰ÙÜ+/°×0BÑ0BÀ7×CSÑCSÐUWÓ0XÓ+YˆHØ#×'Ñ'¨¯©×(=¸2Ó>ˆFØ,.ˆDØ+-ˆFÜ §¡Ô.BÀDÓI×OÈRÓO�Ü˜TŸX™X hÓ/×5°2Ó6�Ü& vÓ.�ÞÙØ—‘˜sÔ#Ø˜kÓ)Ü!)ØØÜ˜DŸH™H VÓ,×:°
Ó;Ø#Ø"Ø Øó"‘J�D˜$ö ØŸ™ TÔ*Ø'+�K Ñ$Ø& sÑ+�ØÑ#Ùð ˜d“?Ø (× 3Ñ 3¸Ð 3Ð >�D˜‘IØ—O‘O D¨¡IÔ.Ø×!Ñ! # rÓ*×1Ñ1´#°d·h±h¸wÓ6G×6MÈ2Ó2N×Oñ7 Pð8 "ŸZ™Zž\‘	��TØ—‘Ô-¨d°F·J±J¸sÀBÓ4GÈÓVÖWñ *ç‰xÜ˜h°Ô:ØÔ%Ø'/�F×$ñS #ðV %0×$5Ñ$5Ö$7Ñ ˆC‘�&˜%ßˆs�s *Õ,Ø—‘Ø˜D˜6Ð!8¸¹
ð C+ð +öô %8ñw ðD €LùòI Jùò os   œL<Á&MÁ>Mc           	     óþ  • US:X  aO  [        U // XeU1S9nUR                  (       a$  [        XR                  S   R                  S/S9S4$ SSU SU < S	34$ UR	                  U5      nUR	                  U5      n	Uc  U	c  SSU SU < S
34$ Uc  [        X	=(       d    SS/S9S4$ U	c  [        XS   S/S9S4$ [        US   U	5      (       a  [        XS   SS/SS9S4$ [        XS   SS/S9SU SU < SUS   < SU	< 34$ )zCOne marker's resolution and, when something needs review, its flag.Úsection)r@   Úsection_keysr   r@   ©r   ÚtextÚsourcesNr    z: figure marker z= names a section of the document and no heading opens with itug    is defined by no note â€” neither the agent's read of the page nor the page's own footnote text has itr   r<   é   ÚagentT)r   r[   r\   Úcorroboratedú: note u9    reads differently in the two sources â€” the agent read z, the page's own text says )r   rL   r   r[   r+   Ú_agree)
r   rP   r   rG   rF   r@   r%   Úpointerr^   Úprinteds
             rU   r3   r3   …   s{  € ð ˆyÓÜ# V H¨b°$ÐY\ÐX]Ñ^ˆØ××Ü v×4DÑ4DÀQÑ4G×4LÑ4LÐWaÐVbÑcÐeiÐiÐiØØ�D�6Ð)¨&©ð 4$ð $ð
ð 	
ð
 �O‰O˜CÓ €EØ�n‰n˜SÓ!€GØ�}˜™ØØ�D�6Ð)¨&©ð 4Oð Oð
ð 	
ð �}Ü 6·¸2È
À|ÑTÐVZÐZÐZØ�Ü 6°a±À7À)ÑLÈdÐRÐRÜˆe�A‰h˜× Ñ ä °1©XÀÈÐ?TÐcgÑhØð
ð 	
ô
 	˜F¨q©¸GÀZÐ;PÑQØ
�ˆv�W˜V™JÐ&_Ø�‰8‰,Ð1°'±ð	>ðð ó    c                ón  • SR                  U R                  5       5      R                  5       R                  S5      nSR                  UR                  5       5      R                  5       R                  S5      n[	        U5      =(       a,    [	        U5      =(       a    X#:H  =(       d    X#;   =(       d    X2;   $ )z¯Do two reads of one note say the same thing?

Whitespace and case are rendering; one read extending the other is the
same note cut at a different point, not a different note.
Ú Ú.)ÚjoinÚsplitÚcasefoldÚrstripÚbool)ÚoneÚotherÚaÚbs       rU   ra   ra   ¯   s|   € ð 	�‰�—‘“Ó×&Ñ&Ó(×/Ñ/°Ó4€AØ�‰�—‘“Ó×(Ñ(Ó*×1Ñ1°#Ó6€AÜ�‹7×?”t˜A“w×? A¡F×$>¨a©f×$>¸¹Ð?rd   c                óH  • Ub  UR                   (       d  / $ SnU H»  n[        UR                  U R                  S5      5      nU(       d  M1  SnUR                    Hx  nUR                   He  nU H\  n	U[        U	R
                  5      ::  d  M  U	R                  R                  [        U R                  U R
                  S95        SU l	        M^     Mg     Mz     M½     U R                  (       d  U(       d  / $ SR                  S U 5       5      n
SU SU R                  < S	U
 S
3/$ )a<  Attach the note to the value cells whose text carries one of its labels.

A label the marker sits on is the join: every word of it (minus the marker
itself) must appear in the cell's text. The marker follows its label
whether the model drew one figure or two around it. A note none of its
labels place is flagged, and it stays on the picture; a marker printed on
a figure's title has no value to sit on, which is what the flag records.
Nothing is flagged when the figure has no value table, or when no label
keeps a word other than a stop word once the marker is removed.
Fr   T)r   r[   ú, c              3  óH   #   • U  H  o(       d  M  [        U5      v •  M     g 7f©N)Úrepr)Ú.0r   s     rU   Ú	<genexpr>Ú#_place_on_values.<locals>.<genexpr>Ú   s   é € ÐA²¨»5›œ˜UŸ˜²ùs   ‚
"�"r    r`   z	 sits on z\ and the figure's value table has no value under that label; the note is on the picture only)Úvaluesr   Úreplacer   Úrowsr[   r;   r4   r   Úplacedrh   )rQ   rN   rD   r%   Útriedr   ÚwordsÚtableÚrowÚcellrc   s              rU   r9   r9   º   sú   € ð  �~˜VŸ]Ÿ]Øˆ	Ø€EÛˆÜ˜EŸM™M¨$¯+©+°rÓ:Ó;ˆÞÙØˆØ—]”]ˆEØ—z”z�Û�DØ¤¨D¯I©IÓ 6Õ6ØŸ™×-Ñ-¬jÀÇÁÐRV×R[ÑR[Ñ.\Ô]Ø&*˜žó  ó "ó #ñ ð ‡{‡{ž%Øˆ	Ø�i‰iÑA±ÓAÓA€Gà
�ˆv�W˜TŸ[™[™O¨9°W°Ið >Ið 	Iðð rd   c           	     ó¶  • [        X5      u  p4U H„  nUR                  (       a)  UR                   SUR                   3R                  5       OUR                  nUR                  5       (       a  Xd;   a  Me  U R	                  [
        R                  XcSS9nM†     [        R                  " SUR                  [        U5      SR                  S U 5       5      5        g)u÷  Insert each resolved note as a footnote node right after the picture.

The same carriage a table's footnotes get: a body node after the element
renders as a paragraph below it and keeps the reading order. The node
carries no provenance box, so nothing sweeping printed regions matches it.

A note already sitting after the picture is not inserted again, so a rerun
over an enriched parse â€” which is what reusing stored scans produces â€”
leaves the document as it was rather than doubling every note.
rf   T)r   r[   ÚsiblingÚafterz=Figure footnotes: picture {} carries {} resolved note(s) ({})rr   c              3  óJ   #   • U  H  oR                   =(       d    S v •  M     g7f)ÚunmarkedN)r   )rv   rQ   s     rU   rw   Ú _insert_notes.<locals>.<genexpr>ö   s   é € Ð>º°—+‘+×+ Ô+ºùs   ‚!#N)Ú_trailing_notesr   r[   ÚstripÚinsert_textr   ÚFOOTNOTEr   Úinfor#   Úlenrh   )r<   rA   ÚnotesÚanchorÚexistingrQ   r[   s          rU   r:   r:   á   s¬   € ô ' xÓ9Ñ€FÛˆØ7;·{·{�$—+‘+�˜a §	¡	˜{Ð+×1Ñ1Ô3ÈÏ	É	ˆØ�z‰z�|‰|˜tÓ/ÙØ×%Ñ%¬L×,AÑ,AÈÐdhÐ%ÐiŠñ	 ô
 ‡K‚KØGØ×ÑÜˆE‹
Ø�	‰	Ñ>¹Ó>Ó>õ	rd   c                ó|  • Un[        5       nUR                  (       a  UR                  R                  U 5      OSnUc  X#4$ UR                   Vs/ s H  oUR                  PM     nnUR
                  U;  a  X#4$ XfR                  UR
                  5      S-   S  H™  nUR                  S5      (       a+  U R                  [        UR                  SS5      S   5         OSnUb/  UR                  [        R                  :w  d  UR                  (       a    X#4$ UR                  UR                   5        UnM›     X#4$ s  snf )u^  The run of footnote nodes already following the picture: the last one,
to anchor new insertions behind it, and their texts, to skip re-inserting.

Only a prov-less footnote belongs to the run â€” one this module inserted on
an earlier pass. A printed footnote the parse positioned on the page never
matches, so it is never mistaken for an insertion.
Nr]   z#/texts/Ú/)r*   ÚparentÚresolveÚchildrenÚcrefr#   ÚindexÚ
startswithÚtextsÚintÚrsplitr   r   r‹   Úprovr2   r[   )	r<   rA   r�   r�   r“   ÚchildÚrefsÚrefÚitems	            rU   rˆ   rˆ   ú   s  € ð €FÜ›€HØ18··ˆW�^‰^×#Ñ# HÔ-ÀT€FØ�~ØÐÐØ$*§O¢OÓ4¢O˜5�JŒJ¡O€DÐ4Ø×Ñ˜tÓ#ØÐÐØ—J‘J˜w×/Ñ/Ó0°1Ñ4Ð6Ó7ˆØ=@¿^¹^ÈJ×=WÑ=Wˆx�~‰~œc #§*¡*¨S°!Ó"4°QÑ"7Ó8Ò9Ð]aˆØ‰<˜4Ÿ:™:¬×)>Ñ)>Ó>À$Ç)Ç)Øð ÐÐð 	�‰�T—Y‘YÔØŠñ 8ð ÐÐùò 5s   ÁD9c           
     óT  • 0 n0 nU R                    GH  nUR                  UR                  =(       d    S5      nUc  M-  [        UR                  [
        S5      =(       d    /  H¼  n[        UR                  S5      =(       d    S5      n[        UR                  S5      =(       d    S5      R                  5       nU(       d  M`  [        U5      n	U	(       a  UR                  X—U45        M‡  UR                  UR                  / 5      R                  [        SUS/S95        M¾     GM     X#4$ )uU  The notes the agent read off this page, pooled, plus the unmarked ones.

Marked notes pool across the page's figures â€” one printed note block
serves several charts â€” keyed by canonical marker, each keeping its
printed marker and text. An unmarked general note has no marker to pool
under and stays with the figure the agent read it for.
r   Nr   r[   r^   rZ   )r$   r+   r,   r/   r0   r   r1   r‰   r   r6   r#   r4   r   )
rC   rB   ÚpooledÚgeneralrK   rA   Úentryr   r[   rP   s
             rU   r)   r)     sô   € ð *,€FØ-/€GØ—,•,ˆØ—*‘*˜V×/Ñ/×5°2Ó6ˆØ‰?ÙÜ˜WŸ\™\¬?¸DÓA×GÀRÒGˆEÜ˜Ÿ™ 8Ó,×2°Ó3ˆFÜ�u—y‘y Ó(×.¨BÓ/×5Ñ5Ó7ˆDÞÙÜ" 6Ó*ˆCÞØ×!Ñ! #° ~Ö6à×"Ñ" 7×#3Ñ#3°RÓ8×?Ñ?Ü ¨°À¸yÑIöô Hñ	 ð  ˆ?Ðrd   c                óF  • 0 nU R                    HŽ  nUR                  [        R                  :w  a  M#  UR                  (       a  UR                  S   R
                  U:w  a  MS  [        UR                  =(       d    S5      nUc  Mv  UR                  US   US   5        M�     U$ )zÛThe page's own footnote text, keyed by the marker each line opens with.

These are the footnote-labelled items the parse produced and the track's
graft retained. A line opening with no marker defines nothing to key on.
r   r   r]   )	r™   r   r   r‹   rœ   Úpage_nor   r[   r6   )r<   r%   rŽ   r    Úparseds        rU   r(   r(   2  s‚   € ð €EØ—”ˆØ�:‰:œ×.Ñ.Ó.ÙØ�y�y˜DŸI™I a™L×0Ñ0°DÓ8ÙÜ% d§i¡i§o°2Ó6ˆØÓØ×Ñ˜V A™Y¨¨q©	Ö2ñ ð €Lrd   c                óN  • / nU R                    H’  nUR                  [        R                  :w  d(  UR                  =(       d    SR                  5       (       d  MK  UR                  UR                  UR                  (       a  UR                  S   R                  OS45        M”     U$ )zGThe document's section headings with their pages, for section pointers.r   r   )	r™   r   r   ÚSECTION_HEADERr[   r‰   r4   rœ   r¦   )r<   Úfoundr    s      rU   r!   r!   D  sr   € à#%€EØ—”ˆØ�:‰:œ×4Ñ4Ó4¸T¿Y¹Y¿_È"×<SÑ<S×<UÑ<UÙØ�‰�d—i‘i¸¿¿ §¡¨1¡×!5Ò!5ÈÐJÖKñ ð €Lrd   c                óX   • U R                  U5      nUb  [        UR                  5      $ / $ rt   )r+   r-   r$   )r>   r%   Údigests      rU   r&   r&   N  s)   € Ø�[‰[˜Ó€FØ#)Ñ#5Œ4�—‘ÓÐ=¸2Ð=rd   N)r<   r
   r=   zSequence[PageScan]r>   úDict[int, PageDigest]Úreturnú	List[str])r   r1   rP   r1   r   r1   rG   zDict[str, Tuple[str, str]]rF   úDict[str, str]r@   úList[Tuple[str, int]]r%   rš   r®   z,Tuple[Optional[ResolvedNote], Optional[str]])rm   r1   rn   r1   r®   rl   )
rQ   r   rN   zSequence[str]rD   zOptional[DigestedFigure]r%   rš   r®   r¯   )r<   r
   rA   r   rŽ   zSequence[ResolvedNote]r®   ÚNone)r<   r
   rA   r   r®   zTuple[Any, set[str]])rC   r   rB   zDict[str, PictureItem]r®   z@Tuple[Dict[str, Tuple[str, str]], Dict[str, List[ResolvedNote]]])r<   r
   r%   rš   r®   r°   )r<   r
   r®   r±   )r>   r­   r%   rš   r®   zList[DigestedFigure])+Ú__doc__Ú
__future__r   Útypingr   r   r   r   r   r	   Údocling_core.types.doc.documentr
   r   Údocling_core.types.doc.labelsr   Úlogurur   Úquber.core.figures.dpt3.modelsr   r   r   r   Úquber.core.figures.graftr   r   Úquber.core.figures.modelsr   Úquber.core.figures.valuesr   Úquber.core.fusion.footnotesr   r   r   rV   r3   ra   r9   r:   rˆ   r)   r(   r!   r&   © rd   rU   Ú<module>r¿      s%  ðñõB #ç =× =ç HÝ 6Ý ç _Ó _ß JÝ .Ý 1ß aÑ aðQØðQàðQð #ðQð ô	Qðh'Øð'à	ð'ð ð'ð ,ð	'ð
 ð'ð $ð'ð ð'ð 2ô'ôT@ð$Ø
ð$àð$ð %ð$ð ð	$ð
 ô$ôNô2ð2Ø
ðØ2ðàEôô>ô$õ>rd   