³ò
4ÒÇIc           @   s‹  d  Z  d d k Z d d k Td d? d „  ƒ  YZ e d j oKd d k Z d d k Z d d k Z e i ƒ  Z e i	 d d d	 d
 d d d d ƒe i	 d d d	 d d d d d ƒe i	 d d d	 d d d d g  d d ƒe i	 d d d	 d d d d g  d d ƒe i	 d d d	 d  d d! ƒe i	 d" d# d	 d$ d d% d d& ƒe i	 d' d( d	 d) d d* d d+ ƒe i	 d, d- d	 d. d d/ d d0 ƒe i	 d1 d2 d	 d3 d d d d4 ƒe i	 d5 d6 d	 d7 d e d d8 d d9 ƒe i ƒ  \ Z Z e i p e i ƒ  e ƒ  n e i d: d; d< e e i ƒ ƒ g  Z xã e e i ƒ D]Ò Z e i e i ƒ Z e d= e e d> d !ƒ e e d i ƒ  i e i ƒ ƒ Z Z  Z! e i" e i# j pL e$ e i" ƒ d= j o e e i" j p& e$ e i# ƒ d= j o* e e i# j o e i% e e  e! f ƒ q4q4We i& o( e e e' e e i ƒ e i& ƒ ƒ Z( n e e e' e e i ƒ ƒ Z( e i) o n e' e( e i* ƒ ƒ  GHe i+ ƒ  n d S(@   sp  
Implementations of inter-annotator agreement coefficients surveyed by Artstein
and Poesio (2007), Inter-Coder Agreement for Computational Linguistics.

An agreement coefficient calculates the amount that annotators agreed on label 
assignments beyond what is expected by chance.

In defining the AnnotationTask class, we use naming conventions similar to the 
paper's terminology.  There are three types of objects in an annotation task: 

    the coders (variables "c" and "C")
    the items to be annotated (variables "i" and "I")
    the potential categories to be assigned (variables "k" and "K")

Additionally, it is often the case that we don't want to treat two different 
labels as complete disagreement, and so the AnnotationTask constructor can also
take a distance metric as a final argument.  Distance metrics are simply 
functions that take two arguments, and return a value between 0.0 and 1.0 
indicating the distance between them.  If not supplied, the default is binary 
comparison between the arguments.

The simplest way to initialize an AnnotationTask is with a list of equal-length 
lists, each containing a coder's assignments for all objects in the task:

    task = AnnotationTask([],[],[])

Alpha (Krippendorff 1980)
Kappa (Cohen 1960)
S (Bennet, Albert and Goldstein 1954)
Pi (Scott 1955)


TODO: Describe handling of multiple coders and missing data

Expected results from the Artstein and Poesio survey paper:

>>> t = AnnotationTask(data=[x.split() for x in open("%sartstein_poesio_example.txt" % (__file__.replace("__init__.py", "")))])
>>> t.avg_Ao()
0.88
>>> t.pi()
0.7995322418977614
>>> t.S()
0.81999999999999984
iÿÿÿÿN(   t   *t   AnnotationTaskc           B   sÈ   e  Z d  Z e e d „ Z d „  Z d „  Z d „  Z e e e d „ Z	 d „  Z
 d „  Z d „  Z d	 d
 „ Z d	 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 d „ Z d „  Z RS(   s3  Represents an annotation task, i.e. people assign labels to items.
    
    Notation tries to match notation in Artstein and Poesio (2007).

    In general, coders and items can be represented as any hashable object.
    Integers, for example, are fine, though strings are more readable.
    Labels must support the distance functions applied to them, so e.g.
    a string-edit-distance makes no sense if your labels are integers,
    whereas interval distance needs numeric values.  A notable case of this
    is the MASI metric, which requires Python sets.
    c         C   sX   | |  _  t ƒ  |  _ t ƒ  |  _ t ƒ  |  _ g  |  _ | d j o |  i | ƒ n d S(   s.   Initialize an empty annotation task.

        N(   t   distancet   sett   It   Kt   Ct   datat   Nonet
   load_array(   t   selfR   R   (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   __init__F   s    		c         C   s   d i  t d „  |  i ƒ ƒ S(   Ns   
c         S   s2   d  |  d |  d i  d d ƒ d i |  d ƒ f S(   s   %s	%s	%st   codert   itemt   _s   	t   ,t   labels(   t   replacet   join(   t   x(    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   <lambda>S   s   (   R   t   mapR   (   R
   (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   __str__R   s    c         C   sy   xr | D]j \ } } } |  i  i | ƒ |  i i | ƒ |  i i | ƒ |  i i h  | d <| d <| d <ƒ q Wd S(   s­   Load the results of annotation.
        
        The argument is a list of 3-tuples, each representing a coder's labeling of an item:
            (coder,item,label)
        R   R   R   N(   R   t   addR   R   R   t   append(   R
   t   arrayR   R   R   (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyR	   W   s     c            s¹   t  ‡  ‡ f d †  |  i ƒ d } t  ‡  ‡ f d †  |  i ƒ d } d t |  i | d | d ƒ ƒ } t i d ˆ ˆ ˆ  | ƒ t i d d i | d ƒ d i | d ƒ d | ƒ | S(	   s6   Agreement between two coders on a given item

        c            s   |  d  ˆ j o |  d ˆ  j S(   R   R   (    (   R   (   t   it   cA(    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyR   g   s    i    c            s   |  d  ˆ j o |  d ˆ  j S(   R   R   (    (   R   (   R   t   cB(    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyR   h   s    g      ð?R   s.   Observed agreement between %s and %s on %s: %fs"   Distance between "%s" and "%s": %fR   (   t   filterR   t   floatR   t   loggingt   debugR   (   R
   R   R   R   t   kAt   kBt   ret(    (   R   R   R   s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   agrc   s    ""$		+c            s  ˆ t  j o? ˆ t  j o2 ˆ  t  j o% t t ‡ f d †  |  i ƒ ƒ } n¤ ˆ t  j oB ˆ t  j o5 ˆ  t  j o( t t ‡ ‡ f d †  |  i ƒ ƒ } nU ˆ t  j oB ˆ  t  j o5 ˆ t  j o( t t ‡ ‡  f d †  |  i ƒ ƒ } n d GHt i d ˆ ˆ ˆ  | ƒ t | ƒ S(   sH   Implements the "n-notation" used in Artstein and Poesio (2007)

        c            s   ˆ  |  d  j S(   R   (    (   R   (   t   k(    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyR   u   s    c            s   ˆ |  d  j o ˆ  |  d j S(   R   R   (    (   R   (   R   R%   (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyR   w   s    c            s   ˆ  |  d  j o ˆ |  d j S(   R   R   (    (   R   (   R%   t   c(    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyR   y   s    s&   You must pass either i or c, not both!s   Count on N[%s,%s,%s]: %d(   R   t   lenR   R   R   R    R   (   R
   R%   R   R&   R#   (    (   R&   R   R%   s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   Np   s    '%'('(c            sZ   t  t t ‡ ‡ ‡  f d †  ˆ i ƒ ƒ ƒ t  t ˆ i ƒ ƒ } t i d ˆ ˆ  | ƒ | S(   s=   Observed agreement between two coders on all items.

        c            s   ˆ  i  ˆ ˆ |  ƒ S(    (   R$   (   R   (   R
   R   R   (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyR   ƒ   s    s(   Observed agreement between %s and %s: %f(   R   t   sumR   R   R'   R   R    (   R
   R   R   R#   (    (   R   R
   R   s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   Ao   s    @c         C   s‹   |  i  i ƒ  } d } d } xO |  i  D]D } | i | ƒ x. | D]& } | |  i | | ƒ 7} | d 7} q? Wq% W| | } t i d | ƒ | S(   sA   Average observed agreement across all coders and items.

        g        g      ð?s   Average observed agreement: %f(   R   t   copyt   removeR*   R   R    (   R
   t   st   countert   totalR   R   R#   (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   avg_Ao‡   s    
  
c         C   sÙ   d } x |  i  D]v } xm |  i D]b } xY |  i D]N } | t |  i d | d | ƒ |  i d | d | ƒ ƒ |  i | | ƒ 7} q0 Wq  Wq Wd t t |  i  ƒ t |  i ƒ t |  i ƒ d ƒ | } t i d | ƒ | S(   s©   The observed disagreement for the alpha coefficient.

        The alpha coefficient, unlike the other metrics, uses this rather than
        observed agreement.
        g        R   R%   g      ð?i   s   Observed disagreement: %f(	   R   R   R   R(   R   R'   R   R   R    (   R
   R/   R   t   jt   lR#   (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   Do_alpha˜   s    
 
 
 T;g      ð?c      	      s¡   d } xg |  i  D]\ ‰  | |  i t ‡  ‡ f d †  |  i ƒ d d t ‡  ‡ f d †  |  i ƒ d d ƒ 7} q W| t |  i  ƒ | } t i d ˆ ˆ | ƒ | S(   sG   The observed disagreement for the weighted kappa coefficient.

        g        c            s   |  d  ˆ j o |  d ˆ  j S(   R   R   (    (   R   (   R   R   (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyR   ­   s    i    R   c            s   |  d  ˆ j o |  d ˆ  j S(   R   R   (    (   R   (   R   R   (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyR   ®   s    s+   Observed disagreement between %s and %s: %f(   R   R   R   R   R'   R   R    (   R
   R   R   t   max_distanceR/   R#   (    (   R   R   R   s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   Do_Kw_pairwise§   s    
 ,.c         C   s´   h  } x{ |  i  D]p } xg |  i  D]\ } t | | g ƒ | i ƒ  j o7 | | j o) |  i | | | ƒ | t | | g ƒ <q  q  Wq Wt | i ƒ  ƒ t | ƒ } t i d | ƒ | S(   s,   Averaged over all labelers
        
        s   Observed disagreement: %f(	   R   t	   frozensett   keysR5   R)   t   valuesR'   R   R    (   R
   R4   t   valsR   R   R#   (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   Do_Kw³   s    
 
 -1c         C   s5   d t  t |  i ƒ ƒ } |  i ƒ  | d | } | S(   s,   Bennett, Albert and Goldstein 1954

        g      ð?(   R   R'   R   R0   (   R
   t   AeR#   (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   SÁ   s    c         C   su   d } x+ |  i  D]  } | |  i d | ƒ d 7} q Wd d t t |  i ƒ d ƒ | } |  i ƒ  | d | } | S(   s   Scott 1955

        g        R%   i   g      ð?g      @i   (   R   R(   R   R'   R   R0   (   R
   R/   R%   R;   R#   (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   piÉ   s    
 %c         C   s   d  S(   N(    (   R
   (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   pi_avgÔ   s    c      	   C   sÌ   d } xu |  i  D]j } | t |  i d | d | ƒ ƒ t t |  i ƒ ƒ t |  i d | d | ƒ ƒ t t |  i ƒ ƒ 7} q W|  i | | ƒ | d | } t i d | | | ƒ t i d | | | ƒ | S(   s
   

        g        R&   R%   g      ð?s(   Expected agreement between %s and %s: %fs   Kappa between %s and %s: %f(	   R   R   R(   R'   R   R*   R   R    t   info(   R
   R   R   R;   R%   R#   (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   kappa_pairwise×   s    
 hc         C   sŸ   h  } xp |  i  D]e } x\ |  i  D]Q } | | j p d | | f | j o q  n |  i | | ƒ | d | | f <q  Wq Wt | i ƒ  ƒ t t | ƒ ƒ } | S(   s   Cohen 1960

        s   %s%s(   R   R@   R)   R8   R   R'   (   R
   R9   t   at   bR#   (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   kappaã   s    
 
 $("c         C   sÔ   d } xa |  i  D]V } xM |  i  D]B } | t |  i d | ƒ |  i d | ƒ ƒ |  i | | ƒ 7} q  Wq Wd t |  i ƒ t |  i ƒ t |  i ƒ t |  i ƒ d | } t i d | ƒ d |  i	 ƒ  | } | S(   s   Krippendorff 1980

        g        R%   g      ð?i   s   Expected disagreement: %f(
   R   R   R(   R   R'   R   R   R   R    R3   (   R
   t   DeR1   R2   R#   (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   alphað   s    
 
 DBc   
   	   C   sà   d } xg |  i  D]\ } xS |  i  D]H } | |  i d | d | ƒ |  i d | d | ƒ |  i | | ƒ 7} q  Wq W| | t t |  i ƒ d ƒ } t i d | | | ƒ |  i | | ƒ } d | | }	 t i	 d | | |	 ƒ |	 S(   s   Cohen 1968

        g        R&   R%   i   s+   Expected disagreement between %s and %s: %fg      ð?s$   Weighted kappa between %s and %s: %f(
   R   R(   R   t   powR'   R   R   R    R5   t   warning(
   R
   R   R   R4   R/   R1   R2   RD   t   DoR#   (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   weighted_kappa_pairwiseý   s    
 
 J c         C   s£   h  } xt |  i  D]i } x` |  i  D]U } | | j p t | | g ƒ | j o q  n |  i | | ƒ | t | | g ƒ <q  Wq Wt | i ƒ  ƒ t t | ƒ ƒ } | S(   s   Cohen 1968

        (   R   R6   RI   R)   R8   R   R'   (   R
   R9   RA   RB   R#   (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyt   weighted_kappa  s    
 
 &*"(   t   __name__t
   __module__t   __doc__R   t   binary_distanceR   R   R	   R$   R(   R*   R0   R3   R5   R:   R<   R=   R>   R@   RC   RE   RI   RJ   (    (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pyR   9   s&   												t   __main__s   -ds
   --distancet   destR   t   defaultRN   t   helps   distance metric to uses   -as   --agreementt	   agreementRC   s"   agreement coefficient to calculates   -es	   --excludet   excludet   actionR   s8   coder names to exclude (may be specified multiple times)s   -is	   --includet   includes.   coder names to include, same format as excludes   -fs   --filet   filesP   file to read labelings from, each line with three columns: 'labeler item labels's   -vs	   --verboset   verboset   0s+   how much debugging to print on stderr (0-4)s   -cs   --columnsept	   columnseps   	sI   char/string that separates the three columns in the file, defaults to tabs   -ls
   --labelsept   labelsepR   s[   char/string that separates labels (if labelers can assign more than one), defaults to commas   -ps
   --presencet   presences=   convert each labeling into 1 or 0, based on presence of LABELs   -Ts
   --thorought   thorought
   store_trues6   calculate agreement for every subset of the annotatorst   leveli2   i
   i    i   (    (,   RM   R   R   R   RK   t   ret   optparset   OptionParsert   parsert
   add_optionR   t   Falset
   parse_argst   optionst	   remainderRW   t
   print_helpt   exitt   basicConfigt   intRX   R   t   openR2   t   splitRZ   t   tokst   strR6   t   stripR[   R   t   objectR   RV   RT   R'   R   R\   t   getattrt   taskR]   RS   t   shutdown(    (    (    s,   /p/zhu/06/nlp/nltk/nltk/metrics/agreement.pys   <module>4   sb   
á!!

! A&&
(
