
    Wi                     "   d dl Zd dlZ	 d dlmZ dZd dl	m
Z
mZ d dlmZ d dlmZ d dlmZ d dlmZ 	 	 	 	 	 	 	 dde
e   dz  d	edz  d
edeeef   dedededej2                  fdZdej6                  de
e   dededef
dZy# eef$ r dZY {w xY w)    N)UMAPTF)ListUnion)MinMaxScaler)select_topic_representationtopicstop_n_topics
use_ctfidfcustom_labelstitlewidthheightreturnc                 J   | j                         }|j                  |j                  dk7  ddf   }|t        |      }nL|'t	        |j                  j                         d|       }n#t	        |j                  j                               }t	        |      }	|	D 
cg c]  }
| j                  |
    }}
t        |t              r|	D 
cg c]#  }
t        |
      dgg| j                  |   |
   z   % }}
|D cg c]'  }dj                  |dd D cg c]  }|d   	 c}      ) }}}|D cg c]  }t        |      dk  r|n|dd dz    }}n{|r6| j                  *|	D 
cg c]  }
| j                  |
| j                  z        }}
nC|	D 
cg c]6  }
d	j                  | j                  |
      dd
 D cg c]  }|d   	 c}      8 }}
}t	        t        | j                         j!                                     }t#        j$                  |D 
cg c]  }
|j'                  |
       c}
      }t)        | j*                  | j,                  |d      \  }}||   }t.        r[|r9t1               j3                  |      }t5        ddddd      j3                  |      }n+t5        ddddd      j3                  |      }nt7        d      t9        j:                  |dddf   |dddf   |	||d      }t=        ||	|||      S c c}
w c c}
w c c}w c c}}w c c}w c c}
w c c}w c c}}
w c c}
w )a  Visualize topics, their sizes, and their corresponding words.

    This visualization is highly inspired by LDAvis, a great visualization
    technique typically reserved for LDA.

    Arguments:
        topic_model: A fitted BERTopic instance.
        topics: A selection of topics to visualize
        top_n_topics: Only select the top n most frequent topics
        use_ctfidf: Whether to use c-TF-IDF representations instead of the embeddings from the embedding model.
        custom_labels: If bool, whether to use custom topic labels that were defined using
                       `topic_model.set_topic_labels`.
                       If `str`, it uses labels from other aspects, e.g., "Aspect1".
        title: Title of the plot.
        width: The width of the figure.
        height: The height of the figure.

    Examples:
    To visualize the topics simply run:

    ```python
    topic_model.visualize_topics()
    ```

    Or if you want to save the resulting figure:

    ```python
    fig = topic_model.visualize_topics()
    fig.write_html("path/to/file.html")
    ```
    <iframe src="../../getting_started/visualization/viz.html"
    style="width:1000px; height: 680px; border: 0px;""></iframe>
    N_   r         z...z |    T)r
   output_ndarray   	hellingerrandom*   )n_neighborsn_componentsmetricinitrandom_statecosinez]UMAP is required to reduce the embeddings.. Please install it using `pip install umap-learn`.   )xyTopicWordsSize)get_topic_freqlocr%   listsortedto_listtopic_sizes_
isinstancestrtopic_aspects_joinlencustom_labels_	_outliers	get_topic
get_topicskeysnparrayindexr   	c_tf_idf_topic_embeddings_HAS_UMAPr   fit_transformr   ModuleNotFoundErrorpd	DataFrame_plotly_topic_visualization)topic_modelr   r	   r
   r   r   r   r   freq_df
