§
    'ê[fë2  ã                   óV   — d dl mZ  G d„ d¦  «        Zd„ Zedk    r e¦   «          dS dS )é    )ÚFreqDistc                   ó^   — e Zd ZdZdd„Zd„ Zd„ Zd„ Z	 	 	 	 dd	„Zd
„ Z	d„ Z
d„ Zdd„Zdd„ZdS )ÚConfusionMatrixa¦  
    The confusion matrix between a list of reference values and a
    corresponding list of test values.  Entry *[r,t]* of this
    matrix is a count of the number of times that the reference value
    *r* corresponds to the test value *t*.  E.g.:

        >>> from nltk.metrics import ConfusionMatrix
        >>> ref  = 'DET NN VB DET JJ NN NN IN DET NN'.split()
        >>> test = 'DET VB VB DET NN NN NN IN DET NN'.split()
        >>> cm = ConfusionMatrix(ref, test)
        >>> print(cm['NN', 'NN'])
        3

    Note that the diagonal entries *Ri=Tj* of this matrix
    corresponds to correct values; and the off-diagonal entries
    correspond to incorrect values.
    Fc                 ó   ‡	‡
‡‡— t          |¦  «        t          |¦  «        k    rt          d¦  «        ‚|rFt          |¦  «        Š
t          |¦  «        Šˆ
ˆfd„}t          t	          ||z   ¦  «        |¬¦  «        Šnt          t	          ||z   ¦  «        ¦  «        Šd„ t          ‰¦  «        D ¦   «         }ˆfd„‰D ¦   «         Š	d}t          ||¦  «        D ]O\  }}‰	||                  ||         xx         dz  cc<   t          |‰	||                  ||                  ¦  «        }ŒP‰| _        || _	        ‰	| _
        || _        t          |¦  «        | _        t          ˆ	fd„t          t          ‰¦  «        ¦  «        D ¦   «         ¦  «        | _        d	S )
aÇ  
        Construct a new confusion matrix from a list of reference
        values and a corresponding list of test values.

        :type reference: list
        :param reference: An ordered list of reference values.
        :type test: list
        :param test: A list of values to compare against the
            corresponding reference values.
        :raise ValueError: If ``reference`` and ``length`` do not have
            the same length.
        z Lists must have the same length.c                 ó(   •— ‰|          ‰|          z    S ©N© )ÚvÚ	ref_fdistÚ
