a
    Sic                  
   @   sl   d Z ddlZddlZddlmZ ddlmZ ddlm	Z
 ddlmZ eddddZeddddZdS )z%Reuters topic classification dataset.    N)_remove_long_seq)get_file)
tf_logging)keras_exportz keras.datasets.reuters.load_datareuters.npz皙?q            c	                    s  d|	v rt d |	d|	r2tdt|	 d}
t| |
d dd} tj| dd	"}|d
 |d  }}W d   n1 s~0    Y  tj	|}t
t|}|| || }|| }durڇ fdd|D }n r fdd|D }|rt|||\}}stdd |D dur@fdd|D }nfdd|D }tt|d|  }tj|d| ddt|d|  }}tj||d ddt||d  }}||f||ffS )a*  Loads the Reuters newswire classification dataset.

    This is a dataset of 11,228 newswires from Reuters, labeled over 46 topics.

    This was originally generated by parsing and preprocessing the classic
    Reuters-21578 dataset, but the preprocessing code is no longer packaged
    with Keras. See this
    [GitHub discussion](https://github.com/keras-team/keras/issues/12072)
    for more info.

    Each newswire is encoded as a list of word indexes (integers).
    For convenience, words are indexed by overall frequency in the dataset,
    so that for instance the integer "3" encodes the 3rd most frequent word in
    the data. This allows for quick filtering operations such as:
    "only consider the top 10,000 most
    common words, but eliminate the top 20 most common words".

    As a convention, "0" does not stand for a specific word, but instead is used
    to encode any unknown word.

    Args:
      path: where to cache the data (relative to `~/.keras/dataset`).
      num_words: integer or None. Words are
          ranked by how often they occur (in the training set) and only
          the `num_words` most frequent words are kept. Any less frequent word
          will appear as `oov_char` value in the sequence data. If None,
          all words are kept. Defaults to None, so all words are kept.
      skip_top: skip the top N most frequently occurring words
          (which may not be informative). These words will appear as
          `oov_char` value in the dataset. Defaults to 0, so no words are
          skipped.
      maxlen: int or None. Maximum sequence length.
          Any longer sequence will be truncated. Defaults to None, which
          means no truncation.
      test_split: Float between 0 and 1. Fraction of the dataset to be used
        as test data. Defaults to 0.2, meaning 20% of the dataset is used as
        test data.
      seed: int. Seed for reproducible data shuffling.
      start_char: int. The start of a sequence will be marked with this
          character. Defaults to 1 because 0 is usually the padding character.
      oov_char: int. The out-of-vocabulary character.
          Words that were cut out because of the `num_words` or
          `skip_top` limits will be replaced with this character.
      index_from: int. Index actual words with this index and higher.
      **kwargs: Used for backwards compatibility.

    Returns:
      Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.

    **x_train, x_test**: lists of sequences, which are lists of indexes
      (integers). If the num_words argument was specific, the maximum
      possible index value is `num_words - 1`. If the `maxlen` argument was
      specified, the largest possible sequence length is `maxlen`.

    **y_train, y_test**: lists of integer labels (1 or 0).

    Note: The 'out of vocabulary' character is only used for
    words that were present in the training set but are not included
    because they're not making the `num_words` cut here.
    Words that were not seen in the training set but are in the test set
    have simply been skipped.
    nb_wordszDThe `nb_words` argument in `load_data` has been renamed `num_words`.z Unrecognized keyword arguments: <https://storage.googleapis.com/tensorflow/tf-keras-datasets/r   Z@d6586e694ee56d7a4e65172e12b3e987c03096cb01eab99753921ef915959916origin	file_hashT)allow_picklexyNc                    s$   g | ]}g fd d|D  qS )c                    s   g | ]}|  qS  r   .0w
index_fromr   R/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/datasets/reuters.py
<listcomp>       (load_data.<locals>.<listcomp>.<listcomp>r   r   r   )r   
start_charr   r   r      r   zload_data.<locals>.<listcomp>c                    s   g | ]} fd d|D qS )c                    s   g | ]}|  qS r   r   r   r   r   r   r      r   r   r   r   r   r   r   r      r   c                 s   s   | ]}t |V  qd S )N)maxr   r   r   r   	<genexpr>   r   zload_data.<locals>.<genexpr>c                    s"   g | ]} fd d|D qS )c                    s,   g | ]$}|  kr k r$n n|nqS r   r   r   	num_wordsoov_charskip_topr   r   r      r   r   r   r   r"   r   r   r      s   c                    s    g | ]} fd d|D qS )c                    s(   g | ] }|  kr k rn q|qS r   r   r   r#   r%   r   r   r      r   r   r   r   r&   r   r   r      r   r	   object)dtype)loggingwarningpop	TypeErrorstrr   nploadrandomRandomStatearangelenshuffler   r    intarray)pathr#   r%   maxlen
test_splitseedr   r$   r   kwargsorigin_folderfxslabelsrngindicesidxx_trainy_trainx_testy_testr   )r   r#   r$   r%   r   r   	load_data   sP    L
0


*rG   z%keras.datasets.reuters.get_word_indexreuters_word_index.jsonc                 C   sL   d}t | |d dd} t| }t|W  d   S 1 s>0    Y  dS )a  Retrieves a dict mapping words to their index in the Reuters dataset.

    Args:
        path: where to cache the data (relative to `~/.keras/dataset`).

    Returns:
        The word index dictionary. Keys are word strings, values are their
        index.
    r   rH   Z 4d44cc38712099c9e383dc6e5f11a921r   N)r   openjsonr/   )r7   r<   r=   r   r   r   get_word_index   s    
rK   )	r   Nr   Nr   r   r	   r
   r   )rH   )__doc__rJ   numpyr.   Zkeras.preprocessing.sequencer   keras.utils.data_utilsr   tensorflow.python.platformr   r)    tensorflow.python.util.tf_exportr   rG   rK   r   r   r   r   <module>   s(             