
    6biX                         S r SSKrSSKrSSKrSSKrSSKrSSKJr  \" S5         SS j5       r	\" S5          SS j5       r
\" S5           SS	 j5       r\" S
5       " S S\5      5       r\" S5      S 5       rg)a  Utilities for text input preprocessing.

Deprecated: `tf.keras.preprocessing.text` APIs are not recommended for new code.
Prefer `tf.keras.utils.text_dataset_from_directory` and
`tf.keras.layers.TextVectorization` which provide a more efficient approach
for preprocessing text input. For an introduction to these APIs, see
the [text loading tutorial]
(https://www.tensorflow.org/tutorials/load_data/text)
and [preprocessing layer guide]
(https://www.tensorflow.org/guide/tf_keras/preprocessing_layers).
    N)keras_exportz.keras.preprocessing.text.text_to_word_sequencec                    U(       a  U R                  5       n U Vs0 s H  oDU_M     nn[        R                  U5      nU R                  U5      n U R	                  U5      nU Vs/ s H  o(       d  M  UPM     sn$ s  snf s  snf )al  Converts a text to a sequence of words (or tokens).

Deprecated: `tf.keras.preprocessing.text.text_to_word_sequence` does not
operate on tensors and is not recommended for new code. Prefer
`tf.strings.regex_replace` and `tf.strings.split` which provide equivalent
functionality and accept `tf.Tensor` input. For an overview of text handling
in Tensorflow, see the [text loading tutorial]
(https://www.tensorflow.org/tutorials/load_data/text).

This function transforms a string of text into a list of words
while ignoring `filters` which include punctuations by default.

>>> sample_text = 'This is a sample sentence.'
>>> tf.keras.preprocessing.text.text_to_word_sequence(sample_text)
['this', 'is', 'a', 'sample', 'sentence']

Args:
    input_text: Input text (string).
    filters: list (or concatenation) of characters to filter out, such as
        punctuation. Default: ``'!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\\t\\n'``,
          includes basic punctuation, tabs, and newlines.
    lower: boolean. Whether to convert the input to lowercase.
    split: str. Separator for word splitting.

Returns:
    A list of words (or tokens).
)lowerstr	maketrans	translatesplit)	
input_textfiltersr   r	   ctranslate_dicttranslate_mapseqis	            Y/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/preprocessing/text.pytext_to_word_sequencer   '   s{    D %%'
(/01hN0MM.1M%%m4J


5
!C s!aAs   1
 !s   A<%
B3Bz keras.preprocessing.text.one_hotc           
      (    [        U U[        UUUUS9$ )a  One-hot encodes a text into a list of word indexes of size `n`.

Deprecated: `tf.keras.text.preprocessing.one_hot` does not operate on
tensors and is not recommended for new code. Prefer
`tf.keras.layers.Hashing` with `output_mode='one_hot'` which provides
equivalent functionality through a layer which accepts `tf.Tensor` input.
See the [preprocessing layer guide]
(https://www.tensorflow.org/guide/tf_keras/preprocessing_layers) for an
overview of preprocessing layers.

This function receives as input a string of text and returns a
list of encoded integers each corresponding to a word (or token)
in the given input string.

Args:
    input_text: Input text (string).
    n: int. Size of vocabulary.
    filters: list (or concatenation) of characters to filter out, such as
      punctuation. Default:
      ```
      '!"#$%&()*+,-./:;<=>?@[\]^_`{|}~\t\n
      ```,
      includes basic punctuation, tabs, and newlines.
    lower: boolean. Whether to set the text to lowercase.
    split: str. Separator for word splitting.
    analyzer: function. Custom analyzer to split the text

Returns:
    List of integers in `[1, n]`. Each integer encodes a word
    (unicity non-guaranteed).
)hash_functionr   r   r	   analyzer)hashing_trickhash)r
   nr   r   r	   r   s         r   one_hotr   T   s'    P 	     z&keras.preprocessing.text.hashing_trickc                     Uc  [         nO	US:X  a  S nUc  [        XXES9nOU" U 5      nU Vs/ s H  o" U5      US-
  -  S-   PM     sn$ s  snf )a  Converts a text to a sequence of indexes in a fixed-size hashing space.

Deprecated: `tf.keras.text.preprocessing.hashing_trick` does not operate on
tensors and is not recommended for new code. Prefer
`tf.keras.layers.Hashing` which provides equivalent functionality through a
layer which accepts `tf.Tensor` input. See the [preprocessing layer guide](
https://www.tensorflow.org/guide/tf_keras/preprocessing_layers) for an
overview of preprocessing layers.

Args:
    text: Input text (string).
    n: Dimension of the hashing space.
    hash_function: When `None` uses a python `hash` function. Can be 'md5'
        or any function that takes in input a string and returns a int.
        Note that 'hash' is not a stable hashing function, so
        it is not consistent across different runs, while 'md5'
        is a stable hashing function. Defaults to `None`.
    filters: list (or concatenation) of characters to filter out, such as
        punctuation. Default: ``!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\\t\\n``,
        includes basic punctuation, tabs, and newlines.
    lower: boolean. Whether to set the text to lowercase.
    split: str. Separator for word splitting.
    analyzer: function. Custom analyzer to split the text

Returns:
    A list of integer word indices (unicity non-guaranteed).
    `0` is a reserved index that won't be assigned to any word.
    Two or more words may be assigned to the same index, due to possible
    collisions by the hashing function.
    The [probability](
        https://en.wikipedia.org/wiki/Birthday_problem#Probability_table)
    of a collision is in relation to the dimension of the hashing space and
    the number of distinct objects.
md5c                 z    [        [        R                  " U R                  5       5      R	                  5       S5      $ )N   )inthashlibr   encode	hexdigest)ws    r   <lambda>hashing_trick.<locals>.<lambda>   s#    #gkk!((*&=&G&G&I2"Nr   r   r   r	      )r   r   )	textr   r   r   r   r	   r   r   r#   s	            r   r   r      sh    X 	%	N#
 tn69:c]1Q'!+c:::s   Az"keras.preprocessing.text.Tokenizerc                   t    \ rS rSrSr       SS jrS rS rS rS r	S	 r
S
 rSS jrSS jrS rS rSrg)	Tokenizer   a  Text tokenization utility class.

Deprecated: `tf.keras.preprocessing.text.Tokenizer` does not operate on
tensors and is not recommended for new code. Prefer
`tf.keras.layers.TextVectorization` which provides equivalent functionality
through a layer which accepts `tf.Tensor` input. See the
[text loading tutorial](https://www.tensorflow.org/tutorials/load_data/text)
for an overview of the layer and text handling in tensorflow.

This class allows to vectorize a text corpus, by turning each
text into either a sequence of integers (each integer being the index
of a token in a dictionary) or into a vector where the coefficient
for each token could be binary, based on word count, based on tf-idf...

By default, all punctuation is removed, turning the texts into
space-separated sequences of words
(words may include the `'` character). These sequences are then
split into lists of tokens. They will then be indexed or vectorized.

`0` is a reserved index that won't be assigned to any word.

Args:
    num_words: the maximum number of words to keep, based
        on word frequency. Only the most common `num_words-1` words will
        be kept.
    filters: a string where each element is a character that will be
        filtered from the texts. The default is all punctuation, plus
        tabs and line breaks, minus the `'` character.
    lower: boolean. Whether to convert the texts to lowercase.
    split: str. Separator for word splitting.
    char_level: if True, every character will be treated as a token.
    oov_token: if given, it will be added to word_index and used to
        replace out-of-vocabulary words during text_to_sequence calls
    analyzer: function. Custom analyzer to split the text.
        The default analyzer is text_to_word_sequence
Nc                    SU;   a'  [         R                  " S5        UR                  S5      nUR                  SS5      n	U(       a  [        S[	        U5      -   5      e[
        R                  " 5       U l        [
        R                  " [        5      U l
        X l        X@l        X0l        Xl        Xl        XPl        X`l        [
        R                  " [        5      U l        0 U l        0 U l        Xpl        g )Nnb_wordszDThe `nb_words` argument in `Tokenizer` has been renamed `num_words`.document_countr   z Unrecognized keyword arguments: )warningswarnpop	TypeErrorr   collectionsOrderedDictword_countsdefaultdictr   	word_docsr   r	   r   	num_wordsr.   
char_level	oov_token
index_docs
word_index
index_wordr   )
selfr8   r   r   r	   r9   r:   r   kwargsr.   s
             r   __init__Tokenizer.__init__   s     MM0 

:.I$4a8>VLMM&224$005

",$"%11#6 r   c                    U GHJ  nU =R                   S-  sl         U R                  (       d  [        U[        5      (       aY  U R                  (       aE  [        U[        5      (       a   U Vs/ s H  o3R	                  5       PM     nnOUR	                  5       nUnOIU R
                  c+  [        UU R                  U R                  U R                  S9nOU R                  U5      nU H:  nXPR                  ;   a  U R                  U==   S-  ss'   M+  SU R                  U'   M<     [        U5       H  nU R                  U==   S-  ss'   M     GMM     [        U R                  R                  5       5      nUR                  S SS9  U R                  c  / nOU R                  /nUR                  S U 5       5        [!        [#        U[        [%        S['        U5      S-   5      5      5      5      U l        U R(                  R                  5        VVs0 s H  u  pXX_M	     snnU l        [        U R                  R                  5       5       H   u  pXXR,                  U R(                  U   '   M"     gs  snf s  snnf )a\  Updates internal vocabulary based on a list of texts.

In the case where texts contains lists,
we assume each entry of the lists to be a token.

Required before using `texts_to_sequences` or `texts_to_matrix`.

Args:
    texts: can be a list of strings,
        a generator of strings (for memory-efficiency),
        or a list of list of strings.
r'   Nr&   c                     U S   $ )Nr'    )xs    r   r$   (Tokenizer.fit_on_texts.<locals>.<lambda>7  s    1Q4r   T)keyreversec              3   *   #    U  H	  oS    v   M     g7f)r   NrD   ).0wcs     r   	<genexpr>)Tokenizer.fit_on_texts.<locals>.<genexpr>=  s     2'BQ%'s   )r.   r9   
isinstancelistr   r   r   r   r	   r5   setr7   itemssortr:   extenddictziprangelenr<   r=   r;   )	r>   textsr(   	text_elemr   r#   wcounts
sorted_vocr   s	            r   fit_on_textsTokenizer.fit_on_texts  s     D1$*T4"8"8::!$--CGH4i 14H#zz|==(/ $"jj"jj	C ---C((($$Q'1,'*+D$$Q'	 
 Xq!Q&! 1 8 t''--/06>>!J..)J2'22 
Dq#j/A*=!>?@
 -1OO,A,A,CD,CDA14,CD--/0DA23OODOOA./ 1O  IJ Es   ,I0I5c                     U =R                   [        U5      -  sl         U H.  n[        U5      nU H  nU R                  U==   S-  ss'   M     M0     g)zUpdates internal vocabulary based on a list of sequences.

Required before using `sequences_to_matrix`
(if `fit_on_texts` was never called).

Args:
    sequences: A list of sequence.
        A "sequence" is a list of integer word indices.
r'   N)r.   rW   rP   r;   )r>   	sequencesr   r   s       r   fit_on_sequencesTokenizer.fit_on_sequencesI  sJ     	s9~-Cc(C"a'"  r   c                 6    [        U R                  U5      5      $ )a  Transforms each text in texts to a sequence of integers.

Only top `num_words-1` most frequent words will be taken into account.
Only words known by the tokenizer will be taken into account.

Args:
    texts: A list of texts (strings).

Returns:
    A list of sequences.
)rO   texts_to_sequences_generator)r>   rX   s     r   texts_to_sequencesTokenizer.texts_to_sequencesY  s     D55e<==r   c              #   &  #    U R                   nU R                  R                  U R                  5      nU GHP  nU R                  (       d  [        U[        5      (       aY  U R                  (       aE  [        U[        5      (       a   U Vs/ s H  oUR                  5       PM     nnOUR                  5       nUnOIU R                  c+  [        UU R                  U R                  U R                  S9nOU R                  U5      n/ nU Hx  nU R                  R                  U5      n	U	b7  U(       a  X:  a  Ub  UR                  U5        MC  ME  UR                  U	5        MX  U R                  c  Mg  UR                  U5        Mz     Uv   GMS     gs  snf 7f)a~  Transforms each text in `texts` to a sequence of integers.

Each item in texts can also be a list,
in which case we assume each item of that list to be a token.

Only top `num_words-1` most frequent words will be taken into account.
Only words known by the tokenizer will be taken into account.

Args:
    texts: A list of texts (strings).

Yields:
    Yields individual sequences.
Nr&   )r8   r<   getr:   r9   rN   rO   r   r   r   r   r	   append)
r>   rX   r8   oov_token_indexr(   rY   r   vectr#   r   s
             r   rc   &Tokenizer.texts_to_sequences_generatorg  s6     NN	//--dnn=D*T4"8"8::!$--CGH4i 14H#zz|==(/ $"jj"jj	C ---CDOO''*= Q^*6 KK8 7 A^^/KK0  J;   Is   BF
F#CF-$Fc                 6    [        U R                  U5      5      $ )a  Transforms each sequence into a list of text.

Only top `num_words-1` most frequent words will be taken into account.
Only words known by the tokenizer will be taken into account.

Args:
    sequences: A list of sequences (list of integers).

Returns:
    A list of texts (strings)
)rO   sequences_to_texts_generator)r>   r_   s     r   sequences_to_textsTokenizer.sequences_to_texts  s     D55i@AAr   c              #     #    U R                   nU R                  R                  U R                  5      nU H  n/ nU H  nU R                  R                  U5      nUbD  U(       a*  Xb:  a%  Ub   UR                  U R                  U   5        MP  MR  UR                  U5        Me  U R                  c  Mt  UR                  U R                  U   5        M     SR                  U5      nUv   M     g7f)a~  Transforms each sequence in `sequences` to a list of texts(strings).

Each sequence has to a list of integers.
In other words, sequences should be a list of sequences

Only top `num_words-1` most frequent words will be taken into account.
Only words known by the tokenizer will be taken into account.

Args:
    sequences: A list of sequences.

Yields:
    Yields individual texts.
N )r8   r<   rg   r:   r=   rh   join)r>   r_   r8   ri   r   rj   numwords           r   rm   &Tokenizer.sequences_to_texts_generator  s      NN	//--dnn=CD**3/# S%5*6 KK(HI 7 D)^^/KK @A  88D>DJ s   B+C-1<C-c                 B    U R                  U5      nU R                  X2S9$ )zConvert a list of texts to a Numpy matrix.

Args:
    texts: list of strings.
    mode: one of "binary", "count", "tfidf", "freq".

Returns:
    A Numpy matrix.
)mode)rd   sequences_to_matrix)r>   rX   rw   r_   s       r   texts_to_matrixTokenizer.texts_to_matrix  s(     ++E2	''	'==r   c                 ~   U R                   (       d5  U R                  (       a  [        U R                  5      S-   nO[        S5      eU R                   nUS:X  a  U R                  (       d  [        S5      e[
        R                  " [        U5      U45      n[        U5       GH  u  pVU(       d  M  [        R                  " [        5      nU H  nX:  a  M
  Xx==   S-  ss'   M     [        UR                  5       5       H  u  pUS:X  a	  XU   U'   M  US:X  a  U	[        U5      -  XE   U'   M/  US:X  a	  SXE   U'   M>  US:X  ag  S[
        R                  " U	5      -   n
[
        R                  " SU R                  SU R                  R                  US5      -   -  -   5      nX-  XE   U'   M  [        S	U5      e   GM     U$ )
a`  Converts a list of sequences into a Numpy matrix.

Args:
    sequences: list of sequences
        (a sequence is a list of integer word indices).
    mode: one of "binary", "count", "tfidf", "freq"

Returns:
    A Numpy matrix.

Raises:
    ValueError: In case of invalid `mode` argument,
        or if the Tokenizer requires to be fit to sample data.
r'   zKSpecify a dimension (`num_words` argument), or fit on some text data first.tfidfz7Fit the Tokenizer on some data before using tfidf mode.countfreqbinaryr   zUnknown vectorization mode:)r8   r<   rW   
ValueErrorr.   npzeros	enumerater3   r6   r   rO   rQ   logr;   rg   )r>   r_   rw   r8   rE   r   r   countsjr   tfidfs               r   rx   Tokenizer.sequences_to_matrix  s    ~~014	 6 
 I7?4#6#6I  HHc)ni01	*FA ,,S1F>	Q	  V\\^,7?aDGV^#c(lADGX%ADGW_ RVVAYB&&--T__5H5HA5N1NOPC !hADG$%BDII# - +4 r   c                    [         R                  " U R                  5      n[         R                  " U R                  5      n[         R                  " U R                  5      n[         R                  " U R
                  5      n[         R                  " U R                  5      nU R                  U R                  U R                  U R                  U R                  U R                  U R                  UUUUUS.$ )a
  Returns the tokenizer configuration as Python dictionary.

The word count dictionaries used by the tokenizer get serialized
into plain JSON, so that the configuration can be read by other
projects.

Returns:
    A Python dictionary with the tokenizer configuration.
)r8   r   r   r	   r9   r:   r.   r5   r7   r;   r=   r<   )jsondumpsr5   r7   r;   r<   r=   r8   r   r   r	   r9   r:   r.   )r>   json_word_countsjson_word_docsjson_index_docsjson_word_indexjson_index_words         r   
get_configTokenizer.get_config  s      ::d&6&67DNN3**T__5**T__5**T__5 ||ZZZZ//"11+')))
 	
r   c                     U R                  5       nU R                  R                  US.n[        R                  " U40 UD6$ )aF  Returns a JSON string containing the tokenizer configuration.

To load a tokenizer from a JSON string, use
`keras.preprocessing.text.tokenizer_from_json(json_string)`.

Args:
    **kwargs: Additional keyword arguments
        to be passed to `json.dumps()`.

Returns:
    A JSON string containing the tokenizer configuration.
)
class_nameconfig)r   	__class____name__r   r   )r>   r?   r   tokenizer_configs       r   to_jsonTokenizer.to_json-  s?     "..11
 zz*5f55r   )r   r9   r.   r   r;   r=   r   r8   r:   r	   r5   r7   r<   )N!!"#$%&()*+,-./:;<=>?@[\]^_`{|}~	
