Ë
    çÍ:j&  ã                   ó¦   — d dl Z d dlmZ d dlmZ d dlmZ 	 d dlZd dl	m
Z
  G d„ de
«      Zd„ Zd	„ Z G d
„ d«      Z G d„ d«      Zy# e$ r Y Œ3w xY w)é    N)Úabstractmethod)Úsqrt)Ústdout)ÚClusterIc                   ó^   — e Zd ZdZdd„Zdd„Zed„ «       Zd„ Zed„ «       Z	d„ Z
d	„ Zd
„ Zd„ Zy)ÚVectorSpaceClustererz©
    Abstract clusterer which takes tokens and maps them into a vector space.
    Optionally performs singular value decomposition to reduce the
    dimensionality.
    Nc                 ó.   — d| _         || _        || _        y)a)  
        :param normalise:       should vectors be normalised to length 1
        :type normalise:        boolean
        :param svd_dimensions:  number of dimensions to use in reducing vector
                                dimensionsionality with SVD
        :type svd_dimensions:   int
        N)Ú_TtÚ_should_normaliseÚ_svd_dimensions)ÚselfÚ	normaliseÚsvd_dimensionss      úf/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/nltk/cluster/util.pyÚ__init__zVectorSpaceClusterer.__init__   s   € ð ˆŒØ!*ˆÔØ-ˆÕó    c                 ó"  — t        |«      dkD  sJ ‚| j                  rt        t        | j                  |«      «      }| j
                  �r| j
                  t        |d   «      k  rôt        j                  j                  t        j                  t        j                  |«      «      «      \  }}}|d | j
                   t        j                  | j
                  t        j                  «      z  }|d d …d | j
                  …f   }|d | j
                  …d d …f   }	t        j                  t        j                  ||	«      «      }t        j                  |«      | _        | j                  ||«       |r|D �
cg c]  }
| j!                  |
«      ‘Œ c}
S y c c}
w ©Nr   )Úlenr   ÚlistÚmapÚ
_normaliser   ÚnumpyÚlinalgÚsvdÚ	transposeÚarrayÚidentityÚfloat64Údotr
   Úcluster_vectorspaceÚclassify)r   ÚvectorsÚassign_clustersÚtraceÚuÚdÚvtÚSÚTÚDtÚvectors              r   ÚclusterzVectorSpaceClusterer.cluster(   sL  € Ü�7‹|˜aÒÐÐð ×!Ò!Üœ3˜tŸ™°Ó8Ó9ˆGð ×Ó D×$8Ñ$8¼3¸wÀq¹z»?Ò$JÜŸ™×)Ñ)¬%¯/©/¼%¿+¹+ÀgÓ:NÓ*OÓP‰JˆQ��2ØÐ(�D×(Ñ(Ð)¬E¯N©NØ×$Ñ$¤e§m¡mó-ñ ˆAð ’!Ð+�t×+Ñ+Ð+Ð+Ñ,ˆAØÐ*�d×*Ñ*Ð*ªAÐ-Ñ.ˆBÜ—o‘o¤e§i¡i°°2Ó&6Ó7ˆGÜ—‘ qÓ)ˆDŒHð 	× Ñ  ¨%Ô0ñ Ø8?Ö@¨f�D—M‘M &Õ)Ò@Ð@ð ùÚ@s   Å0Fc                  ó   — y)zD
        Finds the clusters using the given set of vectors.
        N© )r   r#   r%   s      r   r!   z(VectorSpaceClusterer.cluster_vectorspaceA   ó   � r   c                 óØ   — | j                   r| j                  |«      }| j                  � t        j                  | j                  |«      }| j                  |«      }| j                  |«      S ©N)r   r   r
   r   r    Úclassify_vectorspaceÚcluster_name)r   r,   r-   s      r   r"   zVectorSpaceClusterer.classifyG   sX   € Ø×!Ò!Ø—_‘_ VÓ,ˆFØ�8‰8ÐÜ—Y‘Y˜tŸx™x¨Ó0ˆFØ×+Ñ+¨FÓ3ˆØ× Ñ  Ó)Ð)r   c                  ó   — y)zN
        Returns the index of the appropriate cluster for the vector.
        Nr/   ©r   r,   s     r   r3   z)VectorSpaceClusterer.classify_vectorspaceO   r0   r   c                 ó¸   — | j                   r| j                  |«      }| j                  � t        j                  | j                  |«      }| j                  ||«      S r2   )r   r   r
   r   r    Úlikelihood_vectorspace)r   r,   Úlabels      r   Ú
