o
    i eXw                     @   s   d Z ddlZddlm  mZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ dZdZdZdZdZejZejZejZej Z dZ!eddg dG dd de
j"Z#dS )z-Keras text vectorization preprocessing layer.    N)backend)base_preprocessing_layer)preprocessing_utils)string_lookup)layer_serialization)deserialize_keras_object)layer_utils)tf_utils)keras_exportlower_and_strip_punctuationstrip_punctuationlower
whitespace	characterz)[!"#$%&()\*\+,-\./:;<=>?@\[\\\]^_`{|}~\']zkeras.layers.TextVectorizationz9keras.layers.experimental.preprocessing.TextVectorization)v1c                       s   e Zd ZdZ												d/ fdd		Zd
d Zdd Zd0 fdd	Zdd Zdd Z	dd Z
d1ddZdd Z fddZedd Zd2dd Zd!d" Zd#d$ Zed%d& Zd'd( Zd)d* Zd+d, Zd-d. Z  ZS )3TextVectorizationa5)  A preprocessing layer which maps text features to integer sequences.

    This layer has basic options for managing text in a Keras model. It
    transforms a batch of strings (one example = one string) into either a list
    of token indices (one example = 1D tensor of integer token indices) or a
    dense representation (one example = 1D tensor of float values representing
    data about the example's tokens). This layer is meant to handle natural
    language inputs. To handle simple string inputs (categorical strings or
    pre-tokenized strings) see `tf.keras.layers.StringLookup`.

    The vocabulary for the layer must be either supplied on construction or
    learned via `adapt()`. When this layer is adapted, it will analyze the
    dataset, determine the frequency of individual string values, and create a
    vocabulary from them. This vocabulary can have unlimited size or be capped,
    depending on the configuration options for this layer; if there are more
    unique values in the input than the maximum vocabulary size, the most
    frequent terms will be used to create the vocabulary.

    The processing of each example contains the following steps:

    1. Standardize each example (usually lowercasing + punctuation stripping)
    2. Split each example into substrings (usually words)
    3. Recombine substrings into tokens (usually ngrams)
    4. Index tokens (associate a unique int value with each token)
    5. Transform each example using this index, either into a vector of ints or
       a dense float vector.

    Some notes on passing callables to customize splitting and normalization for
    this layer:

    1. Any callable can be passed to this Layer, but if you want to serialize
       this object you should only pass functions that are registered Keras
       serializables (see `tf.keras.saving.register_keras_serializable` for more
       details).
    2. When using a custom callable for `standardize`, the data received
       by the callable will be exactly as passed to this layer. The callable
       should return a tensor of the same shape as the input.
    3. When using a custom callable for `split`, the data received by the
       callable will have the 1st dimension squeezed out - instead of
       `[["string to split"], ["another string to split"]]`, the Callable will
       see `["string to split", "another string to split"]`. The callable should
       return a Tensor with the first dimension containing the split tokens -
       in this example, we should see something like `[["string", "to",
       "split"], ["another", "string", "to", "split"]]`. This makes the callable
       site natively compatible with `tf.strings.split()`.

    For an overview and full list of preprocessing layers, see the preprocessing
    [guide](https://www.tensorflow.org/guide/keras/preprocessing_layers).

    Args:
      max_tokens: Maximum size of the vocabulary for this layer. This should
        only be specified when adapting a vocabulary or when setting
        `pad_to_max_tokens=True`. Note that this vocabulary
        contains 1 OOV token, so the effective number of tokens is
        `(max_tokens - 1 - (1 if output_mode == "int" else 0))`.
      standardize: Optional specification for standardization to apply to the
        input text. Values can be:
          - `None`: No standardization.
          - `"lower_and_strip_punctuation"`: Text will be lowercased and all
            punctuation removed.
          - `"lower"`: Text will be lowercased.
          - `"strip_punctuation"`: All punctuation will be removed.
          - Callable: Inputs will passed to the callable function, which should
            be standardized and returned.
      split: Optional specification for splitting the input text. Values can be:
          - `None`: No splitting.
          - `"whitespace"`: Split on whitespace.
          - `"character"`: Split on each unicode character.
          - Callable: Standardized inputs will passed to the callable function,
            which should be split and returned.
      ngrams: Optional specification for ngrams to create from the
        possibly-split input text. Values can be None, an integer or tuple of
        integers; passing an integer will create ngrams up to that integer, and
        passing a tuple of integers will create ngrams for the specified values
        in the tuple. Passing None means that no ngrams will be created.
      output_mode: Optional specification for the output of the layer. Values
        can be `"int"`, `"multi_hot"`, `"count"` or `"tf_idf"`, configuring the
        layer as follows:
          - `"int"`: Outputs integer indices, one integer index per split string
            token. When `output_mode == "int"`, 0 is reserved for masked
            locations; this reduces the vocab size to
            `max_tokens - 2` instead of `max_tokens - 1`.
          - `"multi_hot"`: Outputs a single int array per batch, of either
            vocab_size or max_tokens size, containing 1s in all elements where
            the token mapped to that index exists at least once in the batch
            item.
          - `"count"`: Like `"multi_hot"`, but the int array contains a count of
            the number of times the token at that index appeared in the
            batch item.
          - `"tf_idf"`: Like `"multi_hot"`, but the TF-IDF algorithm is applied
            to find the value in each token slot.
        For `"int"` output, any shape of input and output is supported. For all
        other output modes, currently only rank 1 inputs (and rank 2 outputs
        after splitting) are supported.
      output_sequence_length: Only valid in INT mode. If set, the output will
        have its time dimension padded or truncated to exactly
        `output_sequence_length` values, resulting in a tensor of shape
        `(batch_size, output_sequence_length)` regardless of how many tokens
        resulted from the splitting step. Defaults to `None`.
      pad_to_max_tokens: Only valid in  `"multi_hot"`, `"count"`, and `"tf_idf"`
        modes. If True, the output will have its feature axis padded to
        `max_tokens` even if the number of unique tokens in the vocabulary is
        less than max_tokens, resulting in a tensor of shape `(batch_size,
        max_tokens)` regardless of vocabulary size. Defaults to `False`.
      vocabulary: Optional. Either an array of strings or a string path to a
        text file. If passing an array, can pass a tuple, list, 1D numpy array,
        or 1D tensor containing the string vocabulary terms. If passing a file
        path, the file should contain one line per term in the vocabulary. If
        this argument is set, there is no need to `adapt()` the layer.
      idf_weights: Only valid when `output_mode` is `"tf_idf"`. A tuple, list,
        1D numpy array, or 1D tensor of the same length as the vocabulary,
        containing the floating point inverse document frequency weights, which
        will be multiplied by per sample term counts for the final `tf_idf`
        weight. If the `vocabulary` argument is set, and `output_mode` is
        `"tf_idf"`, this argument must be supplied.
      ragged: Boolean. Only applicable to `"int"` output mode. If True, returns
        a `RaggedTensor` instead of a dense `Tensor`, where each sequence may
        have a different length after string splitting. Defaults to `False`.
      sparse: Boolean. Only applicable to `"multi_hot"`, `"count"`, and
        `"tf_idf"` output modes. If True, returns a `SparseTensor` instead of a
        dense `Tensor`. Defaults to `False`.
      encoding: Optional. The text encoding to use to interpret the input
        strings. Defaults to `"utf-8"`.

    Example:

    This example instantiates a `TextVectorization` layer that lowercases text,
    splits on whitespace, strips punctuation, and outputs integer vocab indices.

    >>> text_dataset = tf.data.Dataset.from_tensor_slices(["foo", "bar", "baz"])
    >>> max_features = 5000  # Maximum vocab size.
    >>> max_len = 4  # Sequence length to pad the outputs to.
    >>>
    >>> # Create the layer.
    >>> vectorize_layer = tf.keras.layers.TextVectorization(
    ...  max_tokens=max_features,
    ...  output_mode='int',
    ...  output_sequence_length=max_len)
    >>>
    >>> # Now that the vocab layer has been created, call `adapt` on the
    >>> # text-only dataset to create the vocabulary. You don't have to batch,
    >>> # but for large datasets this means we're not keeping spare copies of
    >>> # the dataset.
    >>> vectorize_layer.adapt(text_dataset.batch(64))
    >>>
    >>> # Create the model that uses the vectorize text layer
    >>> model = tf.keras.models.Sequential()
    >>>
    >>> # Start by creating an explicit input layer. It needs to have a shape of
    >>> # (1,) (because we need to guarantee that there is exactly one string
    >>> # input per batch), and the dtype needs to be 'string'.
    >>> model.add(tf.keras.Input(shape=(1,), dtype=tf.string))
    >>>
    >>> # The first layer in our model is the vectorization layer. After this
    >>> # layer, we have a tensor of shape (batch_size, max_len) containing
    >>> # vocab indices.
    >>> model.add(vectorize_layer)
    >>>
    >>> # Now, the model can map strings to integers, and you can add an
    >>> # embedding layer to map these integers to learned embeddings.
    >>> input_data = [["foo qux bar"], ["qux baz"]]
    >>> model.predict(input_data)
    array([[2, 1, 4, 0],
           [1, 3, 0, 0]])

    Example:

    This example instantiates a `TextVectorization` layer by passing a list
    of vocabulary terms to the layer's `__init__()` method.

    >>> vocab_data = ["earth", "wind", "and", "fire"]
    >>> max_len = 4  # Sequence length to pad the outputs to.
    >>>
    >>> # Create the layer, passing the vocab directly. You can also pass the
    >>> # vocabulary arg a path to a file containing one vocabulary word per
    >>> # line.
    >>> vectorize_layer = tf.keras.layers.TextVectorization(
    ...  max_tokens=max_features,
    ...  output_mode='int',
    ...  output_sequence_length=max_len,
    ...  vocabulary=vocab_data)
    >>>
    >>> # Because we've passed the vocabulary directly, we don't need to adapt
    >>> # the layer - the vocabulary is already set. The vocabulary contains the
    >>> # padding token ('') and OOV token ('[UNK]') as well as the passed
    >>> # tokens.
    >>> vectorize_layer.get_vocabulary()
    ['', '[UNK]', 'earth', 'wind', 'and', 'fire']

    Nr   r   intFutf-8c                    s6  d|v r|d t jkrtd|d  dd|vrt j|d< tj|tttfddddd tj|tt	fddddd |d	kr?t
}|d
krEt}tj|ttt
tfdddd |d u sqt|tsqt|trjtdd |D sqtd| |tkrt|ts|d u std| |tkr|d urtd| d|r|tkrtd| d| |r|d urtd| d| || _|| _|| _|| _t|trttd|d | _n|| _|| _|| _|| _|| _|d|d u| _|dd }t jdi | t j!"d#d t$j%|||	|d|d ur|nt|
| j||d
| _&d S )NdtypezE`TextVectorization` may only have a dtype of string. Received dtype: .r   standardizeT)allowable_strings
layer_namearg_name
allow_noneallow_callablessplitbinaryztf-idfoutput_mode)r   r   r   r   c                 s   s    | ]}t |tV  qd S N)
isinstancer   ).0item r#   `/var/www/myenv/lib/python3.10/site-packages/keras/src/layers/preprocessing/text_vectorization.py	<genexpr>@  s    z-TextVectorization.__init__.<locals>.<genexpr>zL`ngrams` must be None, an integer, or a tuple of integers. Received: ngrams=zy`output_sequence_length` must be either None or an integer when `output_mode` is 'int'. Received: output_sequence_length=zh`output_sequence_length` must not be set if `output_mode` is not 'int'. Received output_sequence_length=zH`ragged` must not be true if `output_mode` is `'int'`. Received: ragged=z and output_mode=zM`output_sequence_length` must not be set if ragged is True. Received: ragged=z and output_sequence_length=   has_input_vocabularyvocabulary_size )

