def build_words_dataset(words=None, vocabulary_size=<NUM_LIT>, printable=True, unk_key='<STR_LIT>'):
if words is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>count = [[unk_key, -<NUM_LIT:1>]]<EOL>count.extend(collections.Counter(words).most_common(vocabulary_size - <NUM_LIT:1>))<EOL>dictionary = dict()<EOL>for word, _ in count:<EOL><INDENT>dictionary[word] = len(dictionary)<EOL><DEDENT>data = list()<EOL>unk_count = <NUM_LIT:0><EOL>for word in words:<EOL><INDENT>if word in dictionary:<EOL><INDENT>index = dictionary[word]<EOL><DEDENT>else:<EOL><INDENT>index = <NUM_LIT:0>  <EOL>unk_count += <NUM_LIT:1><EOL><DEDENT>data.append(index)<EOL><DEDENT>count[<NUM_LIT:0>][<NUM_LIT:1>] = unk_count<EOL>reverse_dictionary = dict(zip(dictionary.values(), dictionary.keys()))<EOL>if printable:<EOL><INDENT>tl.logging.info('<STR_LIT>' % len(collections.Counter(words).keys()))<EOL>tl.logging.info('<STR_LIT>'.format(vocabulary_size))<EOL><DEDENT>if len(collections.Counter(words).keys()) < vocabulary_size:<EOL><INDENT>raise Exception(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>return data, count, dictionary, reverse_dictionary<EOL>
Build the words dictionary and replace rare words with 'UNK' token.
    The most common word has the smallest integer id.

    Parameters
    ----------
    words : list of str or byte
        The context in list format. You may need to do preprocessing on the words, such as lower case, remove marks etc.
    vocabulary_size : int
        The maximum vocabulary size, limiting the vocabulary size. Then the script replaces rare words with 'UNK' token.
    printable : boolean
        Whether to print the read vocabulary size of the given words.
    unk_key : str
        Represent the unknown words.

    Returns
    --------
    data : list of int
        The context in a list of ID.
    count : list of tuple and list
        Pair words and IDs.
            - count[0] is a list : the number of rare words
            - count[1:] are tuples : the number of occurrence of each word
            - e.g. [['UNK', 418391], (b'the', 1061396), (b'of', 593677), (b'and', 416629), (b'one', 411764)]
    dictionary : dictionary
        It is `word_to_id` that maps word to ID.
    reverse_dictionary : a dictionary
        It is `id_to_word` that maps ID to word.

    Examples
    --------
    >>> words = tl.files.load_matt_mahoney_text8_dataset()
    >>> vocabulary_size = 50000
    >>> data, count, dictionary, reverse_dictionary = tl.nlp.build_words_dataset(words, vocabulary_size)

    References
    -----------------
    - `tensorflow/examples/tutorials/word2vec/word2vec_basic.py <https://github.com/tensorflow/tensorflow/blob/r0.7/tensorflow/examples/tutorials/word2vec/word2vec_basic.py>`__