test_fdists    €€úP/var/www/piapp/venv/lib/python3.11/site-packages/nltk/metrics/confusionmatrix.pyÚkeyz%ConfusionMatrix.__init__.<locals>.key5   s   ø€ Ø" 1œ¨
°1¬Ñ5Ð6Ð6ó    ©r   c                 ó   — i | ]\  }}||“Œ	S r	   r	   )Ú.0ÚiÚvals      r   ú
<dictcomp>z,ConfusionMatrix.__init__.<locals>.<dictcomp>=   s   € Ð<Ð<Ð<™h˜q #�3˜Ð<Ð<Ð<r   c                 ó(   •— g | ]}d „ ‰D ¦   «         ‘ŒS )c                 ó   — g | ]}d ‘ŒS )r   r	   )r   Ú_s     r   ú
<listcomp>z7ConfusionMatrix.__init__.<locals>.<listcomp>.<listcomp>@   s   € Ð(Ð(Ð(˜A�aÐ(Ð(Ð(r   r	   )r   r   Úvaluess     €r   r   z,ConfusionMatrix.__init__.<locals>.<listcomp>@   s(   ø€ Ð9Ð9Ð9¨QÐ(Ð( Ð(Ñ(Ô(Ð9Ð9Ð9r   r   é   c              3   ó4   •K  — | ]}‰|         |         V — Œd S r   r	   )r   r   Ú	confusions     €r   ú	<genexpr>z+ConfusionMatrix.__init__.<locals>.<genexpr>Q   s+   øè è € ÐHÐH°˜I aœL¨œOÐHÐHÐHÐHÐHÐHr   N)ÚlenÚ
ValueErrorr   ÚsortedÚsetÚ	enumerateÚzipÚmaxÚ_valuesÚ_indicesÚ
_confusionÚ	_max_confÚ_totalÚsumÚrangeÚ_correct)ÚselfÚ	referenceÚtestÚsort_by_countr   ÚindicesÚmax_confÚwÚgr   r   r   r   s            @@@@r   Ú__init__zConfusionMatrix.__init__    s´  øøøø€ õ ˆy‰>Œ>�S ™YœYÒ&Ð&ÝÐ?Ñ@Ô@Ð@ð ð 		3Ý  Ñ+Ô+ˆIÝ! $™œˆJð7ð 7ð 7ð 7ð 7ð 7õ �C 	¨DÑ 0Ñ1Ô1°sÐ;Ñ;Ô;ˆFˆFå�C 	¨DÑ 0Ñ1Ô1Ñ2Ô2ˆFð =Ð<­)°FÑ*;Ô*;Ð<Ñ<Ô<ˆð :Ð9Ð9Ð9°&Ð9Ñ9Ô9ˆ	ØˆÝ˜	 4Ñ(Ô(ð 	Hð 	H‰DˆAˆqØ�g˜a”jÔ! '¨!¤*Ð-Ð-Ô-°Ñ2Ð-Ð-Ñ-Ý˜8 Y¨w°q¬zÔ%:¸7À1¼:Ô%FÑGÔGˆHˆHð ˆŒàˆŒà#ˆŒà!ˆŒå˜)‘n”nˆŒåÐHÐHÐHÐHµU½3¸v¹;¼;Ñ5GÔ5GÐHÑHÔHÑHÔHˆŒˆˆr   c                 óf   — |\  }}| j         |         }| j         |         }| j        |         |         S )z…
        :return: The number of times that value ``li`` was expected and
        value ``lj`` was given.
        :rtype: int
        )r'   r(   )r.   Úli_lj_tupleÚliÚljr   Újs         r   Ú__getitem__zConfusionMatrix.__getitem__S   s7   € ð ‰ˆˆRØŒM˜"ÔˆØŒM˜"ÔˆØŒ˜qÔ! !Ô$Ð$r   c                 ó(   — d| j         › d| j        › d�S )Nz<ConfusionMatrix: ú/z	 correct>)r-   r*   ©r.   s    r   Ú__repr__zConfusionMatrix.__repr__^   s   € ØJ D¤MÐJÐJ°D´KÐJÐJÐJÐJr   c                 ó*   — |                       ¦   «         S r   )Úpretty_formatr?   s    r   Ú__str__zConfusionMatrix.__str__a   s   € Ø×!Ò!Ñ#Ô#Ð#r   TNc           	      ó‚  ‡ — ‰ j         }‰ j        }|rt          |ˆ fd„¬¦  «        }|r
|d|…         }|rd„ |D ¦   «         }n&d„ t          t	          |¦  «        ¦  «        D ¦   «         }t          d„ |D ¦   «         ¦  «        }dt          |¦  «        z   dz   }	|rd	}
d
}d}nAt	          t          ‰ j        ¦  «        ¦  «        }
dt          |
¦  «        z   dz   }d|
dz
  z  dz   }d}t          |¦  «        D ]m}|d|z  dz   z  }|D ]X}||t	          |¦  «        z
  k    r5||||z
  t	          |¦  «        z                                 |
