+
    ÖÇÜh^  ã                   óX   € R t  ^ RItRR ltR tR tRR ltR	R ltR#   ] d     L i ; i)
a  
Text Segmentation Metrics

1. Windowdiff

Pevzner, L., and Hearst, M., A Critique and Improvement of
  an Evaluation Metric for Text Segmentation,
  Computational Linguistics 28, 19-36


2. Generalized Hamming Distance

Bookstein A., Kulyukin V.A., Raita T.
Generalized Hamming Distance
Information Retrieval 5, 2002, pp 353-375

Baseline implementation in C++
http://digital.cs.usu.edu/~vkulyukin/vkweb/software/ghd/ghd.html

Study describing benefits of Generalized Hamming Distance Versus
WindowDiff for evaluating text segmentation tasks
Begsten, Y.  Quel indice pour mesurer l'efficacite en segmentation de textes ?
TALN 2009


3. Pk text segmentation metric

Beeferman D., Berger A., Lafferty J. (1999)
Statistical Models for Text Segmentation
Machine Learning, 34, 177-210
Nc                óà  € \        V 4      \        V4      8w  d   \        R4      hV\        V 4      8”  d   \        R4      h^ p\        \        V 4      V,
          ^,           4       Fh  p\        WWb,            P	                  V4      WWb,            P	                  V4      ,
          4      pV'       d   WW,          pKU  V\        ^V4      ,          pKj  	  V\        V 4      V,
          R,           ,          # )aó  
Compute the windowdiff score for a pair of segmentations.  A
segmentation is any sequence over a vocabulary of two items
(e.g. "0", "1"), where the specified boundary value is used to
mark the edge of a segmentation.

    >>> s1 = "000100000010"
    >>> s2 = "000010000100"
    >>> s3 = "100000010000"
    >>> '%.2f' % windowdiff(s1, s1, 3)
    '0.00'
    >>> '%.2f' % windowdiff(s1, s2, 3)
    '0.30'
    >>> '%.2f' % windowdiff(s2, s3, 3)
    '0.80'

