ó <¿CVc@suddlmZmZddlmZddlmZedefd„ƒYƒZd„Z e dkrqe ƒndS( iÿÿÿÿ(tprint_functiontunicode_literals(tFreqDist(tpython_2_unicode_compatibletConfusionMatrixcBsSeZdZed„Zd„Zd„Zd„Zeeded„Z d„Z RS(u¦ 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 s—t|ƒt|ƒkr'tdƒ‚n|rvt|ƒ‰t|ƒ‰‡‡fd†}tt||ƒd|ƒ}ntt||ƒƒ}td„t|ƒDƒƒ}g|D]}g|D] }d^q¼^q¯‰d}xXt||ƒD]G\} } ˆ|| || cd7:siic3s|]}ˆ||VqdS(N((R R (t confusion(sn/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/metrics/confusionmatrix.pys NsN(tlent ValueErrorRtsortedtsettdictt enumeratetziptmaxt_valuest_indicest _confusiont _max_conft_totaltsumtranget_correct( tselft referencettestt sort_by_countRtvaluestindicesR tmax_conftwtg((R RRsn/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/metrics/confusionmatrix.pyt__init__ s(   ,#    cCs5|\}}|j|}|j|}|j||S(u… :return: The number of times that value ``li`` was expected and value ``lj`` was given. :rtype: int (RR(Rt li_lj_tupletlitljR tj((sn/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/metrics/confusionmatrix.pyt __getitem__Ps   cCsd|j|jfS(Nu (RR(R((sn/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/metrics/confusionmatrix.pyt__repr__[s cCs |jƒS(N(t pretty_format(R((sn/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/metrics/confusionmatrix.pyt__str___scspˆj}ˆj}|r6t|d‡fd†ƒ}n|rI|| }n|rog|D]}d|^qV}n/gtt|ƒƒD]} t| dƒ^q‚}td„|Dƒƒ} dt| ƒd} |rãd} d } d }n;ttˆjƒƒ} dt| ƒd } d | dd }d}x•t| ƒD]‡}|d | d7}xb|D]Z}|| t|ƒkr˜|||| t|ƒj | dƒ7}qP|d | d7}qPW|d7}q1W|dd| d| dt|ƒf7}xt ||ƒD]ö\}}ˆj |}|| |7}xÂ|D]º}ˆj |}|||dkrX||7}n@|r‚|| d|||ˆj 7}n|| |||7}||krÔ|j d ƒ}|| d||dd}q$|d 7}q$W|d7}qöW|dd| d| dt|ƒf7}|d7}|sl|d7}x5t|ƒD]$\}}|d|d|f7}qAWn|S(uŽ :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? Rcstˆjˆj|ƒ S(N(RRR(R(R(sn/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/metrics/confusionmatrix.pytvsu%sicss|]}t|ƒVqdS(N(R (R R ((sn/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/metrics/confusionmatrix.pys ‚su%us | iu%5.1f%%u .udu u.uu |u | u %s-+-%s+ u-igY@uu| u(row = reference; col = test) u Value key: u%6d: %s (RRRRR tstrRtreprRtrjustRRRtrfindR(Rt show_percentstvalues_in_chartttruncateR R R!R t value_stringstntvaluelent value_formattentrylent entry_formattzerostrtsR R(R)R*t prevspacetvalue((Rsn/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/metrics/confusionmatrix.pyR-bsb    /  ,*    $ !*  cCsx|j}d}ttt|ƒdƒƒ}dt|ƒd}x2tt|ƒƒD]}|||||f7}qRW|S(Nu Value key: iu %ud: %s (RR R1R(RR!R0tindexlent key_formatR ((sn/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/metrics/confusionmatrix.pyRºs N( t__name__t __module__t__doc__tFalseR&R+R,R.tTruetNoneR-R(((sn/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/metrics/confusionmatrix.pyR s 0  WcCsrdjƒ}djƒ}td|ƒtd|ƒtdƒtt||ƒƒtt||ƒjdtƒƒdS(Nu DET NN VB DET JJ NN NN IN DET NNu DET VB VB DET NN NN NN IN DET NNu Reference =u Test =uConfusion matrix:R (tsplittprintRR-RG(RR((sn/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/metrics/confusionmatrix.pytdemoÄs     u__main__N( t __future__RRtnltk.probabilityRt nltk.compatRtobjectRRKRC(((sn/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/metrics/confusionmatrix.pyts·