dz   ¦  «        z  }ŒM|d|
dz   z  z  }ŒY|dz  }Œn|d 	                    d|z  d|
dz   t	          |¦  «        z  z  ¦  «        z  }t          ||¦  «        D ]¾\  }}‰ j        |         }||	|z  z  }|D ]œ}‰ j        |         }||         |         dk    r||z  }n6|r ||d||         |         z  ‰ j        z  z  z  }n||||         |         z  z  }||k    r4|                     d¦  «        }|d|…         dz   ||dz   d…         z   dz   }Œ—|dz  }Œ�|dz  }Œ¿|d 	                    d|z  d|
dz   t	          |¦  «        z  z  ¦  «        z  }|dz  }|s'|dz  }t          |¦  «        D ]\  }}|d|dz   |fz  z  }Œ|S )aŽ  
        :return: A multi-line string representation of this confusion matrix.
        :type truncate: int
        :param truncate: If specified, then only show the specified
            number of values.  Any sorting (e.g., sort_by_count)
            will be performed before truncation.
        :param sort_by_count: If true, then sort by the count of each
            label in the reference data.  I.e., labels that occur more
            frequently in the reference label will be towards the left
            edge of the matrix, and labels that occur less frequently
            will be towards the right edge.

        @todo: add marginals?
        c                 óP   •— t          ‰j        ‰j        |                   ¦  «         S r   ©r+   r(   r'   ©r
   r.   s    €r   ú<lambda>z/ConfusionMatrix.pretty_format.<locals>.<lambda>~   s    ø€ ¥s¨4¬?¸4¼=ÈÔ;KÔ+LÑ'MÔ'MÐ&M€ r   r   Nc                 ó   — g | ]}d |z  ‘ŒS )z%sr	   ©r   r   s     r   r   z1ConfusionMatrix.pretty_format.<locals>.<listcomp>…   s   € Ð:Ð:Ð:¨C˜T C™ZÐ:Ð:Ð:r   c                 ó2   — g | ]}t          |d z   ¦  «        ‘ŒS )r   )Ústr)r   Úns     r   r   z1ConfusionMatrix.pretty_format.<locals>.<listcomp>‡   s"   € ÐDÐDÐD¨A�S  Q¡™ZœZÐDÐDÐDr   c              3   ó4   K  — | ]}t          |¦  «        V — Œd S r   ©r   rJ   s     r   r   z0ConfusionMatrix.pretty_format.<locals>.<genexpr>Š   s(   è è € Ð9Ð9 C•s˜3‘x”xÐ9Ð9Ð9Ð9Ð9Ð9r   ú%zs | é   z%5.1f%%z     .ÚdÚ r   ú.Ú z |z |