topic_listtopicfrequencieswordslabelslabelword
all_topicsindices
embeddingsc_tfidf_useddfs                        e/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/bertopic/plotting/_topics.pyvisualize_topicsrR      s   X ((*Gkk'--2-q01Gf		!--/>?--/0 J@JKu;++E2KKK-%fpq]b3u:t$%(B(B=(QRW(XXqqLQR&<58<=RROTUe#e*r/uSbzE/AAUU	;55AXbcu++EK4I4I,IJccaklX]1F1Fu1Mbq1QRT!WRSll [335::<=>JhhVDE
((/DEG:%%	 J G$J%55jAJAk_amJ'  AhX\^mJ'  "k
 	

 
AqD!AqD!	

B 'r:ueVLL_ Lq<RUcRl EsN   K6(K;5LL 	L(L#L&L-L9	LL  LLrP   rE   c                    fd}| j                   j                         t        | j                   j                         dz        z
  | j                   j                         t        | j                   j                         dz        z   f}| j                  j                         t        | j                  j                         dz        z
  | j                  j                         t        | j                  j                         dz        z   f}t        j                  | dddddddd	d
d
d
ddd      }|j                  t        dt        dd                   |j                  dj                  g d             D 	cg c]  }	t        d|	 d ||	             }
}	t        dddi|
      g}|j                  | ddd d!t        d"d#$      d%||t        d&d'd()      d*did*di|+       |j                  |,       |j                  |,       |j                  d-t        |      dz  |d   t        |      dz  |d.   t        d/d0      1       |j                  d-|d   t        |      dz  |d.   t        |      dz  t        d2d0      1       |j                  |d   t        |      dz  d3dd45       |j                  |d.   t        |      dz  d6dd47       |j                   d8d8d9   |_        |S c c}	w ):zNCreate plotly-based visualization of topics with a slider for topic selection.c                 z    | dk(  rD cg c]  }d }}nD cg c]  }|| k(  rdnd }}d|gigS c c}w c c}w )Nr   #B0BEC5redzmarker.color )topic_selectedr   marker_colorrF   rE   s       rQ   	get_colorz._plotly_topic_visualization.<locals>.get_color~   sV    R/9:!I:L:YcdPUUn%<E)KdLd,011 ;ds   	38g333333?r#   r$   r'   (   simple_white )r#   r$   TF)r%   r&   r'   r#   r$   )r#   r$   sizesize_maxtemplaterI   
hover_datarU   r   DarkSlateGrey)r   color)rc   line)markerz<br>)z<b>Topic %{customdata[0]}</b>z%{customdata[1]}zSize: %{customdata[2]})hovertemplatezTopic update)rJ   methodargsr   t2   )activepadstepsgffffff?g      ?centertop   Black)r^   rc   )textr$   r#   xanchoryanchorfontwhite   Rockwell)bgcolor	font_sizefont_familyvisible)r   r   r   
hoverlabelxaxisyaxissliders)rangerd   r"   z#CFD8DC)rc   r   )typex0y0x1y1rd   z#9E9E9ED1
   )r#   r$   rs   	showarrowyshiftD2)r$   r#   rs   r   xshiftNr   )r#   minabsmaxr$   pxscatterupdate_tracesdictr1   update_layoutupdate_xaxesupdate_yaxes	add_shapesumadd_annotationdata)rP   rE   r   r   r   rZ   x_rangey_rangefigrF   rn   r   s    `          rQ   rB   rB   {   s   2 	
S"$$((*,--

S"$$((*,--G
 	
S"$$((*,--

S"$$((*,--G **


b!!D$UQVW	C T	1O8\]^ kk
   `jjV[T&(	%@PQjEj13)59:G gb0
 2:N% %   $ 7#7# MMw<!1:w<!1:	+   MM1:w<!1:w<!	+   s7|a'7de\^_s7|a'7de\^_xx"~CHJ] ks    K3)NNFFz<b>Intertopic Distance Map</b>  r   )numpyr8   pandasr@   umapr   r=   ImportErrorr?   typingr   r   sklearn.preprocessingr   bertopic._utilsr   plotly.expressexpressr   plotly.graph_objectsgraph_objectsgointboolr/   FigurerR   rA   rB   rW       rQ   <module>r      s    H  . 7  !
  $#&+1fMIfM *fM 	fM
 s#fM fM fM fM YYfMR[BLL [d3i [PS [\_ [il [g 	() Hs   B 	BB