³ò
4ÒÇIc           @   s;   d  e  f d „  ƒ  YZ d „  Z e d j o e ƒ  n d S(   t   ConfusionMatrixc           B   sM   e  Z d  Z e d „ Z d „  Z d „  Z d „  Z e e d „ Z	 d „  Z
 RS(   s|  
    The confusion matrix between a list of reference values and a
    corresponding list of test values.  Entry [M{r},M{t}] of this
    matrix is a count of the number of times that the reference value
    M{r} corresponds to the test value M{t}.  E.g.:

        >>> 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 (M{Ri}=M{Tj}) of this matrix
    corresponds to correct values; and the off-diagonal entries
    correspond to incorrect values.
    c            s«  t  | ƒ t  | ƒ j o t d ƒ ‚ n | oJ t | ƒ ‰  t | ƒ ‰ ‡ ‡  f d †  } t t | | ƒ d | ƒ} n t t | | ƒ ƒ } t d „  t | ƒ Dƒ ƒ } g  } | D]( } | g  }	 | D] } |	 d qË ~	 q· ~ ‰ d }
 xX t | | ƒ D]G \ } } ˆ | | | | c d 7<t |
 ˆ | | | | ƒ }
 qþ W| |  _	 | |  _
 ˆ |  _ |
 |  _ t  | ƒ |  _ t ‡ f d †  t t  | ƒ ƒ Dƒ ƒ |  _ d S(	   sÓ  
        Construct a new confusion matrix from a list of reference
        values and a corresponding list of test values.
        
        @type reference: C{list}
        @param reference: An ordered list of reference values.
        @type test: C{list}
        @param test: A list of values to compare against the
            corresponding reference values.
        @raise ValueError: If C{reference} and C{length} do not have
            the same length.
        s    Lists must have the same length.c            s   ˆ |  ˆ  |  S(   N(    (   t   v(   t
   test_fdistt	   ref_fdist(    s2   /p/zhu/06/nlp/nltk/nltk/metrics/confusionmatrix.pyt   key/   s    R   c         s   s%   x |  ] \ } } | | f Vq Wd  S(   N(    (   t   .0t   it   val(    (    s2   /p/zhu/06/nlp/nltk/nltk/metrics/confusionmatrix.pys	   <genexpr>5   s    i    i   c         3   s!   x |  ] } ˆ  | | Vq Wd  S(   N(    (   R   R   (   t	   confusion(    s2   /p/zhu/06/nlp/nltk/nltk/metrics/confusionmatrix.pys	   <genexpr>I   s    N(   t   lent
   ValueErrort   FreqDistt   sortedt   sett   dictt	   enumeratet   zipt   maxt   _valuest   _indicest
   _confusiont	   _max_conft   _totalt   sumt   ranget   _correct(   t   selft	   referencet   testt   sort_by_countR   t   valuest   indicest   _[1]R   t   _[2]t   max_conft   wt   g(    (   R   R   R   s2   /p/zhu/06/nlp/nltk/nltk/metrics/confusionmatrix.pyt   __init__   s*     < #				c         C   s5   | \ } } |  i  | } |  i  | } |  i | | S(   s†   
        @return: The number of times that value C{li} was expected and
        value C{lj} was given.
        @rtype: C{int}
        (   R   R   (   R   t   .1t   lit   ljR   t   j(    (    s2   /p/zhu/06/nlp/nltk/nltk/metrics/confusionmatrix.pyt   __getitem__K   s   c         C   s   d |  i  |  i f S(   Ns    <ConfusionMatrix: %s/%s correct>(   R   R   (   R   (    (    s2   /p/zhu/06/nlp/nltk/nltk/metrics/confusionmatrix.pyt   __repr__U   s    	c         C   s
   |  i  ƒ  S(   N(   t   pp(   R   (    (    s2   /p/zhu/06/nlp/nltk/nltk/metrics/confusionmatrix.pyt   __str__Y   s    c         C   s  |  i  } | o |  i } n t t |  i ƒ ƒ } t d „  | Dƒ ƒ } d | d } | o d } d } d }	 n2 t |  i ƒ } d | d } d | d	 d
 }	 g  }
 | D] } |
 t | ƒ q° ~
 } d } x— t | ƒ D]‰ } | d | d 7} xd | D]\ } | | t | ƒ j o- | | | | t | ƒ i | d	 ƒ 7} qþ | d | d	 7} qþ W| d 7} qß W| d d | d | d	 t | ƒ f 7} x t t | ƒ ƒ D]ì } | | | | 7} xÇ t t | ƒ ƒ D]³ } | | | d j o | |	 7} nC | o% | | d | | | |  i 7} n | | | | | 7} | | j o1 | i	 d ƒ } | |  d | | d	 d } qÔ| d 7} qÔW| d 7} q©W| d d | d | d	 t | ƒ f 7} | d 7} | pB | d 7} x5 t
 |  i ƒ D]  \ } } | d | | f 7} qîWn | S(   s}   
        @return: A multi-line string representation of this confusion
        matrix.
        @todo: add marginals?
        c         s   s%   x |  ] } t  t | ƒ ƒ Vq Wd  S(   N(   R	   t   str(   R   R   (    (    s2   /p/zhu/06/nlp/nltk/nltk/metrics/confusionmatrix.pys	   <genexpr>j   s    t   %s   s | i   s   %5.1f%%s        .t   dt    i   t   .t    s    |s    |
s	   %s-+-%s+
t   -i    g      Y@t   <t   >s   |
s   (row = reference; col = test)
s   Value key:
s   %6d: %s
(   R   R   R   R	   R   R   R.   t   rjustR   t   rfindR   (   R   t   show_percentst   values_in_chartR   R   t   valuelent   value_formatt   entrylent   entry_formatt   zerostrR    R   t   value_stringst   sR   R)   t	   prevspacet   value(    (    s2   /p/zhu/06/nlp/nltk/nltk/metrics/confusionmatrix.pyR,   \   s`    	
'  -*  %"*

 c         C   sn   |  i  } d } t t | ƒ d ƒ } d | d } x2 t t | ƒ ƒ D] } | | | | | f 7} qH W| S(   Ns   Value key:
i   s     %s   d: %s
(   R   R	   R   (   R   R   R.   t   indexlent
   key_formatR   (    (    s2   /p/zhu/06/nlp/nltk/nltk/metrics/confusionmatrix.pyR   ¡   s    	 (   t   __name__t
   __module__t   __doc__t   FalseR%   R*   R+   R-   t   TrueR,   R   (    (    (    s2   /p/zhu/06/nlp/nltk/nltk/metrics/confusionmatrix.pyR    	   s   0	
		Ec          C   sA   d i  ƒ  }  d i  ƒ  } d G|  GHd G| GHd GHt |  | ƒ GHd  S(   Ns    DET NN VB DET JJ NN NN IN DET NNs    DET VB VB DET NN NN NN IN DET NNs   Reference =s	   Test    =s   Confusion matrix:(   t   splitR    (   R   R   (    (    s2   /p/zhu/06/nlp/nltk/nltk/metrics/confusionmatrix.pyt   demo«   s    		t   __main__N(   t   objectR    RL   RF   (    (    (    s2   /p/zhu/06/nlp/nltk/nltk/metrics/confusionmatrix.pys   <module>	   s   ¢	