
    Wi                     ^    d dl mZ d dlmZ d dlmZ d dlmZ d dlZ	d dl
mZ  G d de      Zy)    )List)TfidfTransformer)	normalize)check_arrayNc            	            e Zd ZdZ	 	 	 	 ddededee   dz  def fdZdde	j                  d	ej                  fd
Zde	j                  fdZ xZS )ClassTfidfTransformera'  A Class-based TF-IDF procedure using scikit-learns TfidfTransformer as a base.

    ![](../algorithm/c-TF-IDF.svg)

    c-TF-IDF can best be explained as a TF-IDF formula adopted for multiple classes
    by joining all documents per class. Thus, each class is converted to a single document
    instead of set of documents. The frequency of each word **x** is extracted
    for each class **c** and is **l1** normalized. This constitutes the term frequency.

    Then, the term frequency is multiplied with IDF which is the logarithm of 1 plus
    the average number of words per class **A** divided by the frequency of word **x**
    across all classes.

    Arguments:
        bm25_weighting: Uses BM25-inspired idf-weighting procedure instead of the procedure
                        as defined in the c-TF-IDF formula. It uses the following weighting scheme:
                        `log(1+((avg_nr_samples - df + 0.5) / (df+0.5)))`
        reduce_frequent_words: Takes the square root of the bag-of-words after normalizing the matrix.
                               Helps to reduce the impact of words that appear too frequently.
        seed_words: Specific words that will have their idf value increased by
                    the value of `seed_multiplier`.
                    NOTE: This will only increase the value of words that have an exact match.
        seed_multiplier: The value with which the idf values of the words in `seed_words`
                         are multiplied.

    Examples:
    ```python
    transformer = ClassTfidfTransformer()
    ```
    Nbm25_weightingreduce_frequent_words
seed_wordsseed_multiplierc                 b    || _         || _        || _        || _        t        t
        |           y N)r	   r
   r   r   superr   __init__)selfr	   r
   r   r   	__class__s        h/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/bertopic/vectorizers/_ctfidf.pyr   zClassTfidfTransformer.__init__)   s2     -%:"$.#T35    X
multiplierc                 t   t        |d      }t        j                  |      st        j                  |      }t        j
                  }| j                  r|j                  \  }}t	        j                  t	        j                  |j                  d                  }t        |j                  d      j                               }| j                  r%t	        j                  d||z
  dz   |dz   z  z         }nt	        j                  ||z  dz         }|||z  }t        j                  |d||fd|      | _        | S )	zLearn the idf vector (global term weights).

        Arguments:
            X: A matrix of term/token counts.
            multiplier: A multiplier for increasing/decreasing certain IDF scores
        )csrcsc)accept_sparser   )axis   g      ?r   )offsetsshapeformatdtype)r   spissparse
csr_matrixnpfloat64use_idfr   squeezeasarraysumintmeanr	   logdiags	_idf_diag)	r   r   r   r    _
n_featuresdfavg_nr_samplesidfs	            r   fitzClassTfidfTransformer.fit6   s    8{{1~a A

<<GGMAz BJJquu!u}56B !A!3!3!56N ""ffQ>B#6#<c"JKL
 ffnr1Q67 %J&XX!:.DN r   c                     | j                   rNt        |ddd      }| j                  r$t        j                  |j
                        |_        || j                  z  }|S )zTransform a count-based matrix to c-TF-IDF.

        Arguments:
            X (sparse matrix): A matrix of term/token counts.

        Returns:
            X (sparse matrix): A c-TF-IDF matrix
        r   l1F)r   normcopy)r&   r   r
   r$   sqrtdatar.   )r   r   s     r   	transformzClassTfidfTransformer.transformb   sJ     <<!!$U;A))DNN"Ar   )FFN   r   )__name__
__module____qualname____doc__boolr   strfloatr   r!   r#   r$   ndarrayr4   r;   __classcell__)r   s   @r   r   r   	   sw    B  %&+'+!"66  $6 I$	6
 6*R]] *

 *X2== r   r   )typingr   sklearn.feature_extraction.textr   sklearn.preprocessingr   sklearn.utilsr   numpyr$   scipy.sparsesparser!   r    r   r   <module>rN      s&     < + %  j, jr   