
    Wi                         d dl Zd dlmZmZ d dlmZ 	 	 	 	 	 	 d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topics	log_scalecustom_labelstitlewidthheightreturnc                    |g n|}| j                         j                  j                         j                         }|D cg c]  }| j	                  |       }	}t        j                  |	D 
cg c]  }
|
D cg c]  }|d   	 c} c}}
      }
t        j                  |	D 
cg c]'  }
t        t        |
            D cg c]  }|dz   	 c}) c}}
      }g }t        ||
      D ]T  \  }}}t        |dkD        rt        |t              r:| ddj                  t        t        | j                  |   |          dd       z   }nh| j                   |r| j                   || j"                  z      }n=d| ddj                  | j	                  |      D cg c]  }|d   	 c}      z   }|dd	 }||v rd
nd}||v rdnd}t        |dk(        rt%        |
|
dkD           ||dk(  <   |rt        j&                  |||dkD        n|}t)        j*                  ||d|d|t-        |d            }|j/                  |       W t)        j0                  |      }|j3                  dt        |d         gdd       |j5                  dd| ddddt-        dd      d||t-        dd d!"      #       |j3                  d$%       |r|j7                  d&%       |S |j7                  d'%       |S c c}w c c}w c c}}
w c c}w c c}}
w c c}w )(al  Visualize the ranks of all terms across all topics.

    Each topic is represented by a set of words. These words, however,
    do not all equally represent the topic. This visualization shows
    how many words are needed to represent a topic and at which point
    the beneficial effect of adding words starts to decline.

    Arguments:
        topic_model: A fitted BERTopic instance.
        topics: A selection of topics to visualize. These will be colored
                red where all others will be colored black.
        log_scale: Whether to represent the ranking on a log scale
        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:
        fig: A plotly figure

    Examples:
    To visualize the ranks of all words across
    all topics simply run:

    ```python
    topic_model.visualize_term_rank()
    ```

    Or if you want to save the resulting figure:

    ```python
    fig = topic_model.visualize_term_rank()
    fig.write_html("path/to/file.html")
    ```

    <iframe src="../../getting_started/visualization/term_rank.html"
    style="width:1000px; height: 530px; border: 0px;""></iframe>

    <iframe src="../../getting_started/visualization/term_rank_log.html"
    style="width:1000px; height: 530px; border: 0px;""></iframe>

    Reference:

    This visualization was heavily inspired by the
    "Term Probability Decline" visualization found in an
    analysis by the amazing [tmtoolkit](https://tmtoolkit.readthedocs.io/).
    Reference to that specific analysis can be found
    [here](https://wzbsocialsciencecenter.github.io/tm_corona/tm_analysis.html).
    N   g      ?_   z	<b>Topic z</b>:r   2   redblackg?)outwhere zlines+lines)colorr	   )xyname	hovertextmodeopacityline)data   )rangetick0dtickFplotly_whiteg?g      ?centertop   Black)sizer   )textr   r   xanchoryanchorfontwhite   Rockwell)bgcolor	font_sizefont_family)
showlegendtemplater   r	   r
   
hoverlabelz	Term Rank)
title_textzc-TF-IDF score (log scale)zc-TF-IDF score)get_topic_infoTopicuniquetolist	get_topicnparrayr    lenzipany
isinstancestrjoinnexttopic_aspects_custom_labels_	_outliersminlog10goScatterdictappendFigureupdate_xaxesupdate_layoutupdate_yaxes)topic_modelr   r   r   r   r	   r
   	topic_idstopictopic_wordsvaluesvalueindiceslinesr   r   labelwordr   r   r   figs                         h/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/bertopic/plotting/_term_rank.pyvisualize_term_rankr^      s   x >RvF**,2299;BBDI=FGE;((/GKGXXKP&f5Ua5PQFhhR]^c&k0BCuC^_G E9gv6 q!1s7|-- 'chhtC9S9STa9bch9i4j/klnmn/o&pp++7M#225;;P;P3PQ#E7%0388Q\QfQfglQm<nT!W<n3oocr
 #f_E'E F?aG16{vz 23!q&	3<Q/!A::"S1D LL7: ))
C As71:/qBgb0
 2:N    ,$@A J 	$45J} H5PC^ =os<   K*/	K4
8K/K4
%K?
 K:K?
L/K4
:K?
)NFFz#<b>Term score decline per Topic</b>i   i  )numpyr<   typingr   r   plotly.graph_objectsgraph_objectsrJ   intboolrB   rN   r^        r]   <module>rg      s      !
  $&+6}I} } s#	}
 } } } YY}rf   