
    Wi                         d dl Z d dlZd dlmZmZ d dlmZ d dl	m
Z
 	 	 	 	 	 	 	 	 ddee   dz  dededeeef   ded	ed
ededej                  fdZy)    N)ListUnion)make_subplotstopicstop_n_topicsn_wordscustom_labelstitlewidthheight	autoscalereturnc	                    t        j                  g d      }	| j                         }
|
j                  |
j                  dk7  ddf   }
|t        |      }nO|'t        |
j                  j                         d|       }n&t        |
j                  j                         dd       }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    }}nJ| j                  ,|r*|D cg c]  }| j                  || j                  z        }}n|D cg c]  }d| 	 }}d}t        t!        j"                  t        |      |z              }t%        ||dd|dkD  rd|z  nd|      }d}d}|D ]  }| j'                  |      D cg c]
  \  }}|dz    c}}d| ddd   }| j'                  |      D cg c]  \  }}|	 c}}d| ddd   }|j)                  t+        j,                  ||dt/        |	            ||       |r[t        |      dkD  rdt        |      dz
  dz  z   }t        |      dkD  r+|j1                  t3        |dz
  t        |      z               ||k(  rd}|dz  }|dz  } |j5                  dd| dddt3        d d!"      d#|dz  |dkD  r||z  n|d$z  t3        d%d&d'(      )       |j7                  d*+       |j1                  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 a barchart of selected topics.

    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.
        n_words: Number of words to show in a topic
        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 each figure.
        height: The height of each figure.
        autoscale: Whether to automatically calculate the height of the figures to fit the whole bar text

    Returns:
        fig: A plotly figure

    Examples:
    To visualize the barchart of selected topics
    simply run:

    ```python
    topic_model.visualize_barchart()
    ```

    Or if you want to save the resulting figure:

    ```python
    fig = topic_model.visualize_barchart()
    fig.write_html("path/to/file.html")
    ```
    <iframe src="../../getting_started/visualization/bar_chart.html"
    style="width:1100px; height: 660px; border: 0px;""></iframe>
    )z#D55E00z#0072B2z#CC79A7z#E69F00z#56B4E9z#009E73z#F0E442Nr      _         z...zTopic Fg?   g?)rowscolsshared_xaxeshorizontal_spacingvertical_spacingsubplot_titlesz  h)xyorientationmarker_color)rowcol         	      )size)tickfontplotly_whiteg      ?centertop   Black)r)   color)textr   xanchoryanchorfontg?white   Rockwell)bgcolor	font_sizefont_family)template
showlegendr
   r   r   
hoverlabelT)showgrid)	itertoolscycleget_topic_freqlocTopiclistsortedto_list
isinstancestrtopic_aspects_joinlencustom_labels_	_outliersintnpceilr   	get_topic	add_tracegoBarnextupdate_yaxesdictupdate_layoutupdate_xaxes)topic_modelr   r   r   r	   r
   r   r   r   colorsfreq_dftopicr   labelslabelcolumnsr   figr"   columnwordr   wordsscorescoress                            g/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/bertopic/plotting/_barchart.pyvisualize_barchartrh   	   s   \ __jkF ((*Gkk'--2-q01Gf		!--/>?--/!45 -%ouvfkCJ-.1K1KM1Z[`1aavvUcd6#((&!*#EE!H#EFddXfgu3u:?%cr
U8JJgg		#	#	/MaghX]+44U[=R=R5RShh8>?uF5'*??Grwws6{W,-.D
'+axtQ%C CF ,7,A,A%,HIqI(7STXVXTXY(3(=(=e(DEHAu%EhwOPTRTPTUFFVu#DLQ 	 	
 5zBE
R2555zA~  $Vc\c%j4P*Q RWF1HCaKF+0 gb0
 ai $qv}fsl2:N   d#d#J w#Edgh?  JEs<   9(M(M?M	MM#M9M ,M%M+M)N      Fz<b>Topic Word Scores</b>r%   r%   F)r?   numpyrO   typingr   r   plotly.graph_objectsgraph_objectsrS   plotly.subplotsr   rN   boolrH   Figurerh        rg   <module>rt      s       ! )
  $&++{I{ { 	{
 s#{ { { { { YY{rs   