z	{}-+-{}+
Ú-r   g      Y@Ú<Ú>z|
z(row = reference; col = test)
úValue key:
z%6d: %s
)r(   r&   r!   r,   r   r%   Úreprr)   ÚrjustÚformatr$   r'   r*   Úrfindr#   )r.   Úshow_percentsÚvalues_in_chartÚtruncater1   r   r   Úvalue_stringsÚvaluelenÚvalue_formatÚentrylenÚentry_formatÚzerostrÚsr   r   r9   r:   r;   Ú	prevspaceÚvalues   `                    r   rB   zConfusionMatrix.pretty_formatd   s–  ø€ ð* ”Oˆ	à”ˆØð 	ÝØÐMÐMÐMÐMðñ ô ˆFð ð 	'Ø˜I˜X˜IÔ&ˆFàð 	EØ:Ð:°6Ð:Ñ:Ô:ˆMˆMàDÐDµµs¸6±{´{Ñ1CÔ1CÐDÑDÔDˆMõ Ð9Ð9¨=Ð9Ñ9Ô9Ñ9Ô9ˆØ�T (™^œ^Ñ+¨fÑ4ˆàð 	1ØˆHØ$ˆLØˆGˆGå�4 ¤Ñ/Ô/Ñ0Ô0ˆHØ¥ h¡¤Ñ/°#Ñ5ˆLØ˜X¨™\Ñ*¨SÑ0ˆGð ˆÝ�x‘”ð 	ð 	ˆAØ�#˜‘. DÑ(Ñ(ˆAØ$ð .ð .�Ø˜¥3 s¡8¤8Ñ+Ò+Ð+Ø˜˜Q ™\­C°©H¬HÑ4Ô5×;Ò;¸HÀq¹LÑIÔIÑI�A�Aà˜ ¨1¡Ñ-Ñ-�A�AØ�‰KˆAˆAð 	
ˆ\× Ò   x¡°¸ÀA¹ÍÈVÉÌÑ8TÑ1UÑVÔVÑVˆõ ˜=¨&Ñ1Ô1ð 	ð 	‰GˆC�Ø”˜bÔ!ˆAØ� Ñ#Ñ#ˆAØð ð �Ø”M "Ô%�Ø˜Q”< ”? aÒ'Ð'Ø˜‘L�A�AØ"ð 8Ø˜¨°¸1´¸a´Ñ)@À4Ä;Ñ)NÑOÑO�A�Aà˜¨	°!¬°Q¬Ñ7Ñ7�AØ˜’6�6Ø !§¢¨¡¤�IØ˜*˜9˜*œ¨Ñ+¨a°	¸A±°°Ô.@Ñ@À3ÑF�A�Aà˜‘H�A�AØ�‰JˆAˆAð 	
ˆ\× Ò   x¡°¸ÀA¹ÍÈVÉÌÑ8TÑ1UÑVÔVÑVˆð 	
Ð.Ñ.ˆØð 	2Ø�ÑˆAÝ% fÑ-Ô-ð 2ð 2‘��5Ø�[ A¨¡E¨5 >Ñ1Ñ1��àˆr   c                 óø   — | j         }d}t          t          t          |¦  «        dz
  ¦  «        ¦  «        }dt          |¦  «        z   dz   }t          t          |¦  «        ¦  «        D ]}|||||         fz  z  }Œ|S )NrY   r   z  %zd: %s
)r&   r   rZ   r,   )r.   r   rL   ÚindexlenÚ
key_formatr   s         r   r   zConfusionMatrix.keyÃ   s}   € Ø”ˆØˆÝ•t�C ™KœK¨!™OÑ,Ô,Ñ-Ô-ˆØ�T (™^œ^Ñ+¨iÑ7ˆ
Ý•s˜6‘{”{Ñ#Ô#ð 	/ð 	/ˆAØ�:  F¨1¤I Ñ.Ñ.ˆCˆCàˆ
r   c                 óv   ‡ ‡— ‰ ‰‰f         }t          ˆ ˆfd„‰ j        D ¦   «         ¦  «        }|dk    rdS ||z  S )añ  Given a value in the confusion matrix, return the recall
        that corresponds to this value. The recall is defined as:

        - *r* = true positive / (true positive + false positive)

        and can loosely be considered the ratio of how often ``value``
        was predicted correctly relative to how often ``value`` was
        the true result.

        :param value: value used in the ConfusionMatrix
        :return: the recall corresponding to ``value``.
        :rtype: float
        c              3   ó,   •K  — | ]}‰‰|f         V — Œd S r   r	   )r   Ú
pred_valuer.   ri   s     €€r   r   z)ConfusionMatrix.recall.<locals>.<genexpr>Þ   s-   øè è € ÐKÐK°
�D˜ 
Ð*Ô+ÐKÐKÐKÐKÐKÐKr   r   ç        ©r+   r&   )r.   ri   ÚTPÚTP_FNs   ``  r   ÚrecallzConfusionMatrix.recallÍ   óS   øø€ ð �%˜�,ÔˆåÐKÐKÐKÐKÐK¸d¼lÐKÑKÔKÑKÔKˆØ�AŠ:ˆ:Ø�3Ø�E‰zÐr   c                 óv   ‡ ‡— ‰ ‰‰f         }t          ˆ ˆfd„‰ j        D ¦   «         ¦  «        }|dk    rdS ||z  S )aú  Given a value in the confusion matrix, return the precision
        that corresponds to this value. The precision is defined as:

        - *p* = true positive / (true positive + false negative)

        and can loosely be considered the ratio of how often ``value``
        was predicted correctly relative to the number of predictions
        for ``value``.

        :param value: value used in the ConfusionMatrix
        :return: the precision corresponding to ``value``.
        :rtype: float
        c              3   ó,   •K  — | ]}‰|‰f         V — Œd S r   r	   )r   Ú
