
    Wi                         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z  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_over_time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   j'                  ddg      }t)        j*                         }t        |j                  j-                               D ]  \  }}|j                  |j                  |k(  ddf   }|j.                  j0                  d   }|j2                  j0                  }|r3t5        |j6                  j0                  j9                  dd            d   }n|j6                  }|j;                  t)        j<                  |j>                  |d|	|dz     d||D cg c]
  }d| d|  c}              |jA                  d       |jC                  d       |jE                  |rdnd| ddddtG        dd      d d!||tG        d"d#d$%      tG        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 over time.

    Arguments:
        topic_model: A fitted BERTopic instance.
        topics_over_time: 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 over time, simply run:

    ```python
    topics_over_time = topic_model.topics_over_time(docs, timestamps)
    topic_model.visualize_topics_over_time(topics_over_time)
    ```

    Or if you want to save the resulting figure:

    ```python
    fig = topic_model.visualize_topics_over_time(topics_over_time)
    fig.write_html("path/to/file.html")
    ```
    <iframe src="../../getting_started/visualization/trump.html"
    style="width:1000px; height: 680px; border: 0px;""></iframe>
    )z#E69F00z#56B4E9z#009E73z#F0E442z#D55E00z#0072B2z#CC79A7N_   r         z...(   NameTopic	Timestamp   lines   textz	<b>Topic z</b><br>Words: )xymodemarker_color	hoverinfoname	hovertextT)showgridzNormalized Frequency	Frequencygffffff?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   )yaxis_titler   templater   r   
hoverlabellegend)$get_topic_freqlocr   listsortedto_list
isinstancestrtopic_aspects_joinlen	enumeratetopic_labels_keyscustom_labels_items	_outliersmapisinsort_valuesgoFigureuniquer   valuesWordsr   r%   reshape	add_traceScatterr   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fig
trace_data
topic_namewordsr   words                              o/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/bertopic/plotting/_topics_over_time.pyvisualize_topics_over_timerk      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P\\^egr]stD ))+C!$**"3"3"56 
uXXdjjE1145
__++A.
  ''*..55==aDEaHA$$AJJ&&#EAI. PUVYug_TFCV
	

* d#d#.A*{gb0
  2:N2
  $ Ju t B^am

0 Ws<   #(OO')O"5	O'O-O2'O8#O>P"O')NNFFz<b>Topics over Time</b>i  i  )pandaspdtypingr   r   plotly.graph_objectsgraph_objectsrM   sklearn.preprocessingr   	DataFrameintboolr@   rN   rk        rj   <module>rw      s      ! +  $# %&+*ll * I	
  s#    YYrv   