§
    ’©ºj�&  ã                   ó€  — d dl Z d dlZd dlmZ d dlmZmZmZ d dlm	Z	m
Z
 d dlmZ eeef         Zeeee         f         Zeeeeeeeee         ee         f         f         f         f         Zeeee         f         Zeeeeeeef         f                  eeeeee         ee         f         f         f         Zdgdgdœd	d
dddœZdedefd„Zdedee         fd„Zdedede	fd„Zdedede	fd„Zdeeege	f         dedee         de	fd„Zdededeeeef         eeeeeeeeeef                  f                  f                  f         fd„Zdeeef         deeeeeeeeeef                  f                  f                  dee	e	e	f         fd„Zd e	d!e	d"e	deee	f         fd#„Zdededeee	f         fd$„Z dS )%é    N)ÚCounter)ÚAnyÚCallableÚUnion)ÚTensorÚtensor)Úrank_zero_warné   zThis is a test text)Úanswer_startÚtextzThis is a test context.Ú1zIs this a test?z
train test)ÚanswersÚcontextÚidÚquestionÚtitleÚsÚreturnc           	      óÞ   — dt           dt           fd„}dt           dt           fd„}dt           dt           fd„}dt           dt           fd„} | | | || ¦  «        ¦  «        ¦  «        ¦  «        S )zALower text and remove punctuation, articles and extra whitespace.r   r   c                 ó.   — t          j        dd| ¦  «        S )Nz\b(a|an|the)\bú )ÚreÚsub©r   s    úr/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/text/squad.pyÚremove_articlesz(_normalize_text.<locals>.remove_articles,   s   € ÝŒvÐ'¨¨dÑ3Ô3Ð3ó    c                 óP   — d                      |                      ¦   «         ¦  «        S )Nr   )ÚjoinÚsplitr   s    r   Úwhite_space_fixz(_normalize_text.<locals>.white_space_fix/   s   € Ø�xŠx˜Ÿ
š
™œÑ%Ô%Ð%r   c                 óx   ‡— t          t          j        ¦  «        Šd                     ˆfd„| D ¦   «         ¦  «        S )NÚ c              3   ó$   •K  — | ]
}|‰v¯|V — Œd S ©N© )Ú.0ÚchÚexcludes     €r   ú	<genexpr>z7_normalize_text.<locals>.remove_punc.<locals>.<genexpr>4   s-   øè è € Ð>Ð>˜b¨B°gÐ,=Ð,=�rÐ,=Ð,=Ð,=Ð,=Ð>Ð>r   )ÚsetÚstringÚpunctuationr   )r   r)   s    @r   Úremove_puncz$_normalize_text.<locals>.remove_punc2   s:   ø€ Ý•fÔ(Ñ)Ô)ˆØ�wŠwÐ>Ð>Ð>Ð> DÐ>Ñ>Ô>Ñ>Ô>Ð>r   c                 ó*   — |                       ¦   «         S r%   )Úlowerr   s    r   r0   z_normalize_text.<locals>.lower6   s   € Ø�zŠz‰|Œ|Ðr   )Ústr)r   r   r!   r.   r0   s        r   Ú_normalize_textr2   )   s¸   € ð4�cð 4¥cð 4ð 4ð 4ð 4ð&�cð &¥cð &ð &ð &ð &ð?�#ð ?¥#ð ?ð ?ð ?ð ?ð•Cð �Cð ð ð ð ð ˆ?˜?˜?¨;¨;°u°u¸Q±x´xÑ+@Ô+@ÑAÔAÑBÔBÐBr   c                 óL   — | sg n t          | ¦  «                             ¦   «         S )z&Split a sentence into separate tokens.)r2   r    )r   s    r   Ú_get_tokensr4   <   s%   € àÐ6ˆ2ˆ2�O¨AÑ.Ô.×4Ò4Ñ6Ô6Ð6r   Úpredicted_answerÚtarget_answerc                 ó2  — t          |¦  «        }t          | ¦  «        }t          |¦  «        t          |¦  «        z  }t          t          |                     ¦   «         ¦  «        ¦  «        }t          |¦  «        dk    st          |¦  «        dk    r t          t          ||k    ¦  «        ¦  «        S |dk    rt          d¦  «        S d|z  t          t          |¦  «        ¦  «        z  }d|z  t          t          |¦  «        ¦  «        z  }d|z  |z  ||z   z  S )z#Compute F1 Score for two sentences.r   ç        g      ð?é   )r4   r   r   ÚsumÚvaluesÚlenÚint)r5   r6   Útarget_tokensÚpredicted_tokensÚcommonÚnum_sameÚ	precisionÚrecalls           r   Ú_compute_f1_scorerD   A   s  € å Ñ.Ô.€MÝ"Ð#3Ñ4Ô4ÐÝ�]Ñ#Ô#¥gÐ.>Ñ&?Ô&?Ñ?€FÝ•c˜&Ÿ-š-™/œ/Ñ*Ô*Ñ+Ô+€HÝ
ˆ=ÑÔ˜QÒÐ¥#Ð&6Ñ"7Ô"7¸1Ò"<Ð"<å•c˜-Ð+;Ò;Ñ<Ô<Ñ=Ô=Ð=Ø�1‚}€}Ý�c‰{Œ{ÐØ�h‘¥­Ð,<Ñ(=Ô(=Ñ!>Ô!>Ñ>€IØ�8‰^�f¥S¨Ñ%7Ô%7Ñ8Ô8Ñ8€FØ�	‰M˜FÑ" y°6Ñ'9Ñ:Ð:r   Ú
predictionÚground_truthc                 óv   — t          t          t          | ¦  «        t          |¦  «        k    ¦  «        ¦  «        S )z&Compute Exact Match for two sentences.)r   r=   r2   )rE   rF   s     r   Ú_compute_exact_match_scorerH   Q   s-   € å•#•o jÑ1Ô1µ_À\Ñ5RÔ5RÒRÑSÔSÑTÔTÐTr   Ú	metric_fnÚground_truthsc                 ó>   ‡ ‡— t          ˆ ˆfd„|D ¦   «         ¦  «        S )zJCalculate maximum score for a predicted answer with all reference answers.c              3   ó0   •K  — | ]} ‰‰|¦  «        V — Œd S r%   r&   )r'   ÚtruthrI   rE   s     €€r   r*   z1_metric_max_over_ground_truths.<locals>.<genexpr>Z   s/   øè è € ÐGÐG°ˆyˆy˜ UÑ+Ô+ÐGÐGÐGÐGÐGÐGr   )Úmax)rI   rE   rJ   s   `` r   Ú_metric_max_over_ground_truthsrO   V   s,   øø€ õ ÐGÐGÐGÐGÐG¸ÐGÑGÔGÑGÔGÐGr   ÚpredsÚtargetsc                 óÈ  ‡	— t          | t          ¦  «        r| g} t          |t          ¦  «        r|g}| D ]-}|                     ¦   «         }d|vsd|vrt          d¦  «        ‚Œ.|D ]X}|                     ¦   «         }d|vsd|vrt          dt          › �¦  «        ‚|d         }d|vrt          dt          › �¦  «        ‚ŒYd„ | D ¦   «         }d	„ Š	d
dˆ	fd„|D ¦   «         igig}||fS )zOCheck for types and convert the input to necessary format to compute the input.Úprediction_textr   z¦Expected keys in a single prediction are 'prediction_text' and 'id'.Please make sure that 'prediction_text' maps to the answer string and 'id' maps to the key string.r   z«Expected keys in a single target are 'answers' and 'id'.Please make sure that 'answers' maps to a `SQuAD` format dictionary and 'id' maps to the key string.
SQuAD Format: r   zzExpected keys in a 'answers' are 'text'.Please make sure that 'answer' maps to a `SQuAD` format dictionary.
SQuAD Format: c                 ó,   — i | ]}|d          |d         “ŒS )r   rS   r&   )r'   rE   s     r   ú
<dictcomp>z&_squad_input_check.<locals>.<dictcomp>‚   s%   € ÐZÐZÐZÀj�*˜TÔ" JÐ/@Ô$AÐZÐZÐZr   c                 óD   — d„ | d         d         D ¦   «         | d         dœS )Nc                 ó   — g | ]}d |i‘ŒS r   r&   )r'   Útxts     r   ú
<listcomp>z8_squad_input_check.<locals>.<lambda>.<locals>.<listcomp>ƒ   s   € Ð)ZÐ)ZÐ)Z¸C¨6°3¨-Ð)ZÐ)ZÐ)Zr   r   r   r   )r   r   r&   )Útgts    r   ú<lambda>z$_squad_input_check.<locals>.<lambda>ƒ   s.   € Ð)ZÐ)ZÀ3ÀyÄ>ÐRXÔCYÐ)ZÑ)ZÔ)ZÐbeÐfjÔbkÐlÐl€ r   Ú
paragraphsÚqasc                 ó&   •— g | ]} ‰|¦  «        ‘ŒS r&   r&   )r'   ÚtargetÚ
_fn_answers     €r   rY   z&_squad_input_check.<locals>.<listcomp>„   s#   ø€ Ð,VÐ,VÐ,VÀF¨Z¨Z¸Ñ-?Ô-?Ð,VÐ,VÐ,Vr   )Ú
isinstanceÚdictÚkeysÚKeyErrorÚSQuAD_FORMAT)
rP   rQ   ÚpredÚ	pred_keysr_   Útarget_keysr   Ú
preds_dictÚtargets_dictr`   s
            @r   Ú_squad_input_checkrk   ]   sv  ø€ õ �%�ÑÔð Ø�ˆå�'�4Ñ Ô ð Ø�)ˆàð ð ˆØ—I’I‘K”Kˆ	Ø IÐ-Ð-°¸YÐ1FÐ1FÝðuñô ð ð 2Gð ð ð ˆØ—k’k‘m”mˆØ˜KÐ'Ð'¨4°{Ð+BÐ+BÝð"õ  ð"ð "ñô ð ð ;AÀÔ:KˆØ˜Ð Ð Ýð"õ  ð"ð "ñô ð ð !ð [ÐZÐTYÐZÑZÔZ€JØlÐl€JØ! UÐ,VÐ,VÐ,VÐ,VÈgÐ,VÑ,VÔ,VÐ$WÐ#XÐYÐZ€LØ�|Ð#Ð#r   r_   c           	      ó’  — t          d¦  «        }t          d¦  «        }t          d¦  «        }|D ]“}|d         D ]ˆ}|d         D ]}}|dz  }|d         | vrt          d|d         › d�¦  «         Œ+d	„ |d
         D ¦   «         }| |d                  }	|t          t          |	|¦  «        z  }|t          t          |	|¦  «        z  }Œ~Œ‰Œ”|||fS )au  Compute F1 Score and Exact Match for a collection of predictions and references.

    Args:
        preds: A dictionary mapping an `id` to the predicted `answer`.
        target:
            A list of dictionary mapping `paragraphs` to list of dictionary mapping `qas` to a list of dictionary
            containing `id` and list of all possible `answers`.

    Return:
        Tuple containing F1 score, Exact match score and total number of examples.

    Example:
        >>> from torchmetrics.functional.text.squad import _squad_update
        >>> preds = [{"prediction_text": "1976", "id": "56e10a3be3433e1400422b22"}]
        >>> target = [{"answers": {"answer_start": [97], "text": ["1976"]}, "id": "56e10a3be3433e1400422b22"}]
        >>> preds_dict = {pred["id"]: pred["prediction_text"] for pred in preds}
        >>> targets_dict = [
        ...     dict(paragraphs=[dict(qas=[dict(answers=[
        ...         {"text": txt} for txt in tgt["answers"]["text"]], id=tgt["id"]) for tgt in target
        ...     ])])
        ... ]
        >>> _squad_update(preds_dict, targets_dict)
        (tensor(1.), tensor(1.), tensor(1))

    r8   r   r\   r]   r
   r   zUnanswered question z will receive score 0.c                 ó   — g | ]
}|d          ‘ŒS r   r&   )r'   Úxs     r   rY   z!_squad_update.<locals>.<listcomp>¯   s   € Ð BÐ BÐ B¨q  6¤Ð BÐ BÐ Br   r   )r   r	   rO   rH   rD   )
rP   r_   Úf1Úexact_matchÚtotalÚarticleÚ	paragraphÚqarJ   rf   s
             r   Ú_squad_updateru   ˆ   s  € õ: 
�‰Œ€BÝ˜‘+”+€KÝ�1‰IŒI€EØð 
]ð 
]ˆØ  Ô.ð 		]ð 		]ˆIØ Ô&ð ]ð ]�Ø˜‘
�Ø�d”8 5Ð(Ð(Ý"Ð#Z¸"¸T¼(Ð#ZÐ#ZÐ#ZÑ[Ô[Ð[ØØ BÐ B°B°y´MÐ BÑ BÔ B�Ø˜R œX”�ØÕ=Õ>XÐZ^Ð`mÑnÔnÑn�ØÕ4Õ5FÈÈmÑ\Ô\Ñ\��ð]ð		]ð ˆ{˜EÐ!Ð!r   ro   rp   rq   c                 ó,   — d|z  |z  }d| z  |z  } || dœS )z•Aggregate the F1 Score and Exact match for the batch.

    Return:
        Dictionary containing the F1 score, Exact match score for the batch.

    g      Y@)rp   ro   r&   )ro   rp   rq   s      r   Ú_squad_computerw   ·   s/   € ð ˜+Ñ%¨Ñ-€KØ	�‰�eÑ	€BØ&¨bÐ1Ð1Ð1r   c                 ór   — t          | |¦  «        \  }}t          ||¦  «        \  }}}t          |||¦  «        S )aã  Calculate `SQuAD Metric`_ .

    Args:
        preds: A Dictionary or List of Dictionary-s that map `id` and `prediction_text` to the respective values.

            Example prediction:

            .. code-block:: python

                {"prediction_text": "TorchMetrics is awesome", "id": "123"}

        target: A Dictionary or List of Dictionary-s that contain the `answers` and `id` in the SQuAD Format.

            Example target:

            .. code-block:: python

                {
                    'answers': [{'answer_start': [1], 'text': ['This is a test answer']}],
                    'id': '1',
                }

            Reference SQuAD Format:

            .. code-block:: python

                {
                    'answers': {'answer_start': [1], 'text': ['This is a test text']},
                    'context': 'This is a test context.',
                    'id': '1',
                    'question': 'Is this a test?',
                    'title': 'train test'
                }


    Return:
        Dictionary containing the F1 score, Exact match score for the batch.

    Example:
        >>> from torchmetrics.functional.text.squad import squad
        >>> preds = [{"prediction_text": "1976", "id": "56e10a3be3433e1400422b22"}]
        >>> target = [{"answers": {"answer_start": [97], "text": ["1976"]},"id": "56e10a3be3433e1400422b22"}]
        >>> squad(preds, target)
        {'exact_match': tensor(100.), 'f1': tensor(100.)}

    Raises:
        KeyError:
            If the required keys are missing in either predictions or targets.

    References:
        [1] SQuAD: 100,000+ Questions for Machine Comprehension of Text by Pranav Rajpurkar, Jian Zhang, Konstantin
        Lopyrev, Percy Liang `SQuAD Metric`_ .

    )rk   ru   rw   )rP   r_   ri   Útarget_dictro   rp   rq   s          r   Úsquadrz   Ã   sA   € õn 1°¸Ñ?Ô?Ñ€J�Ý*¨:°{ÑCÔCÑ€Bˆ�UÝ˜"˜k¨5Ñ1Ô1Ð1r   )!r   r,   Úcollectionsr   Útypingr   r   r   Útorchr   r   Útorchmetrics.utilitiesr	   rb   r1   ÚSINGLE_PRED_TYPEÚlistÚ
PREDS_TYPEr=   ÚSINGLE_TARGET_TYPEÚTARGETS_TYPEÚUPDATE_METHOD_SINGLE_PRED_TYPEre   r2   r4   rD   rH   rO   Útuplerk   ru   rw   rz   r&   r   r   ú<module>r†      sŸ  ðð" 
€	€	€	Ø €€€Ø Ð Ð Ð Ð Ð Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'à  Ð  Ð  Ð  Ð  Ð  Ð  Ð  à 1Ð 1Ð 1Ð 1Ð 1Ð 1à˜˜S˜”>Ð ØÐ# TÐ*:Ô%;Ð;Ô<€
Ø˜#˜u S¨$¨s°E¸$¸s¼)ÀTÈ#ÄYÐ:NÔ4OÐ/OÔ*PÐ%PÔQÐQÔRÐ ØÐ'¨Ð.@Ô)AÐAÔB€Ø!& t¨D°°e¸CÀ¸H´oÐ1EÔ,FÔ'GÈÈdÐSVÐX]Ð^bÐcfÔ^gÐimÐnqÔirÐ^rÔXsÐSsÔNtÐ'tÔ!uÐ ð "# Ð.CÐ-DÐEÐEØ(Ø
Ø!Øðð €ðC�sð C˜sð Cð Cð Cð Cð&7�3ð 7˜4 œ9ð 7ð 7ð 7ð 7ð
;¨ð ;¸Cð ;ÀFð ;ð ;ð ;ð ;ð U¨3ð U¸cð UÀfð Uð Uð Uð Uð
HØ˜˜c˜
 FÐ*Ô+ðHØ9<ðHØMQÐRUÌYðHàðHð Hð Hð Hð($Øð($Ø ,ð($à
ˆ4��S�Œ>˜4  S¨$¨t°C¸¸dÀ3ÈÀ8¼nÔ9MÐ4MÔ/NÔ*OÐ%OÔ PÔQÐQÔRð($ð ($ð ($ð ($ðV,"Ø��S�Œ>ð,"à��c˜4  S¨$¨t°C¸°H¬~Ô*>Ð%>Ô ?Ô@Ð@ÔAÔBð,"ð ˆ6�6˜6Ð!Ô"ð,"ð ,"ð ,"ð ,"ð^	2�vð 	2¨Fð 	2¸6ð 	2ÀdÈ3ÐPVÈ;ÔFWð 	2ð 	2ð 	2ð 	2ð92�ð 92 \ð 92°d¸3À¸;Ô6Gð 92ð 92ð 92ð 92ð 92ð 92r   