real_valuer.   ri   s     €€r   r   z,ConfusionMatrix.precision.<locals>.<genexpr>ô   s-   øè è € ÐKÐK°
�D˜ UÐ*Ô+ÐKÐKÐKÐKÐKÐKr   r   rp   rq   )r.   ri   rr   ÚTP_FPs   ``  r   Ú	precisionzConfusionMatrix.precisionã   ru   r   ç      à?c                 ó”   — |                       |¦  «        }|                      |¦  «        }|dk    s|dk    rdS d||z  d|z
  |z  z   z  S )ao  
        Given a value used in the confusion matrix, return the f-measure
        that corresponds to this value. The f-measure is the harmonic mean
        of the ``precision`` and ``recall``, weighted by ``alpha``.
        In particular, given the precision *p* and recall *r* defined by:

        - *p* = true positive / (true positive + false negative)
        - *r* = true positive / (true positive + false positive)

        The f-measure is:

        - *1/(alpha/p + (1-alpha)/r)*

        With ``alpha = 0.5``, this reduces to:

        - *2pr / (p + r)*

        :param value: value used in the ConfusionMatrix
        :param alpha: Ratio of the cost of false negative compared to false
            positives. Defaults to 0.5, where the costs are equal.
        :type alpha: float
        :return: the F-measure corresponding to ``value``.
        :rtype: float
        rp   g      ð?r   )rz   rt   )r.   ri   ÚalphaÚpÚrs        r   Ú	f_measurezConfusionMatrix.f_measureù   sX   € ð2 �NŠN˜5Ñ!Ô!ˆØ�KŠK˜ÑÔˆØ�Š8ˆ8�q˜C’x�xØ�3Ø�e˜a‘i 1 u¡9°¡/Ñ1Ñ2Ð2r   c                 óz  ‡ — ‰ j         }|rt          |ˆ fd„¬¦  «        }|r
|d|…         }t          t          d„ |D ¦   «         ¦  «        d¦  «        }d|dz
  z  › dd|z  › d	�}|D ]U}||d