:param seg1: a segmentation
:type seg1: str or list
:param seg2: a segmentation
:type seg2: str or list
:param k: window width
:type k: int
:param boundary: boundary value
:type boundary: str or int or bool
:param weighted: use the weighted variant of windowdiff
:type weighted: boolean
:rtype: float
z!Segmentations have unequal lengthzCWindow width k should be smaller or equal than segmentation lengthsç      ð?)ÚlenÚ
ValueErrorÚrangeÚabsÚcountÚmin)Úseg1Úseg2ÚkÚboundaryÚweightedÚwdÚiÚndiffs   &&&&&   Ú;/usr/lib/python3/dist-packages/nltk/metrics/segmentation.pyÚ
windowdiffr   1   sÃ   € ô< ˆ4ƒy”C˜“IÔÜÐ<Ó=Ð=ØŒ3ˆt‹9„}ÜØQó
ð 	
ð 
€BÜ”3�t“9˜q•= 1Õ$Ö%ˆÜ�D˜Q�U�O×)Ñ)¨(Ó3°d¸q½u°o×6KÑ6KÈHÓ6UÕUÓVˆßØ�KŠBà”#�a˜“-ÕŠBñ &ð ”�T“˜Q• Õ$Õ%Ð%ó    c                 ó´   € \         P                  ! W34      pV\         P                  ! V4      ,          VR&   V\         P                  ! V 4      ,          VR&   V# )é    )r   ºNNN)r   r   )ÚnpÚemptyÚarange)ÚnrowsÚncolsÚins_costÚdel_costÚmats   &&&& r   Ú	_init_matr    b   sE   € Ü
�(Š(�E�>Ó
"€CØœ2Ÿ9š9 UÓ+Õ+€Cˆ�IØœ2Ÿ9š9 UÓ+Õ+€Cˆ�IØ€Jr   c                 ól  € \        V4       F¤  w  rg\        V4       F�  w  r‰V\        Wy,
          4      ,          WV3,          ,           p
Wy8X  d   WV3,          pM6Wy8”  d   W@Wh^,           3,          ,           pMW0V^,           V3,          ,           p\        Wº4      W^,           V^,           3&   K’  	  K¦  	  R# )é   N)Ú	enumerater   r	   )r   ÚrowvÚcolvr   r   Úshift_cost_coeffr   ÚrowiÚjÚcoljÚ
shift_costÚtcosts   &&&&&&      r   Ú_ghd_auxr,   i   s”   € Ü˜T–?‰ˆÜ  –‰GˆAØ)¬C°µÓ,<Õ<¸sÀaÀ4½yÕHˆJØŒ|à˜q˜D�	‘Ø”à  q¨a­% x¥=Õ0‘ð ! q¨1¥u¨a x¥=Õ0�Ü # EÓ 6ˆC�A•�q˜1•u�Óó 'ó #r   c                óÊ  € \        V 4       UUu. uF  w  rgWu8X  g   K  VNK  	  ppp\        V4       UUu. uF  w  rgWu8X  g   K  VNK  	  p	pp\        V4      p
\        V	4      pV
^ 8X  d
   V^ 8X  d   R# V
^ 8”  d   V^ 8X  d	   W¢,          # V
^ 8X  d   V^ 8”  d	   W³,          # \        V^,           V
^,           W#4      p\        WÉW‚W44       \	        VR,          4      # u uppi u uppi )aÚ  
Compute the Generalized Hamming Distance for a reference and a hypothetical
segmentation, corresponding to the cost related to the transformation
of the hypothetical segmentation into the reference segmentation
through boundary insertion, deletion and shift operations.

A segmentation is any sequence over a vocabulary of two items
(e.g. "0", "1"), where the specified boundary value is used to
mark the edge of a segmentation.

Recommended parameter values are a shift_cost_coeff of 2.
Associated with a ins_cost, and del_cost equal to the mean segment
length in the reference segmentation.

    >>> # Same examples as Kulyukin C++ implementation
    >>> ghd('1100100000', '1100010000', 1.0, 1.0, 0.5)
    0.5
    >>> ghd('1100100000', '1100000001', 1.0, 1.0, 0.5)
    2.0
    >>> ghd('011', '110', 1.0, 1.0, 0.5)
    1.0
    >>> ghd('1', '0', 1.0, 1.0, 0.5)
    1.0
    >>> ghd('111', '000', 1.0, 1.0, 0.5)
    3.0
    >>> ghd('000', '111', 1.0, 2.0, 0.5)
    6.0

:param ref: the reference segmentation
:type ref: str or list
:param hyp: the hypothetical segmentation
:type hyp: str or list
:param ins_cost: insertion cost
:type ins_cost: float
:param del_cost: deletion cost
:type del_cost: float
:param shift_cost_coeff: constant used to compute the cost of a shift.
    ``shift cost = shift_cost_coeff * |i - j|`` where ``i`` and ``j``
    are the positions indicating the shift
:type shift_cost_coeff: float
:param boundary: boundary value
:type boundary: str or int or bool
:rtype: float
g        )éÿÿÿÿr.   )r#   r   r    r,   Úfloat)ÚrefÚhypr   r   r&   r   r   ÚvalÚref_idxÚhyp_idxÚ
nref_boundÚ
nhyp_boundr   s   &&&&&&       r   Úghdr7   y   sÍ   € ô\ "+¨3¤ÔC¡‘X�a°3±?�qˆq¡€GÑCÜ!*¨3¤ÔC¡‘X�a°3±?�qˆq¡€GÑCä�W“€JÜ�W“€Jà�Q„˜:¨œ?ÙØ	�aŒ˜J¨!œOØÕ$Ð$Ø	�qŒ˜Z¨!œ^ØÕ$Ð$ä
�J •N J°¥N°HÓ
G€CÜˆS˜7¨hÔIÜ��V•ÓÐùó DùÛCs   �
CžCµ
CÁCc                ó¦  € Vf;   \        \        \        V 4      V P                  V4      R,          ,          4      4      p^ p\	        \        V 4      V,
          ^,           4       FL  pWWR,            P                  V4      ^ 8„  pWWR,            P                  V4      ^ 8„  pWg8w  g   KC  V^,          pKN  	  V\        V 4      V,
          R,           ,          # )a©  
Compute the Pk metric for a pair of segmentations A segmentation
is any sequence over a vocabulary of two items (e.g. "0", "1"),
where the specified boundary value is used to mark the edge of a
segmentation.

>>> '%.2f' % pk('0100'*100, '1'*400, 2)
'0.50'
>>> '%.2f' % pk('0100'*100, '0'*400, 2)
'0.50'
>>> '%.2f' % pk('0100'*100, '0100'*100, 2)
'0.00'

:param ref: the reference segmentation
:type ref: str or list
:param hyp: the segmentation to evaluate
:type hyp: str or list
:param k: window size, if None, set to half of the average reference segment length
:type boundary: str or int or bool
:param boundary: boundary value
:type boundary: str or int or bool
:rtype: float
ç       @r   )ÚintÚroundr   r   r   )r0   r1   r   r   Úerrr   ÚrÚhs   &&&&    r   Úpkr?   ¼   s®   € ð2 	‚yÜ””c˜#“h #§)¡)¨HÓ"5¸Õ";Õ<Ó=Ó>ˆà
€CÜ”3�s“8˜a•< !Õ#Ö$ˆØ�A•EˆN× Ñ  Ó*¨QÑ.ˆØ�A•EˆN× Ñ  Ó*¨QÑ.ˆØŽ6Ø�1�HŠCñ	 %ð
 ”#�c“(˜Q•, Õ$Õ%Ð%r   )Ú1F)r9   r9   r   r@   )Nr@   )	Ú__doc__Únumpyr   ÚImportErrorr   r    r,   r7   r?   © r   r   Ú<module>rE      sC   ðñð@	Ûô
+&òbò7ô =öF"&øð_ ô 	Ùð	ús   „ Ÿ)¨)