
    /h1                     F    S SK Jr   " S S5      rS r\S:X  a  \" 5         gg)    )FreqDistc                   l    \ rS rSrSrSS jrS rS rS r    SS jr	S	 r
S
 rS rSS jrSS jrSrg)ConfusionMatrix   an  
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.
c                   ^^^ [        U5      [        U5      :w  a  [        S5      eU(       a3  [        U5      m[        U5      mUU4S jn[        [	        X-   5      US9nO[        [	        X-   5      5      n[        U5       VVs0 s H  u  pgXv_M	     nnnU V	s/ s H  o V	s/ s H  n	SPM     sn	PM     sn	mSn
[        X5       H/  u  pTX      X   ==   S-  ss'   [        U
TX      X      5      n
M1     XPl        Xl	        TU l
        Xl        [        U5      U l        [        U4S j[        [        U5      5       5       5      U l        gs  snnf s  sn	f s  sn	f )aw  
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                    > TU    TU    -   * $ N )v	ref_fdist
test_fdists    T/var/www/auris/envauris/lib/python3.13/site-packages/nltk/metrics/confusionmatrix.pykey%ConfusionMatrix.__init__.<locals>.key5   s    "1
1566    r   r      c              3   4   >#    U  H  nTU   U   v   M     g 7fr	   r
   ).0i	confusions     r   	<genexpr>+ConfusionMatrix.__init__.<locals>.<genexpr>Q   s     H5GIaLO5Gs   N)len
ValueErrorr   sortedset	enumeratezipmax_values_indices
_confusion	_max_conf_totalsumrange_correct)self	referencetestsort_by_countr   valuesr   valindices_max_confwgr   r   r   s                @@@r   __init__ConfusionMatrix.__init__    s@    y>SY&?@@  +I!$J7 C	 01s;FC	 012F +4F*;<*;hq36*;< 399&Q(Aa(&9		(DAgj!'*-2-8Ywz%:7:%FGH )
 #!)nHU3v;5GHH) = )9s   E	E#E
/EEc                 j    Uu  p#U R                   U   nU R                   U   nU R                  U   U   $ )ze
: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__ConfusionMatrix.__getitem__S   s;     MM"MM"q!!$$r   c                 <    SU R                    SU R                   S3$ )Nz<ConfusionMatrix: /z	 correct>)r(   r%   r)   s    r   __repr__ConfusionMatrix.__repr__^   s    #DMM?!DKK=	JJr   c                 "    U R                  5       $ r	   )pretty_formatr?   s    r   __str__ConfusionMatrix.__str__a   s    !!##r   Nc           	      n  ^  T R                   nT R                  nU(       a  [        UU 4S jS9nU(       a  USU nU(       a  U Vs/ s H  nSU-  PM
     nnO0[        [	        U5      5       V	s/ s H  n	[        U	S-   5      PM     nn	[        S U 5       5      n
S[        U
5      -   S-   nU(       a  S	nS
nSnO:[	        [        T R                  5      5      nS[        U5      -   S-   nSUS-
  -  S-   nSn[        U
5       Hc  nUSU
-  S-   -  nU HJ  nUU
[	        U5      -
  :  a*  XUU
-
  [	        U5      -      R                  US-   5      -  nM?  USUS-   -  -  nML     US-  nMe     USR                  SU
-  SUS-   [	        U5      -  -  5      -  n[        X5       H  u  nnT R                  U   nXU-  -  nU H  nT R                  U   nUU   U   S:X  a  X-  nO2U(       a  XSUU   U   -  T R                  -  -  -  nOXUU   U   -  -  nUU:X  a'  UR                  S5      nUSU S-   UUS-   S -   S-   nM  US-  nM     US-  nM     USR                  SU
-  SUS-   [	        U5      -  -  5      -  nUS-  nU(       d'  US-  n[        U5       H  u  nnUSUS-   U4-  -  nM     U$ s  snf s  sn	f )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R                  TR                  U       5      * $ r	   r&   r#   r"   r   r)   s    r   <lambda>/ConfusionMatrix.pretty_format.<locals>.<lambda>~   s    s4??4==;K+L'M&Mr   r   Nz%sr   c              3   8   #    U  H  n[        U5      v   M     g 7fr	   r   )r   r.   s     r   r   0ConfusionMatrix.pretty_format.<locals>.<genexpr>   s     9=Cs3xx=   %zs |    z%5.1f%%z     .d . z |z |
z	{}-+-{}+
-r   g      Y@<>z|
z(row = reference; col = test)
Value key:
z%6d: %s
)r#   r!   r   r'   r   strr    reprr$   rjustformatr   r"   r%   rfindr   )r)   show_percentsvalues_in_charttruncater,   r   r-   r.   value_stringsnvaluelenvalue_formatentrylenentry_formatzerostrsr   r8   r9   r:   	prevspacevalues   `                     r   rC   ConfusionMatrix.pretty_formatd   s   * OO	MF IX&F39:6CTCZ6M:M16s6{1CD1CASQZ1CMD 9=99T(^+f4H$LG4/0Hh/#5LX\*S0G xA#.D((A$3s8++Q\CH45;;HqLIIA1--A	 %
 KA ! 	
\  xAV8T1UVV =1GCb!A##AMM"%Q<?a'LA"1a)@4;;)NOOA	!Q77A6 !I*9+a	A.@@3FAHA  JA! 2& 	
\  xAV8T1UVV 	
..A%f-5[AE5>11 . y ;Ds   J-.J2c           
         U R                   nSn[        [        [        U5      S-
  5      5      nS[        U5      -   S-   nUSR                  [	        [        U5      5       Vs/ s H  oTXQU   4-  PM     sn5      -  nU$ s  snf )NrY   r   z  %zd: %s
rU   )r!   r   r[   joinr'   )r)   r-   rZ   indexlen
key_formatr   s         r   r   ConfusionMatrix.key   s{    tCK!O,-T(^+i7
rwwU3v;=OP=Oa^3=OPQQ
 Qs   "A?
c                 j   ^ ^ T TT4   n[        U U4S jT R                   5       5      nUS:X  a  gX#-  $ )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   2   >#    U  H  nTTU4   v   M     g 7fr	   r
   )r   
pred_valuer)   rk   s     r   r   )ConfusionMatrix.recall.<locals>.<genexpr>   s     Kl
D
*+l   r           r&   r!   )r)   rk   TPTP_FNs   ``  r   recallConfusionMatrix.recall   9     %,KdllKKA:zr   c                 j   ^ ^ T TT4   n[        U U4S jT R                   5       5      nUS:X  a  gX#-  $ )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   2   >#    U  H  nTUT4   v   M     g 7fr	   r
   )r   
real_valuer)   rk   s     r   r   ,ConfusionMatrix.precision.<locals>.<genexpr>   s     Kl
DU*+lrv   r   rw   rx   )r)   rk   ry   TP_FPs   ``  r   	precisionConfusionMatrix.precision   r}   r   c                     U R                  U5      nU R                  U5      nUS:X  d  US:X  a  gSX#-  SU-
  U-  -   -  $ )a  
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
rw   g      ?r   )r   r{   )r)   rk   alphaprs        r   	f_measureConfusionMatrix.f_measure   sJ    2 NN5!KK8qCxei1u9/122r   c                 T  ^  T R                   nU(       a  [        UU 4S jS9nU(       a  USU n[        [        S U 5       5      S5      nSUS-
  -   SSU-   S	3nU HF  nUUS
U 3 ST R                  U5      S ST R	                  U5      S ST R                  XqS9S S3-  nMH     U$ )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R                  TR                  U       5      * $ r	   rH   rI   s    r   rJ   *ConfusionMatrix.evaluate.<locals>.<lambda>:  s     s4??4==QRCS3T/U.Ur   r   Nc              3   8   #    U  H  n[        U5      v   M     g 7fr	   rM   )r   tags     r   r   +ConfusionMatrix.evaluate.<locals>.<genexpr>>  s      :TcSTrO      rS   z"Tag | Prec.  | Recall | F-measure
rV   z -+--------+--------+-----------
rX   z | z<6.4f)r   z.4f
)r!   r   r    r   r{   r   )r)   r   ra   r,   tagstag_column_lenri   r   s   `       r   evaluateConfusionMatrix.evaluate  s    @ || $$UVD	?DS :T ::A> nq()**M^#$$EG 	
 Cq(()>>#&u-S;;s#E*#>>#>3C8<A  r   )r#   r(   r"   r$   r%   r!   )F)FTNF)      ?)r   NF)__name__
__module____qualname____firstlineno____doc__r4   r;   r@   rD   rC   r   r{   r   r   r   __static_attributes__r
   r   r   r   r      sL    $1If	%K$
 ]~,,3>9r   r   c                  :   SR                  5       n SR                  5       n[        SU 5        [        SU5        [        S5        [        [        X5      5        [        [        X5      R                  SS95        [        [        X5      R	                  S5      5        g )	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)r,   VB)splitprintr   rC   r{   )r*   r+   s     r   demor   R  s{    288:I-335D	-#	+t	
	/)
*+	/)
*
8
8t
8
LM	/)
*
1
1$
78r   __main__N)nltk.probabilityr   r   r   r   r
   r   r   <module>r      s0    &B BJ
	9 zF r   