
    Wi                         d dl Z d dlZd dlmZ deedf   deeef   dedefdZdedef   fdZ		 	 	 	 	 dd	e
d
e
dedededz  f
dZy)    N)Union
doc_length	tokenizerdocumentreturnc                 h   ||dk(  r|d| }|S |dk(  r$dj                  |j                         d|       }|S |dk(  r6| j                  j                         }dj                   ||      d|       }|S t	        |d      r1t	        |d      r%|j                  |      }|j                  |d|       }S |S )ah  Truncate a document to a certain length.

    If you want to add a custom tokenizer, then it will need to have a `decode` and
    `encode` method. An example would be the following custom tokenizer:

    ```python
    class Tokenizer:
        'A custom tokenizer that splits on commas'
        def encode(self, doc):
            return doc.split(",")

        def decode(self, doc_chunks):
            return ",".join(doc_chunks)
    ```

    You can use this tokenizer by passing it to the `tokenizer` parameter.

    Arguments:
        topic_model: A BERTopic model
        doc_length: The maximum length of each document. If a document is longer,
                    it will be truncated. If None, the entire document is passed.
        tokenizer: The tokenizer used to calculate to split the document into segments
                   used to count the length of a document.
                       * If tokenizer is 'char', then the document is split up
                         into characters which are counted to adhere to `doc_length`
                       * If tokenizer is 'whitespace', the document is split up
                         into words separated by whitespaces. These words are counted
                         and truncated depending on `doc_length`
                       * If tokenizer is 'vectorizer', then the internal CountVectorizer
                         is used to tokenize the document. These tokens are counted
                         and truncated depending on `doc_length`. They are decoded with
                         whitespaces.
                       * If tokenizer is a callable, then that callable is used to tokenize
                         the document. These tokens are counted and truncated depending
                         on `doc_length`
        document: A single document

    Returns:
        truncated_document: A truncated document
    Nchar
whitespace 
vectorizerencodedecode)joinsplitvectorizer_modelbuild_tokenizerhasattrr   r   )topic_modelr   r   r   truncated_documentencoded_documents         j/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/bertopic/representation/_utils.pytruncate_documentr      s    R !)+:!6 "! ,&!$(..*:;J*G!H "! ,&#44DDFI!$)H*=kz*J!K "! Y)gi.J(//9!*!1!12B;J2O!P!!O    c                 B    | |t        d      | |t        d      yy)zGValidates parameters that are used in the function `truncate_document`.NzPlease select from one of the valid options for the `tokenizer` parameter: 
{'char', 'whitespace', 'vectorizer'} 
If `tokenizer` is of type callable ensure it has methods to encode and decode a document 
zNIf `tokenizer` is provided, `doc_length` of type int must be provided as well.)
ValueError)r   r   s     r   %validate_truncate_document_parametersr   >   sA    Z3j
 	

 
	:#5ijj $6	r   initial_delayexponential_basejittermax_retrieserrorsc                 &      fd}|S )z*Retry a function with exponential backoff.c                      d}}	 	  | i |S # $ rQ |dz  }|
kD  rt        d
 d      |d	t        j                         z  z   z  z  }t        j                  |       Y nt         $ r}|d }~ww xY wq)Nr      zMaximum number of retries (z) exceeded.)	Exceptionrandomtimesleep)argskwargsnum_retriesdelayer!   r   funcr   r   r    s        r   wrapperz/retry_with_exponential_backoff.<locals>.wrapperT   s     T,V,,  "q  ,#&A+k$Z[[ )Q&--/1I-IJJ 

5!  ) s    AA4%A4-A//A4 )r.   r   r   r   r    r!   r/   s   `````` r   retry_with_exponential_backoffr1   J   s     : Nr   )r$      T
   N)r&   r'   typingr   intstrcallabler   r   r   floatbooltupler1   r0   r   r   <module>r;      s      5uS$Y/? 5ERUW_R_L` 5lo 5tw 5p	kE$PZJZD[ 	k '' ' 	'
 ' DL'r   