max_tokens
vocabularyidf_weightspad_to_max_tokens
mask_tokenr   sparser'   encodingr(   r#   )'tfstring
ValueErrorr   validate_string_argLOWER_AND_STRIP_PUNCTUATIONLOWERSTRIP_PUNCTUATION
WHITESPACE	CHARACTER	MULTI_HOTTF_IDFINTCOUNTr    r   tupleall_max_tokens_standardize_split_ngrams_argrange_ngrams_ragged_output_mode_output_sequence_length	_encodingpop_has_input_vocabularysuper__init__r   keras_kpl_gaugeget_cellsetr   StringLookup_lookup_layer)selfr*   r   r   ngramsr   output_sequence_lengthr-   r+   r,   r/   raggedr0   kwargsr(   	__class__r#   r$   rM      s   




zTextVectorization.__init__c                 C   sZ   | j tkrt|d | jgS | jd u r!t|dkr t|d }nt|d }| j	|S )Nr   r&   )r&   r   )
rG   r<   r1   TensorShaperH   rB   lenr>   rR   compute_output_shape)rS   input_shaper#   r#   r$   r\     s   

z&TextVectorization.compute_output_shapec                 C   s6   |  |j }| jtkrtjnt }tj	||dS )N)shaper   )
r\   r^   as_listrG   r<   r1   int64r   floatx
TensorSpec)rS   
input_specoutput_shapeoutput_dtyper#   r#   r$   compute_output_signature  s   z*TextVectorization.compute_output_signaturec                    s   t  j|||d dS )a
  Computes a vocabulary of string terms from tokens in a dataset.

        Calling `adapt()` on a `TextVectorization` layer is an alternative to
        passing in a precomputed vocabulary on construction via the `vocabulary`
        argument. A `TextVectorization` layer should always be either adapted
        over a dataset or supplied with a vocabulary.

        During `adapt()`, the layer will build a vocabulary of all string tokens
        seen in the dataset, sorted by occurrence count, with ties broken by
        sort order of the tokens (high to low). At the end of `adapt()`, if
        `max_tokens` is set, the vocabulary wil be truncated to `max_tokens`
        size. For example, adapting a layer with `max_tokens=1000` will compute
        the 1000 most frequent tokens occurring in the input dataset. If
        `output_mode='tf-idf'`, `adapt()` will also learn the document
        frequencies of each token in the input dataset.

        In order to make `TextVectorization` efficient in any distribution
        context, the vocabulary is kept static with respect to any compiled
        `tf.Graph`s that call the layer. As a consequence, if the layer is
        adapted a second time, any models using the layer should be re-compiled.
        For more information see
        `tf.keras.layers.experimental.preprocessing.PreprocessingLayer.adapt`.

        `adapt()` is meant only as a single machine utility to compute layer
        state.  To analyze a dataset that cannot fit on a single machine, see
        [Tensorflow Transform](
        https://www.tensorflow.org/tfx/transform/get_started) for a
        multi-machine, map-reduce solution.

        Arguments:
          data: The data to train on. It can be passed either as a
              `tf.data.Dataset`, or as a numpy array.
          batch_size: Integer or `None`.
              Number of samples per state update.
              If unspecified, `batch_size` will default to 32.
              Do not specify the `batch_size` if your data is in the
              form of datasets, generators, or `keras.utils.Sequence` instances
              (since they generate batches).
          steps: Integer or `None`.
              Total number of steps (batches of samples)
              When training with input tensors such as
              TensorFlow data tensors, the default `None` is equal to
              the number of samples in your dataset divided by
              the batch size, or 1 if that cannot be determined. If x is a
              `tf.data` dataset, and 'steps' is None, the epoch will run until
              the input dataset is exhausted. When passing an infinitely
              repeating dataset, you must specify the `steps` argument. This
              argument is not supported with array inputs.
        )