likelihoodzVectorSpaceClusterer.likelihoodU   sJ   € Ø×!Ò!Ø—_‘_ VÓ,ˆFØ�8‰8ÐÜ—Y‘Y˜tŸx™x¨Ó0ˆFØ×*Ñ*¨6°5Ó9Ð9r   c                 ó6   — | j                  |«      }||k(  rdS dS )zP
        Returns the likelihood of the vector belonging to the cluster.
        g      ð?g        )r3   )r   r,   r-   Ú	predicteds       r   r8   z+VectorSpaceClusterer.likelihood_vectorspace\   s&   € ð ×-Ñ-¨fÓ5ˆ	Ø Ò*ˆsÐ3°Ð3r   c                 ó˜   — | j                   r| j                  |«      }| j                  � t        j                  | j                  |«      }|S )zU
        Returns the vector after normalisation and dimensionality reduction
        )r   r   r
   r   r    r6   s     r   r,   zVectorSpaceClusterer.vectorc   s>   € ð ×!Ò!Ø—_‘_ VÓ,ˆFØ�8‰8ÐÜ—Y‘Y˜tŸx™x¨Ó0ˆFØˆr   c                 óF   — |t        t        j                  ||«      «      z  S )z7
        Normalises the vector to unit length.
        ©r   r   r    r6   s     r   r   zVectorSpaceClusterer._normalisem   s   € ð œœUŸY™Y v¨vÓ6Ó7Ñ7Ð7r   )FN)FF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r-   r   r!   r"   r3   r:   r8   r,   r   r/   r   r   r   r      sS   „ ñó
.óAð2 ñó ðò
*ð ñó ðò
:ò4òó8r   r   c                 óJ   — | |z
  }t        t        j                  ||«      «      S )z}
    Returns the euclidean distance between vectors u and v. This is equivalent
    to the length of the vector (u - v).
    r?   )r&   ÚvÚdiffs      r   Úeuclidean_distancerG   t   s#   € ð
 ˆq‰5€DÜ”—	‘	˜$ Ó%Ó&Ð&r   c           	      ó´   — dt        j                  | |«      t        t        j                  | | «      «      t        t        j                  ||«      «      z  z  z
  S )zw
    Returns 1 minus the cosine of the angle between vectors v and u. This is
    equal to ``1 - (u.v / |u||v|)``.
    é   )r   r    r   )r&   rE   s     r   Úcosine_distancerJ   }   sA   € ð
 ”—	‘	˜!˜Q“¤4¬¯	©	°!°Q«Ó#8¼4ÄÇ	Á	È!ÈQÃÓ;PÑ#PÑQÑRÐRr   c                   ó*   — e Zd ZdZd„ Zdd„Zd„ Zd„ Zy)Ú_DendrogramNodezTree node of a dendrogram.c                 ó    — || _         || _        y r2   )Ú_valueÚ	_children)r   ÚvalueÚchildrens      r   r   z_DendrogramNode.__init__ˆ   s   € ØˆŒØ!ˆ�r   c                 ó¨   — | j                   r5g }| j                   D ]"  }|j                  |j                  |«      «       Œ$ |S |r| j                  gS | gS r2   )rO   ÚextendÚleavesrN   )r   ÚvaluesrT   Úchilds       r   rT   z_DendrogramNode.leavesŒ   sQ   € Ø�>Š>ØˆFØŸ™ò 4�Ø—‘˜eŸl™l¨6Ó2Õ3ð4àˆMÙØ—K‘K�=Ð à�6ˆMr   c                 óÖ  — | j                   | fg}t        |«      |k  r |j                  «       \  }}|j                  s|j	                  ||f«       nm|j                  D ]?  }|j                  r|j                  |j                   |f«       Œ-|j                  d|f«       ŒA |j                  «        t        |«      |k  rŒ g }|D ]$  \  }}|j                  |j                  «       «       Œ& |S r   )rN   r   ÚpoprO   ÚpushÚappendÚsortrT   )r   ÚnÚqueueÚpriorityÚnoderV   Úgroupss          r   r`   z_DendrogramNode.groups—   sÐ   € Ø—+‘+˜tÐ$Ð%ˆä�%‹j˜1ŠnØ"ŸY™Y›[‰NˆH�dØ—>’>Ø—