Trq   FNN)r   )r   
__module____qualname____firstlineno____doc__r@   r\   r`   rd   rc   rn   rm   ry   rx   r   r   __static_attributes__rD   r   r   r*   r*      s\    #N 6"!H:4x( >.`B@>:x
>6r   r*   z,keras.preprocessing.text.tokenizer_from_jsonc                    [         R                  " U 5      nUR                  S5      n[         R                  " UR                  S5      5      n[         R                  " UR                  S5      5      n[         R                  " UR                  S5      5      nUR	                  5        VVs0 s H  u  pg[        U5      U_M     nnn[         R                  " UR                  S5      5      nUR	                  5        VVs0 s H  u  pg[        U5      U_M     nnn[         R                  " UR                  S5      5      n	[        S0 UD6n
X:l        XJl        XZl	        Xl
        Xl        U
$ s  snnf s  snnf )aI  Parses a JSON tokenizer configuration and returns a tokenizer instance.

Deprecated: `tf.keras.preprocessing.text.Tokenizer` does not operate on
tensors and is not recommended for new code. Prefer
`tf.keras.layers.TextVectorization` which provides equivalent functionality
through a layer which accepts `tf.Tensor` input. See the
[text loading tutorial](https://www.tensorflow.org/tutorials/load_data/text)
for an overview of the layer and text handling in tensorflow.

Args:
    json_string: JSON string encoding a tokenizer configuration.

Returns:
    A TF-Keras Tokenizer instance
r   r5   r7   r;   r=   r<   rD   )r   loadsrg   r1   rQ   r   r*   r5   r7   r;   r<   r=   )json_stringr   r   r5   r7   r;   kvr=   r<   	tokenizers              r   tokenizer_from_jsonr   B  s!   " zz+.!!(+F**VZZ67K

6::k23IFJJ|45J(2(8(8(:;(:#a&!)(:J;FJJ|45J(2(8(8(:;(:#a&!)(:J;FJJ|45J#F#I'#%%% <;s   *E(=E.)r   Trq   )r   Trq   N)Nr   Trq   N)r   r3   r    r   r/   numpyr    tensorflow.python.util.tf_exportr   r   r   r   objectr*   r   rD   r   r   <module>r      s   
      : >? 3

	)! @)!X 01 3

/ 2/d 67 2

7; 87;t 23|6 |6 4|6~ <=" >"r   