
    gQ                     "    d Z d Zd Zd ZddZy)z#
Tools for comparing ranked lists.
c              #      K   t        |       } t        |      }| D ]  }	 || |   ||   z
  f  y# t        $ r Y !w xY ww)zFinds the difference between the values in ranks1 and ranks2 for keys
    present in both dicts. If the arguments are not dicts, they are converted
    from (key, rank) sequences.
    N)dictKeyError)ranks1ranks2ks      J/var/www/openai/venv/lib/python3.12/site-packages/nltk/metrics/spearman.py_rank_distsr	      sT     
 &\F&\F	VAY***   		s    A 1A 	=A =A c                     d}d}t        | |      D ]  \  }}|||z  z  }|dz  } 	 dd|z  |||z  dz
  z  z  z
  S # t        $ r Y yw xY w)aU  Returns the Spearman correlation coefficient for two rankings, which
    should be dicts or sequences of (key, rank). The coefficient ranges from
    -1.0 (ranks are opposite) to 1.0 (ranks are identical), and is only
    calculated for keys in both rankings (for meaningful results, remove keys
    present in only one list before ranking).          g        )r	   ZeroDivisionError)r   r   nresr   ds         r   spearman_correlationr      sp     	
A
CFF+1q1u	Q ,AGqAEAI/00 s   ; 	AAc                 &    d t        |       D        S )zGiven a sequence, yields each element with an increasing rank, suitable
    for use as an argument to ``spearman_correlation``.
    c              3   *   K   | ]  \  }}||f  y w)N ).0ir   s      r   	<genexpr>z&ranks_from_sequence.<locals>.<genexpr>1   s     .~tq!QF~s   )	enumerate)seqs    r   ranks_from_sequencer   -   s     /y~..    c              #      K   d}d}t        |       D ]$  \  }\  }}	 t        ||z
        |kD  r|}||f |}& y# t        $ r Y w xY ww)zGiven a sequence of (key, score) tuples, yields each key with an
    increasing rank, tying with previous key's rank if the difference between
    their scores is less than rank_gap. Suitable for use as an argument to
    ``spearman_correlation``.
    Nr   )r   abs	TypeError)scoresrank_gap
prev_scorerankr   keyscores          r   ranks_from_scoresr&   4   sh      JD$V,<C	5:%&1 4i
 -  		s%   A	:A		AA	AA	N)gV瞯<)__doc__r	   r   r   r&   r   r   r   <module>r(      s   
$/r   