ó
    ´OÂf&  ã                   ó¨   • S SK r S SKJr  S SKJr  S SKJr   S SKrS SK	J
r
   " S S\
5      rS rS	 r " S
 S5      r " S S5      rg! \ a     N4f = f)é    N)Úabstractmethod)Úsqrt)Ústdout)ÚClusterIc                   ój   • \ rS rSrSrSS jrSS jr\S 5       rS r	\S 5       r
S	 rS
 rS rS rSrg)ÚVectorSpaceClustereré   z™
Abstract clusterer which takes tokens and maps them into a vector space.
Optionally performs singular value decomposition to reduce the
dimensionality.
Nc                 ó*   • SU l         Xl        X l        g)zù
: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      Ú3/usr/lib/python3/dist-packages/nltk/cluster/util.pyÚ__init__ÚVectorSpaceClusterer.__init__   s   € ð ˆŒØ!*ÔØ-Õó    c                 óN  • [        U5      S:”  d   eU R                  (       a  [        [        U R                  U5      5      nU R
                  (       Ga  U R
                  [        US   5      :  aø  [        R                  R                  [        R                  " [        R                  " U5      5      5      u  pEnUS U R
                   [        R                  " U R
                  [        R                  5      -  nUS S 2S U R
                  24   nUS U R
                  2S S 24   n	[        R                  " [        R                  " Xy5      5      n[        R                  " U5      U l        U R                  X5        U(       a!  U V
s/ s H  o R!                  U
5      PM     sn
$ g s  sn
f ©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   ÚclusterÚVectorSpaceClusterer.cluster(   sK  € Ü�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°Ó&6Ó7ˆGÜ—’ qÓ)ˆDŒHð 	× Ñ  Ô0ö Ù8?Ó@º¨f—M‘M &Ö)¹Ñ@Ð@ð ùÚ@s   ÆF"c                 ó   • g)z4
Finds the clusters using the given set of vectors.
N© )r   r&   r(   s      r   r$   Ú(VectorSpaceClusterer.cluster_vectorspaceA   ó   � r   c                 óæ   • U R                   (       a  U R                  U5      nU R                  b!  [        R                  " U R                  U5      nU R                  U5      nU R                  U5      $ ©N)r   r   r   r   r#   Úclassify_vectorspaceÚcluster_name)r   r/   r0   s      r   r%   ÚVectorSpaceClusterer.classifyG   sX   € Ø×!×!Ø—_‘_ VÓ,ˆFØ�8‰8ÑÜ—Y’Y˜tŸx™x¨Ó0ˆFØ×+Ñ+¨FÓ3ˆØ× Ñ  Ó)Ð)r   c                 ó   • g)z>
Returns the index of the appropriate cluster for the vector.
Nr3   ©r   r/   s     r   r8   Ú)VectorSpaceClusterer.classify_vectorspaceO   r5   r   c                 óÄ   • U R                   (       a  U R                  U5      nU R                  b!  [        R                  " U R                  U5      nU R                  X5      $ r7   )r   r   r   r   r#   Úlikelihood_vectorspace)r   r/   Úlabels      r   Ú
likelihoodÚVectorSpaceClusterer.likelihoodU   sH   € Ø×!×!Ø—_‘_ VÓ,ˆFØ�8‰8ÑÜ—Y’Y˜tŸx™x¨Ó0ˆFØ×*Ñ*¨6Ó9Ð9r   c                 ó6   • U R                  U5      nX#:X  a  S$ S$ )z@
Returns the likelihood of the vector belonging to the cluster.
g      ð?g        )r8   )r   r/   r0   Ú	predicteds       r   r?   Ú+VectorSpaceClusterer.likelihood_vectorspace\   s$   € ð ×-Ñ-¨fÓ5ˆ	ØÓ*ˆsÐ3°Ð3r   c                 ó¦   • U R                   (       a  U R                  U5      nU R                  b!  [        R                  " U R                  U5      nU$ )zE
Returns the vector after normalisation and dimensionality reduction
)r   r   r   r   r#   r<   s     r   r/   ÚVectorSpaceClusterer.vectorc   s>   € ð ×!×!Ø—_‘_ VÓ,ˆFØ�8‰8ÑÜ—Y’Y˜tŸx™x¨Ó0ˆFØˆr   c                 óF   • U[        [        R                  " X5      5      -  $ )z'
Normalises the vector to unit length.
©r   r   r#   r<   s     r   r   ÚVectorSpaceClusterer._normalisem   s   € ð œœUŸYšY vÓ6Ó7Ñ7Ð7r   r   )FN)FF)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r0   r   r$   r%   r8   rA   r?   r/   r   Ú__static_attributes__r3   r   r   r   r      sS   † ñô
.ôAð2 ñó ðò
*ð ñó ðò
:ò4òõ8r   r   c                 óH   • X-
  n[        [        R                  " X"5      5      $ )zq
Returns the euclidean distance between vectors u and v. This is equivalent
to the length of the vector (u - v).
rI   )r)   ÚvÚdiffs      r   Úeuclidean_distancerT   t   s   € ð
 ‰5€DÜ”—	’	˜$Ó%Ó&Ð&r   c           	      ó´   • S[         R                  " X5      [        [         R                  " X 5      5      [        [         R                  " X5      5      -  -  -
  $ )zk
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)   rR   s     r   Úcosine_distancerW   }   s;   € ð
 ”—	’	˜!“¤4¬¯	ª	°!«Ó#8¼4ÄÇ	Â	È!ÃÓ;PÑ#PÑQÑRÐRr   c                   ó4   • \ rS rSrSrS rS	S jrS rS rSr	g)
Ú_DendrogramNodeé…   zTree node of a dendrogram.c                 ó   • Xl         X l        g r7   )Ú_valueÚ	_children)r   ÚvalueÚchildrens      r   r   Ú_DendrogramNode.__init__ˆ   s   € ØŒØ!�r   c                 óÀ   • U R                   (       a7  / nU R                    H#  nUR                  UR                  U5      5        M%     U$ U(       a  U R                  /$ U /$ r7   )r]   ÚextendÚleavesr\   )r   Úvaluesrc   Úchilds       r   rc   Ú_DendrogramNode.leavesŒ   sL   € Ø�>�>ØˆFØŸœ�Ø—‘˜eŸl™l¨6Ó2Ö3ñ (àˆMÞØ—K‘K�=Ð à�6ˆMr   c                 óô  • U R                   U 4/n[        U5      U:  a­  UR                  5       u  p4UR                  (       d  UR	                  X445        OwUR                   HF  nUR                  (       a  UR                  UR                   U45        M3  UR                  SU45        MH     UR                  5         [        U5      U:  a  M­  / nU H$  u  p4UR                  UR                  5       5        M&     U$ r   )r\   r   Úpopr]   ÚpushÚappendÚsortrc   )r   ÚnÚqueueÚpriorityÚnodere   Úgroupss          r   rp   Ú_DendrogramNode.groups—   sÀ   € Ø—+‘+˜tÐ$Ð%ˆä�%‹j˜1‹nØ"ŸY™Y›[‰NˆHØ—>—>Ø—
‘
˜HÐ+Ô,ØØŸœ�Ø—?—?Ø—L‘L %§,¡,°Ð!6Ö7à—L‘L ! U Ö,ñ	 (ð �J‰JŒLô �%‹j˜1�nð ˆÛ#‰NˆHØ�M‰M˜$Ÿ+™+›-Ö(ñ $àˆr   c                 óH   • [        U R                  UR                  5      S:  $ r   )rW   r\   )r   Ú
comparators     r   Ú__lt__Ú_DendrogramNode.__lt__¬   s   € Ü˜tŸ{™{¨J×,=Ñ,=Ó>ÀÑBÐBr   )r]   r\   N)T)
rK   rL   rM   rN   rO   r   rc   rp   rt   rP   r3   r   r   rY   rY   …   s   † Ù$ò"ô	òõ*Cr   rY   c                   óB   • \ rS rSrSr/ 4S jrS rS r/ 4S jrS r	Sr
g	)
Ú
Dendrogramé°   zó
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                 ó¨   • U Vs/ s H  n[        U5      PM     snU l        [        R                  " U R                  5      U l        SU l        gs  snf )z[
:param  items: the items at the leaves of the dendrogram
:type   items: sequence of (any)
rV   N)rY   Ú_itemsÚcopyÚ_original_itemsÚ_merge)r   ÚitemsÚitems      r   r   ÚDendrogram.__init__¸   sA   € ñ
 :?Ó?º°” tÖ,¹Ñ?ˆŒÜ#Ÿyšy¨¯©Ó5ˆÔØˆ�ùò @s   …Ac                 óè   ^ • [        U5      S:¼  d   e[        T R                  /U 4S jU 5       Q76 nT =R                  S-  sl        UT R                  US   '   USS  H  nT R                  U	 M     g)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   óB   >#   • U  H  nTR                   U   v •  M     g 7fr7   )rz   )Ú.0Úir   s     €r   Ú	<genexpr>Ú#Dendrogram.merge.<locals>.<genexpr>Ë   s   øé € Ð-NÂgÀ¨d¯k©k¸!®nÂgùs   ƒrV   r   N)r   rY   r}   rz   )r   Úindicesro   r…   s   `   r   ÚmergeÚDendrogram.mergeÁ   sk   ø€ ô �7‹|˜qÓ Ð Ð Ü˜tŸ{™{ÐOÔ-NÁgÓ-NÒOˆØ�Š�qÑ�Ø"&ˆ�‰�G˜A‘JÑØ˜˜“ˆAØ—‘˜A’ò r   c                 ó¶   • [        U R                  5      S:”  a!  [        U R                  /U R                  Q76 nOU R                  S   nUR	                  U5      $ )zr
Finds the n-groups of items (leaves) reachable from a cut at depth n.
:param  n: number of groups
:type   n: int
rV   r   )r   rz   rY   r}   rp   )r   rl   Úroots      r   rp   ÚDendrogram.groupsÑ   sG   € ô ˆt�{‰{Ó˜aÓÜ" 4§;¡;Ð=°·±Ò=‰Dà—;‘;˜q‘>ˆDØ�{‰{˜1‹~Ðr   c                 óX  ^^^• Su  p#n[        U R                  5      S:”  a!  [        U R                  /U R                  Q76 nOU R                  S   nU R                  nU(       a  UnOU Vs/ s H  nSUR
                  -  PM     nn[        [        [         U5      5      S-   mTS-  m[        TT-
  S-
  5      mSUU4S jjn	S n
UR
                  U4/nU Vs/ s H
  o‰" S5      PM     nnU(       Ga…  UR                  5       u  pÞ[        [        S	 UR                  5      5      n[        [        UR                  U5      5      nU(       a  [        U5      n[        U5      n[        [        U5      5       H�  nUU   U;   aI  UW:X  a  U
" U	" USU5      5        O%UW:X  a  U
" U	" X#S5      5        OU
" U	" X#U5      5        U	" U5      UU'   MU  WUs=::  a  W::  a  O  OU
" U	" X3U5      5        Mv  U
" UU   5        Mƒ     U
" S
5        UR                   H3  nUR                  (       d  M  UR                  UR
                  U45        M5     UR!                  5         U H  nU
" U5        M     U
" S
5        U(       a  GM…  U
" SR#                  U4S jU 5       5      5        U
" S
5        gs  snf s  snf )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
)Ú+Ú-Ú|rV   r   z%sr‚   Ú c                 ó    >• TU-   U  UT-   3$ r7   r3   )ÚcentreÚleftÚrightÚlhalfÚrhalfs      €€r   ÚformatÚDendrogram.show.<locals>.formatû   s   ø€ Ø˜d‘l�^ F 8¨E°E©M¨?Ð;Ð;r   c                 ó0   • [         R                  " U 5        g r7   )r   Úwrite)Ústrs    r   ÚdisplayÚ Dendrogram.show.<locals>.displayþ   s   € Ü�LŠL˜Õr   c                 ó*   • U R                  S5      S   $ )NFr   )rc   )Úcs    r   Ú<lambda>Ú!Dendrogram.show.<locals>.<lambda>  s   € °·±¸%³ÀÒ1Cr   Ú
Ú c              3   óD   >#   • U  H  oR                  T5      v •  M     g 7fr7   )Úcenter)r„   r   Úwidths     €r   r†   Ú"Dendrogram.show.<locals>.<genexpr>#  s   øé € Ð@²x¨tŸ™ E×*Ð*²xùs   ƒ N)r’   r’   )r   rz   rY   r}   r|   r\   Úmaxr   Úintrh   r   r]   ÚindexÚminÚrangerj   rk   Újoin)r   Úleaf_labelsÚJOINÚHLINKÚVLINKrŒ   rc   Úlast_rowÚleafr™   rž   rm   Ú	verticalsrn   ro   Úchild_left_leafrˆ   Úmin_idxÚmax_idxr…   re   Úverticalr—   r˜   r¨   s                         @@@r   ÚshowÚDendrogram.showÝ   sP  ú€ ð +Ñˆ�Uô ˆt�{‰{Ó˜aÓÜ" 4§;¡;Ð=°·±Ò=‰Dà—;‘;˜q‘>ˆDØ×%Ñ%ˆæØ"‰Há7=Ó>²v¨t˜˜tŸ{™{Ô*±vˆHÐ>ô ”Cœ˜XÓ&Ó'¨!Ñ+ˆØ˜‘
ˆÜ�E˜E‘M AÑ%Ó&ˆ÷	<ð 	<ò	ð —+‘+˜tÐ$Ð%ˆÙ-3Ó4ªV T�V˜C–[©Vˆ	Ð4ßØ"ŸY™Y›[‰NˆHÜ"¤3Ñ'CÀTÇ^Á^Ó#TÓUˆOÜœ3˜vŸ|™|¨_Ó=Ó>ˆGÞÜ˜g›,�Ü˜g›,�Üœ3˜v›;Ö'�Ø˜!‘9 Ó/Ø˜G“|Ù¡ t¨S°%Ó 8Õ9Ø˜g›Ù¡ t°CÓ 8Õ9á¡ t°EÓ :Ô;Ù#)¨%£=�I˜a“LØ Õ, WÖ,Ù™F 5°Ó7Ö8á˜I a™LÖ)ñ (ñ �DŒMØŸœ�Ø—?—?‘?Ø—L‘L %§,¡,°Ð!6Ö7ñ (ð �J‰JŒLã%�Ù˜Ö!ñ &á�DŒM÷9 ‰eñ> 	�—‘Ô@±xÓ@Ó@ÔAÙ��ùòc ?ùò  5s   Á,J"ÃJ'c                 óÒ   • [        U R                  5      S:”  a!  [        U R                  /U R                  Q76 nOU R                  S   nUR	                  S5      nS[        U5      -  $ )NrV   r   Fz<Dendrogram with %d leaves>)r   rz   rY   r}   rc   )r   rŒ   rc   s      r   Ú__repr__ÚDendrogram.__repr__&  sT   € Üˆt�{‰{Ó˜aÓÜ" 4§;¡;Ð=°·±Ò=‰Dà—;‘;˜q‘>ˆDØ—‘˜UÓ#ˆØ,¬s°6«{Ñ:Ð:r   )rz   r}   r|   N)rK   rL   rM   rN   rO   r   r‰   rp   r»   r¾   rP   r3   r   r   rw   rw   °   s,   † ñð  ô òò 
ð  "ô GõR;r   rw   )r{   Úabcr   Úmathr   Úsysr   r   ÚImportErrorÚnltk.cluster.apir   r   rT   rW   rY   rw   r3   r   r   Ú<module>rÅ      sh   ðó Ý Ý Ý ð	Ûõ &ô\8˜8ô \8ò~'òS÷(Cñ (C÷V|;ò |;øðC ó 	Ùð	ús   ˜A ÁAÁA