‘
˜H dÐ+Ô,ØØŸ™ò -�Ø—?’?Ø—L‘L %§,¡,°Ð!6Õ7à—L‘L ! U Õ,ð	-ð �J‰JŒLô �%‹j˜1‹nð ˆØ#ò 	)‰NˆH�dØ�M‰M˜$Ÿ+™+›-Õ(ð	)àˆr   c                 óH   — t        | j                  |j                  «      dk  S r   )rJ   rN   )r   Ú
comparators     r   Ú__lt__z_DendrogramNode.__lt__¬   s   € Ü˜tŸ{™{¨J×,=Ñ,=Ó>ÀÑBÐBr   N)T)r@   rA   rB   rC   r   rT   r`   rc   r/   r   r   rL   rL   …   s   „ Ù$ò"ó	òó*Cr   rL   c                   ó6   — e Zd ZdZg fd„Zd„ Zd„ Zg fd„Zd„ Zy)Ú
Dendrograma  
    Represents a dendrogram, a tree with a specified branching order.  This
    must be initialised with the leaf items, then iteratively call merge for
    each branch. This class constructs a tree representing the order of calls
    to the merge function.
    c                 ó    — |D �cg c]  }t        |«      ‘Œ c}| _        t        j                  | j                  «      | _        d| _        yc c}w )zs
        :param  items: the items at the leaves of the dendrogram
        :type   items: sequence of (any)
        rI   N)rL   Ú_itemsÚcopyÚ_original_itemsÚ_merge)r   ÚitemsÚitems      r   r   zDendrogram.__init__¸   s=   € ð
 :?Ö?°” tÕ,Ò?ˆŒÜ#Ÿy™y¨¯©Ó5ˆÔØˆ�ùò @s   …Ac                 óà   ‡ — t        |«      dk\  sJ ‚t        ‰ j                  gˆ fd„|D «       ¢­Ž }‰ xj                  dz  c_        |‰ j                  |d   <   |dd D ]  }‰ j                  |= Œ y)a=  
        Merges nodes at given indices in the dendrogram. The nodes will be
        combined which then replaces the first node specified. All other nodes
        involved in the merge will be removed.

        :param  indices: indices of the items to merge (at least two)
        :type   indices: seq of int
        é   c              3   ó<   •K  — | ]  }‰j                   |   –— Œ y ­wr2   )rg   )Ú.0Úir   s     €r   ú	<genexpr>z#Dendrogram.merge.<locals>.<genexpr>Ë   s   øè ø€ Ò-NÀ¨d¯k©k¸!­nÑ-Nùs   ƒrI   r   N)r   rL   rj   rg   )r   Úindicesr_   rq   s   `   r   ÚmergezDendrogram.mergeÁ   sp   ø€ ô �7‹|˜qÒ Ð Ð Ü˜tŸ{™{ÐOÓ-NÀgÔ-NÒOˆØ�Š�qÑ�Ø"&ˆ�‰�G˜A‘JÑØ˜˜�ò 	ˆAØ—‘˜A‘ñ	r   c                 ó´   — t        | j                  «      dkD  r!t        | j                  g| j                  ¢­Ž }n| j                  d   }|j	                  |«      S )z’
        Finds the n-groups of items (leaves) reachable from a cut at depth n.
        :param  n: number of groups
        :type   n: int
        rI   r   )r   rg   rL   rj   r`   )r   r\   Úroots      r   r`   zDendrogram.groupsÑ   sG   € ô ˆt�{‰{Ó˜aÒÜ" 4§;¡;Ð=°·±Ò=‰Dà—;‘;˜q‘>ˆDØ�{‰{˜1‹~Ðr   c                 óþ  ‡‡‡— d\  }}}t        | j                  «      dkD  r!t        | j                  g| j                  ¢­Ž }n| j                  d   }| j                  }|r|}n|D �cg c]  }d|j
                  z  ‘Œ }}t        t        t         |«      «      dz   Š‰dz  Št        ‰‰z
  dz
  «      Šdˆˆfd„	}	d„ }
|j
                  |fg}|D �cg c]
  } |	d«      ‘Œ }}|�rj|j                  «       \  }}t        t        d	„ |j                  «      «      }t        t        |j                  |«      «      }|rt        |«      }t        |«      }t        t        |«      «      D ]{  }||   |v rH|k(  r |
 |	|d|«      «       n&|k(  r |
 |	||d«      «       n |
 |	|||«      «        |	|«      ||<   ŒR|cxk  rk  rn n |
 |	|||«      «       Œq |
||   «       Œ}  |
d
«       |j                  D ],  }|j                  sŒ|j                  |j
                  |f«       Œ. |j!                  «        |D ]
  } |