|› �›d‰                      |¦  «        d›d‰                      |¦  «        d›d‰                      ||¬¦  «        d›d�z  }ŒV|S )aG  
        Tabulate the **recall**, **precision** and **f-measure**
        for each value in this confusion matrix.

        >>> reference = "DET NN VB DET JJ NN NN IN DET NN".split()
        >>> test = "DET VB VB DET NN NN NN IN DET NN".split()
        >>> cm = ConfusionMatrix(reference, test)
        >>> print(cm.evaluate())
        Tag | Prec.  | Recall | F-measure
        ----+--------+--------+-----------
        DET | 1.0000 | 1.0000 | 1.0000
         IN | 1.0000 | 1.0000 | 1.0000
         JJ | 0.0000 | 0.0000 | 0.0000
         NN | 0.7500 | 0.7500 | 0.7500
         VB | 0.5000 | 1.0000 | 0.6667
        <BLANKLINE>

        :param alpha: Ratio of the cost of false negative compared to false
            positives, as used in the f-measure computation. Defaults to 0.5,
            where the costs are equal.
        :type alpha: float
        :param truncate: If specified, then only show the specified
            number of values. Any sorting (e.g., sort_by_count)
            will be performed before truncation. Defaults to None
        :type truncate: int, optional
        :param sort_by_count: Whether to sort the outputs on frequency
            in the reference label. Defaults to False.
        :type sort_by_count: bool, optional
        :return: A tabulated recall, precision and f-measure string
        :rtype: str
        c                 óP   •— t          ‰j        ‰j        |                   ¦  «         S r   rF   rG   s    €r   rH   z*ConfusionMatrix.evaluate.<locals>.<lambda><  s!   ø€ ­s°4´?À4Ä=ÐQRÔCSÔ3TÑ/UÔ/UÐ.U€ r   r   Nc              3   ó4   K  — | ]}t          |¦  «        V — Œd S r   rO   )r   Útags     r   r   z+ConfusionMatrix.evaluate.<locals>.<genexpr>@  s(   è è € Ð :Ð :¨c¥ S¡¤Ð :Ð :Ð :Ð :Ð :Ð :r   é   rS   z"Tag | Prec.  | Recall | F-measure
rV   z -+--------+--------+-----------
rX   z | z<6.4f)r}   z.4fÚ
)r&   r!   r%   rz   rt   r€   )r.   r}   r`   r1   ÚtagsÚtag_column_lenrg   r„   s   `       r   ÚevaluatezConfusionMatrix.evaluate  sH  ø€ ð@ Œ|ˆð ð 	WÝ˜$Ð$UÐ$UÐ$UÐ$UÐVÑVÔVˆDØð 	#Ø˜	˜˜	”?ˆDå�SÐ :Ð :°TÐ :Ñ :Ô :Ñ:Ô:¸AÑ>Ô>ˆð �n qÑ(Ñ)ð Gð GØ�^Ñ#ðGð Gð Gð 	
ð ð 	ð 	ˆCØØð <˜ð <Ð-ð <ð <Ø—>’> #Ñ&Ô&Ð1ð<ð <à—;’;˜sÑ#Ô#Ð.ð<ð <ð —>’> #¨U�>Ñ3Ô3Ð;ð<ð <ð <ñˆAˆAð ˆr   )F)FTNF)r{   )r{   NF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r6   r<   r@   rC   rB   r   rt   rz   r€   r‰   r	   r   r   r   r      sã   € € € € € ðð ð$1Ið 1Ið 1Ið 1Iðf	%ð 	%ð 	%ðKð Kð Kð$ð $ð $ð
 ØØØð]ð ]ð ]ð ]ð~ð ð ðð ð ð,ð ð ð,3ð 3ð 3ð 3ð>9ð 9ð 9ð 9ð 9ð 9r   r   c                  ó°  — d                      ¦   «         } d                      ¦   «         }t          d| ¦  «         t          d|¦  «         t          d¦  «         t          t          | |¦  «        ¦  «         t          t          | |¦  «                             d¬¦  «        ¦  «         t          t          | |¦  «                             d¦  «        ¦  «         d S )	Nz DET NN VB DET JJ NN NN IN DET NNz DET VB VB DET NN NN NN IN DET NNzReference =z	Test    =zConfusion matrix:T)r1   ÚVB)ÚsplitÚprintr   rB   rt   )r/   r0   s     r   Údemor’   T  sÀ   € Ø2×8Ò8Ñ:Ô:€IØ-×3Ò3Ñ5Ô5€DÝ	ˆ-˜Ñ#Ô#Ð#Ý	ˆ+�tÑÔÐÝ	Ð
ÑÔÐÝ	�/˜) TÑ
*Ô
*Ñ+Ô+Ð+Ý	�/˜) TÑ
*Ô
*×
8Ò
8ÀtÐ
8Ñ
LÔ
LÑMÔMÐMå	�/˜) TÑ
*Ô
*×
1Ò
1°$Ñ
7Ô
7Ñ8Ô8Ð8Ð8Ð8r   Ú__main__N)Únltk.probabilityr   r   r’   rŠ   r	   r   r   ú<module>r•      s}   ðð &Ð %Ð %Ð %Ð %Ð %ðDð Dð Dð Dð Dñ Dô Dð DðN
	9ð 	9ð 	9ð ˆzÒÐØ€D�F„F€F€F€Fð Ðr   