batch_sizestepsN)rL   adapt)rS   datarg   rh   rX   r#   r$   ri     s   2zTextVectorization.adaptc                 C   s   | j | | d S r   )rR   update_state_preprocess)rS   rj   r#   r#   r$   rk     s   zTextVectorization.update_statec                 C      | j   d S r   )rR   finalize_staterS   r#   r#   r$   rn        z TextVectorization.finalize_statec                 C   rm   r   )rR   reset_statero   r#   r#   r$   rq     rp   zTextVectorization.reset_stateTc                 C   s   | j |S )a{  Returns the current vocabulary of the layer.

        Args:
          include_special_tokens: If True, the returned vocabulary will include
            the padding and OOV tokens, and a term's index in the vocabulary
            will equal the term's index when calling the layer. If False, the
            returned vocabulary will not include any padding or OOV tokens.
        )rR   get_vocabulary)rS   include_special_tokensr#   r#   r$   rr     s   	z TextVectorization.get_vocabularyc                 C   s
   | j  S )zGets the current size of the layer's vocabulary.

        Returns:
          The integer size of the vocabulary, including optional mask and
          OOV indices.
        )rR   r(   ro   r#   r#   r$   r(     s   
z!TextVectorization.vocabulary_sizec                    sx   | j j| j| j| j| j| j| j j| j j| j	t
| j jt
| j j| j|  d}t  }tt| t|  S )N)r*   r   r   rT   r   rU   r-   r/   rV   r+   r,   r0   r(   )rR   r*   rA   rB   rC   rG   rH   r-   r/   rF   utilslistify_tensorsinput_vocabularyinput_idf_weightsrI   r(   rL   
get_configdictlistitems)rS   configbase_configrX   r#   r$   rx     s(   
zTextVectorization.get_configc                 C   sP   |d t ttfvrt|d |d< |d ttfvr!t|d |d< | di |S )Nr   r   r#   )r5   r6   r7   r   r8   r9   )clsr|   r#   r#   r$   from_config  s   zTextVectorization.from_configc                 C   s   | j j||d dS )aW  Sets vocabulary (and optionally document frequency) for this layer.

        This method sets the vocabulary and idf weights for this layer directly,
        instead of analyzing a dataset through 'adapt'. It should be used
        whenever the vocab (and optionally document frequency) information is
        already known.  If vocabulary data is already present in the layer, this
        method will replace it.

        Args:
          vocabulary: Either an array or a string path to a text file. If
            passing an array, can pass a tuple, list, 1D numpy array, or 1D
            tensor containing the vocbulary terms. If passing a file path, the
            file should contain one line per term in the vocabulary.
          idf_weights: A tuple, list, 1D numpy array, or 1D tensor of inverse
            document frequency weights with equal length to vocabulary. Must be
            set if `output_mode` is `"tf_idf"`. Should not be set otherwise.

        Raises:
          ValueError: If there are too many inputs, the inputs do not match, or
            input data is missing.
          RuntimeError: If the vocabulary cannot be set when this function is
            called. This happens when `"multi_hot"`, `"count"`, and "tf_idf"
            modes, if `pad_to_max_tokens` is False and the layer itself has
            already been called.
        )r,   N)rR   set_vocabulary)rS   r+   r,   r#   r#   r$   r     s   z TextVectorization.set_vocabularyc                 C   s   t j|tjd}| jttfv rtj|}| jt	tfv r$tj
