
    Wi                     p    d dl Zd dlZ	 d dlmZ dZ	 d	de	dej                  defdZy# eef$ r dZY "w xY w)
    N)StylerTFdocumenttopic_token_distribution	normalizec                 l  
 | j                   j                         } ||      }t        |      dk(  rt        d      |r1t	        j
                  ||j                         z        j                  }nt	        j
                  |      j                  }t        |      D cg c]  \  }}| d|  c}}|_	        t        |      D cg c]  \  }}| d|z    c}}|_	        t        | j                  j                               | j                  d |_        |j                  |j                  d      dk7  ddf   }d 
dd	}	t        |      dk(  r|S t         rO|j"                  j%                  d
      j'                  dd      j)                  
fd      j+                  |	d      }|S c c}}w c c}}w )a  Visualize the topic distribution calculated by `.approximate_topic_distribution`
    on a token level. Thereby indicating the extend to which a certain word or phrases belong
    to a specific topic. The assumption here is that a single word can belong to multiple
    similar topics and as such give information about the broader set of topics within
    a single document.

    Note:
    This function will return a stylized pandas dataframe if Jinja2 is installed. If not,
    it will only return a pandas dataframe without color highlighting. To install jinja:

    `pip install jinja2`

    Arguments:
        topic_model: A fitted BERTopic instance.
        document: The document for which you want to visualize
                  the approximated topic distribution.
        topic_token_distribution: The topic-token distribution of the document as
                                  extracted by `.approximate_topic_distribution`
        normalize: Whether to normalize, between 0 and 1 (summing to 1), the
                   topic distribution values.

    Returns:
        df: A stylized dataframe indicating the best fitting topics
            for each token.

    Examples:
    ```python
    # Calculate the topic distributions on a token level
    # Note that we need to have `calculate_token_level=True`
    topic_distr, topic_token_distr = topic_model.approximate_distribution(
            docs, calculate_token_level=True
    )

    # Visualize the approximated topic distributions
    df = topic_model.visualize_approximate_distribution(docs[0], topic_token_distr[0])
    df
    ```

    To revert this stylized dataframe back to a regular dataframe,
    you can run the following:

    ```python
    df.data.columns = [column.strip() for column in df.data.columns]
    df = df.data
    ```
    r   z7Make sure that your document contains at least 1 token._ N   )axisc                     | dk(  rdnd}d|z  S )Nr   whiteblackz	color: %s )valcolors     w/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/bertopic/plotting/_approximate_distribution.py
text_colorz6visualize_approximate_distribution.<locals>.text_colorS   s    !8U""    c                     dj                  |      }t        j                  t        j                  | dk(  |d      | j
                  | j                        S )Nzbackground-color: {}r    )indexcolumns)formatpd	DataFramenpwherer   r   )datar   attrs      r   highligh_colorz:visualize_approximate_distribution.<locals>.highligh_colorW   sA    %,,U3||BHHTQYb9UYUaUabbr   z{:.3f}Blues)cmapr   c                      |       S )Nr   )xr   s    r   <lambda>z4visualize_approximate_distribution.<locals>.<lambda>a   s    
1 r   )r   )vectorizer_modelbuild_tokenizerlen
ValueErrorr   r   sumT	enumerater   listtopic_labels_values	_outliersr   loc	HAS_JINJAstyler   background_gradientapplymapapply)topic_modelr   r   r   analyzertokensdfitokenr    r   s             @r   "visualize_approximate_distributionr=      s   j ++;;=HhF
6{aRSS \\25M5Q5Q5SSTVV\\23551:61BCXQUG1QC.CBJ6?6GH(!UUGC!G9%HBJK--44678M8M8OPBH	Q1$q(	)B#c 2w!|		HHOOH%  gD 9X-.U>U-	 	 I/ DHs   F*F0)F)numpyr   pandasr   pandas.io.formats.styler   r2   ModuleNotFoundErrorImportErrorstrndarrayboolr=   r   r   r   <module>rF      sa     .I 	XX !jjX 	X	 	[) Is   ) 	55