ó
¾÷Xc           @@  sx   d  d l  m Z d  d l Z d  d l Z d  d l m Z d d d d d d „ Z d d	 „ Z	 d
 d e
 e d d „ Z d S(   i    (   t   absolute_importN(   t   ranget   int32t   preg        c         C@  s.  t  |  d ƒ s t d ƒ ‚ n  g  } xI |  D]A } t  | d ƒ sY t d t | ƒ ƒ ‚ n  | j t | ƒ ƒ q+ Wt |  ƒ } | d k rš t j | ƒ } n  t ƒ  }	 x: |  D]2 }
 t |
 ƒ d k rª t j	 |
 ƒ j
 d }	 Pqª qª Wt j | | f |	 ƒ | j | ƒ } x!t |  ƒ D]\ } }
 t |
 ƒ s1qn  | d k rK|
 | } n) | d k rd|
 |  } n t d | ƒ ‚ t j	 | d	 | ƒ} | j
 d |	 k r¿t d
 | j
 d | |	 f ƒ ‚ n  | d k rê| | | d t | ƒ … f <q| d k r| | | t | ƒ d … f <qt d | ƒ ‚ qW| S(   sù  Pads each sequence to the same length (length of the longest sequence).

    If maxlen is provided, any sequence longer
    than maxlen is truncated to maxlen.
    Truncation happens off either the beginning (default) or
    the end of the sequence.

    Supports post-padding and pre-padding (default).

    # Arguments
        sequences: list of lists where each element is a sequence
        maxlen: int, maximum length
        dtype: type to cast the resulting sequence.
        padding: 'pre' or 'post', pad either before or after each sequence.
        truncating: 'pre' or 'post', remove values from sequences larger than
            maxlen either in the beginning or in the end of the sequence
        value: float, value to pad the sequences to the desired value.

    # Returns
        x: numpy array with dimensions (number_of_sequences, maxlen)

    # Raises
        ValueError: in case of invalid values for `truncating` or `padding`,
            or in case of invalid shape for a `sequences` entry.
    t   __len__s   `sequences` must be iterable.s=   `sequences` must be a list of iterables. Found non-iterable: i    i   R   t   posts#   Truncating type "%s" not understoodt   dtypesQ   Shape of sample %s of sequence at position %s is different from expected shape %sNs    Padding type "%s" not understood(   t   hasattrt
   ValueErrort   strt   appendt   lent   Nonet   npt   maxt   tuplet   asarrayt   shapet   onest   astypet	   enumerate(   t	   sequencest   maxlenR   t   paddingt
   truncatingt   valuet   lengthst   xt   num_samplest   sample_shapet   st   idxt   trunc(    (    s;   /tmp/pip-build-isqEY4/keras/keras/preprocessing/sequence.pyt   pad_sequences	   sF    	& gñhãˆµøä>c         C@  sy   d } t  j t t |  ƒ ƒ ƒ } d | d <| t  j | ƒ | d d d | } | | } t  j d | t  j | ƒ ƒ S(   s‹  Generates a word rank-based probabilistic sampling table.

    This generates an array where the ith element
    is the probability that a word of rank i would be sampled,
    according to the sampling distribution used in word2vec.

    The word2vec formula is:
        p(word) = min(1, sqrt(word.frequency/sampling_factor) / (word.frequency/sampling_factor))

    We assume that the word frequencies follow Zipf's law (s=1) to derive
    a numerical approximation of frequency(rank):
       frequency(rank) ~ 1/(rank * (log(rank) + gamma) + 1/2 - 1/(12*rank))
        where gamma is the Euler-Mascheroni constant.

    # Arguments
        size: int, number of possible words to sample.
        sampling_factor: the sampling factor in the word2vec formula.

    # Returns
        A 1D Numpy array of length `size` where the ith entry
        is the probability that a word of rank i should be sampled.
    gX9´Èvâ?i   i    g      à?g      ð?g      (@(   R   t   arrayt   listR   t   logt   minimumt   sqrt(   t   sizet   sampling_factort   gammat   rankt   inv_fqt   f(    (    s;   /tmp/pip-build-isqEY4/keras/keras/preprocessing/sequence.pyt   make_sampling_tableS   s    
'
i   g      ð?c         C@  s+  g  } g  } xü t  |  ƒ D]î \ }	 }
 |
 s1 q n  | d k	 r\ | |
 t j ƒ  k  r\ q q\ n  t d |	 | ƒ } t t |  ƒ |	 | d ƒ } xx t | | ƒ D]g } | |	 k rœ |  | } | sÄ qœ n  | j |
 | g ƒ | ró | j d d g ƒ q| j d ƒ qœ qœ Wq W| d k rÒt t | ƒ | ƒ } g  | D] } | d ^ q4} t j	 | ƒ | g  t | ƒ D]0 }	 | |	 t | ƒ t j
 d | d ƒ g ^ qg7} | r¾| d d g g | 7} qÒ| d g | 7} n  | r!t j
 d d ƒ } t j | ƒ t j	 | ƒ t j | ƒ t j	 | ƒ n  | | f S(   sQ  Generates skipgram word pairs.

    Takes a sequence (list of indexes of words),
    returns couples of [word_index, other_word index] and labels (1s or 0s),
    where label = 1 if 'other_word' belongs to the context of 'word',
    and label=0 if 'other_word' is randomly sampled

    # Arguments
        sequence: a word sequence (sentence), encoded as a list
            of word indices (integers). If using a `sampling_table`,
            word indices are expected to match the rank
            of the words in a reference dataset (e.g. 10 would encode
            the 10-th most frequently occuring token).
            Note that index 0 is expected to be a non-word and will be skipped.
        vocabulary_size: int. maximum possible word index + 1
        window_size: int. actually half-window.
            The window of a word wi will be [i-window_size, i+window_size+1]
        negative_samples: float >= 0. 0 for no negative (=random) samples.
            1 for same number as positive samples. etc.
        shuffle: whether to shuffle the word couples before returning them.
        categorical: bool. if False, labels will be
            integers (eg. [0, 1, 1 .. ]),
            if True labels will be categorical eg. [[1,0],[0,1],[0,1] .. ]
        sampling_table: 1D array of size `vocabulary_size` where the entry i
            encodes the probabibily to sample a word of rank i.

    # Returns
        couples, labels: where `couples` are int pairs and
            `labels` are either 0 or 1.

    # Note
        By convention, index 0 in the vocabulary is
        a non-word and will be skipped.
    i    i   g    ÐcAN(   R   R   t   randomR   t   minR   R   R
   t   intt   shufflet   randintt   seed(   t   sequencet   vocabulary_sizet   window_sizet   negative_samplesR1   t   categoricalt   sampling_tablet   couplest   labelst   it   wit   window_startt
   window_endt   jt   wjt   num_negative_samplest   ct   wordsR3   (    (    s;   /tmp/pip-build-isqEY4/keras/keras/preprocessing/sequence.pyt	   skipgramss   sF    %	
A(   t
   __future__R    t   numpyR   R.   t	   six.movesR   R   R!   R-   t   Truet   FalseRE   (    (    (    s;   /tmp/pip-build-isqEY4/keras/keras/preprocessing/sequence.pyt   <module>   s   I!	