|td}t| jr.| |}| jd ur|jjdkrT|jd dkrMtd|j d|jj tj|dd}| jtkr`tj|}n| jtkrmtj|d}nt| jrx| |}ntd	| j | jd urtjj|| jd
d}|S )N)r   r)   r&   zWhen using `TextVectorization` to tokenize strings, the input rank must be 1 or the last shape dimension must be 1. Received: inputs.shape=z with rank=)axiszUTF-8z%s is not a supported splitting.TextVectorization supports the following options for `split`: None, 'whitespace', or a Callable. )ngram_width	separator)rt   ensure_tensorr1   r2   rA   r6   r5   stringsr   r7   regex_replaceDEFAULT_STRIP_REGEXcallablerB   r^   rankr3   squeezer8   r   r9   unicode_splitrE   rT   )rS   inputsr#   r#   r$   rl   9  sJ   







zTextVectorization._preprocessc                 C   s   t |tttjfrt|}| |}| jd u r|S | 	|}| jt
kr&|S | jr+|S t|rA|j }| j|d< |jd|dS | jd urp|dd | jf }t|}t|d d | jgfd}t||\}}t||S |S )Nr   r   )default_valuer^   .)r    rz   r>   npndarrayr1   convert_to_tensorrl   rG   rR   r<   rF   r	   	is_raggedr^   r_   rH   	to_tensorconcat required_space_to_batch_paddingspad)rS   r   lookup_datar^   padded_shapepadding_r#   r#   r$   calln  s2   









zTextVectorization.callc                 C   s
   t | S r   )r   VocabularySavedModelSaverro   r#   r#   r$   _trackable_saved_model_saver  s   
z.TextVectorization._trackable_saved_model_saverc                 C      | j | d S r   )rR   save_own_variablesrS   storer#   r#   r$   r        z$TextVectorization.save_own_variablesc                 C   r   r   )rR   load_own_variablesr   r#   r#   r$   r     r   z$TextVectorization.load_own_variablesc                 C   r   r   )rR   save_assetsrS   dir_pathr#   r#   r$   r     r   zTextVectorization.save_assetsc                 C   r   r   )rR   load_assetsr   r#   r#   r$   r     r   zTextVectorization.load_assets)Nr   r   Nr   NFNNFFr   )NN)Tr   )__name__
__module____qualname____doc__rM   r\   rf   ri   rk   rn   rq   rr   r(   rx   classmethodr   r   rl   r   propertyr   r   r   r   r   __classcell__r#   r#   rX   r$   r   3   sJ     B 4
	

5/
r   )$r   numpyr   tensorflow.compat.v2compatv2r1   	keras.srcr   keras.src.enginer   keras.src.layers.preprocessingr   rt   r   #keras.src.saving.legacy.saved_modelr   "keras.src.saving.serialization_libr   keras.src.utilsr   r	    tensorflow.python.util.tf_exportr
   r5   r7   r6   r8   r9   r;   r<   r:   r=   r   PreprocessingLayerr   r#   r#   r#   r$   <module>   s8   