|«       Œ  |
d
«       |r�Œj |
dj#                  ˆfd„|D «       «      «        |
d
«       yc c}w c c}w )zØ
        Print the dendrogram in ASCII art to standard out.

        :param leaf_labels: an optional list of strings to use for labeling the
                            leaves
        :type leaf_labels: list
        )ú+ú-ú|rI   r   z%srn   ú c                 ó    •— ‰|z  › | › |‰z  › �S r2   r/   )ÚcentreÚleftÚrightÚlhalfÚrhalfs      €€r   ÚformatzDendrogram.show.<locals>.formatû   s   ø€ Ø˜d‘l�^ F 8¨E°E©M¨?Ð;Ð;r   c                 ó.   — t        j                  | «       y r2   )r   Úwrite)Ústrs    r   Údisplayz Dendrogram.show.<locals>.displayþ   s   € Ü�L‰L˜Õr   c                 ó*   — | j                  d«      d   S )NFr   )rT   )Úcs    r   ú<lambda>z!Dendrogram.show.<locals>.<lambda>  s   € °·±¸%³ÀÑ1C€ r   ú
Ú c              3   ó@   •K  — | ]  }|j                  ‰«      –— Œ y ­wr2   )Úcenter)rp   rl   Úwidths     €r   rr   z"Dendrogram.show.<locals>.<genexpr>#  s   øè ø€ Ò@¨t˜Ÿ™ E×*Ñ@ùs   ƒN)r{   r{   )r   rg   rL   rj   ri   rN   Úmaxr   ÚintrX   r   rO   ÚindexÚminÚrangerZ   r[   Újoin)r   Úleaf_labelsÚJOINÚHLINKÚVLINKrv   rT   Úlast_rowÚleafr‚   r†   r]   Ú	verticalsr^   r_   Úchild_left_leafrs   Úmin_idxÚmax_idxrq   rV   Úverticalr€   r�   rŽ   s                         @@@r   ÚshowzDendrogram.showÝ   s^  ú€ ð +Ñˆˆe�Uô ˆt�{‰{Ó˜aÒÜ" 4§;¡;Ð=°·±Ò=‰Dà—;‘;˜q‘>ˆDØ×%Ñ%ˆáØ"‰Hà7=Ö>¨t˜˜tŸ{™{Ó*Ð>ˆHÐ>ô ”Cœ˜XÓ&Ó'¨!Ñ+ˆØ˜‘
ˆÜ�E˜E‘M AÑ%Ó&ˆö	<ò	ð —+‘+˜tÐ$Ð%ˆØ-3Ö4 T‘V˜C•[Ð4ˆ	Ð4ÚØ"ŸY™Y›[‰NˆH�dÜ"¤3Ñ'CÀTÇ^Á^Ó#TÓUˆOÜœ3˜vŸ|™|¨_Ó=Ó>ˆGÙÜ˜g›,�Ü˜g›,�Üœ3˜v›;Ó'ò *�Ø˜!‘9 Ñ/Ø˜G’|Ù¡ t¨S°%Ó 8Õ9Ø˜gšÙ¡ t¨U°CÓ 8Õ9á¡ t¨U°EÓ :Ô;Ù#)¨%£=�I˜a’LØ Ô, WÕ,Ù™F 5¨%°Ó7Õ8á˜I a™LÕ)ð*ñ �DŒMØŸ™ò 8�Ø—?“?Ø—L‘L %§,¡,°Ð!6Õ7ð8ð �J‰JŒLà%ò "�Ù˜Õ!ð"á�DŒMó9 ñ> 	�—‘Ó@°xÔ@Ó@ÔAÙ��ùòc ?ùò  5s   Á'I5ÃI:c                 óÐ   — t        | j                  «      dkD  r!t        | j                  g| j                  ¢­Ž }n| j                  d   }|j	                  d«      }dt        |«      z  S )NrI   r   Fz<Dendrogram with %d leaves>)r   rg   rL   rj   rT   )r   rv   rT   s      r   Ú__repr__zDendrogram.__repr__&  sT   € Üˆt�{‰{Ó˜aÒÜ" 4§;¡;Ð=°·±Ò=‰Dà—;‘;˜q‘>ˆDØ—‘˜UÓ#ˆØ,¬s°6«{Ñ:Ð:r   N)	r@   rA   rB   rC   r   rt   r`   r    r¢   r/   r   r   re   re   °   s,   „ ñð  ó òò 
ð  "ó GóR;r   re   )rh   Úabcr   Úmathr   Úsysr   r   ÚImportErrorÚnltk.cluster.apir   r   rG   rJ   rL   re   r/   r   r   ú<module>r¨      sh   ðó Ý Ý Ý ð	Ûõ &ô\8˜8ô \8ò~'òS÷(Cñ (C÷V|;ò |;øðC ò 	Ùð	ús   ˜A ÁAÁA