
    WiV                         d dl Zd dlmZmZ d dlmZ d dlm	Z	 	 	 	 	 	 	 	 ddej                  dedee   dz  dedeeef   d	ed
ededej                  fdZy)    N)ListUnion)	normalizetopics_per_classtop_n_topicstopicsnormalize_frequencycustom_labelstitlewidthheightreturnc	                    g d}	| j                         }
|
j                  |
j                  dk7  ddf   }
|t        |      }nL|'t	        |
j                  j                         d|       }n#t	        |
j                  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    }}t        | j                  j                               D ci c]  \  }}|||    }}}n| j                  J|rH| j                  j                         D ci c]"  \  }}|| j                  || j                   z      $ }}}nC| j                  j                         D ci c]  \  }}|t        |      d
kD  r|dd
 d	z   n|  }}}|j                  j#                  |      |d<   |j                  |j                  j%                  |      ddf   }t'        j(                         }t        |      D ]  \  }}|dk(  rd}nd}|j                  |j                  |k(  ddf   }|j*                  j,                  d   }|j.                  j,                  }|r3t1        |j2                  j,                  j5                  dd            d   }n|j2                  }|j7                  t'        j8                  |j:                  |||	|dz     d|d|D cg c]
  }d| d|  c}              |j=                  d       |j?                  d       |jA                  |rdndd| ddddtC        dd      d d!||tC        d"d#d$%      tC        d&'      (       |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 ))a  Visualize topics per class.

    Arguments:
        topic_model: A fitted BERTopic instance.
        topics_per_class: The topics you would like to be visualized with the
                          corresponding topic representation
        top_n_topics: To visualize the most frequent topics instead of all
        topics: Select which topics you would like to be visualized
        normalize_frequency: Whether to normalize each topic's frequency individually
        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.

    Returns:
        A plotly.graph_objects.Figure including all traces

    Examples:
    To visualize the topics per class, simply run:

    ```python
    topics_per_class = topic_model.topics_per_class(docs, classes)
    topic_model.visualize_topics_per_class(topics_per_class)
    ```

    Or if you want to save the resulting figure:

    ```python
    fig = topic_model.visualize_topics_per_class(topics_per_class)
    fig.write_html("path/to/file.html")
    ```
    <iframe src="../../getting_started/visualization/topics_per_class.html"
    style="width:1400px; height: 1000px; border: 0px;""></iframe>
    )z#E69F00z#56B4E9z#009E73z#F0E442z#D55E00z#0072B2z#CC79A7N_   r         z...(   NameT
legendonly      texthz	<b>Topic z</b><br>Words: )yxvisiblemarker_color	hoverinfonameorientation	hovertext)showgridzNormalized Frequency	FrequencyClassgffffff?g?centertop   Black)sizecolor)r   r   r   xanchoryanchorfontsimple_whitewhite   Rockwell)bgcolor	font_sizefont_familyz<b>Global Topic Representation)r   )xaxis_titleyaxis_titler   templater   r   
hoverlabellegend)"get_topic_freqlocTopiclistsortedto_list
isinstancestrtopic_aspects_joinlen	enumeratetopic_labels_keyscustom_labels_items	_outliersmapisingoFigurer   valuesWordsr   r%   reshape	add_traceBarr&   update_xaxesupdate_yaxesupdate_layoutdict)topic_modelr   r   r   r	   r
   r   r   r   colorsfreq_dfselected_topicstopictopic_nameslabelslabelindexkeyr   valuedatafigr   
trace_data
topic_namewordsr   words                               o/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/bertopic/plotting/_topics_per_class.pyvisualize_topics_per_classrl      s   ^F ((*Gkk'--2-q01Gv,		! !6!6!8,!GH !6!6!89 -%lrschUT*+k.H.H.WX].^^ssR]^sxxvbqz Beq BC^^U`aEE
RuU3BZ%5GGaaAJ;KdKdKiKiKkAlm:5#sK..mm		#	#	/MWbWpWpWvWvWx
MSSRSC++C+2G2G,GHH
 

 U`TmTmTsTsTu
FPc5Cs5zBse#EA
 
  05599+FV 0 6 6 ; ;O La OPD ))+C!/2 
uA:G"GXXdjjE1145
__++A.
  ''*..55==aDEaHA$$AFF""#EAI. PUVYug_TFCV		

4 d#d#.A*{gb0
  2:N2
  & JA t B^am

: Ws<   #(O O
)O5	O
OO'O#O!7O'O
)
   NFFz<b>Topics per Class</b>i  i  )pandaspdtypingr   r   plotly.graph_objectsgraph_objectsrO   sklearn.preprocessingr   	DataFrameintboolrC   rP   rl        rk   <module>ry      s      ! + # %&+*EllE E I	E
 E s#E E E E YYErx   