We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Why TF-IDF matrix generated by keras.preprocessing.text.Tokenizer() has negative values?
Say, if run the following script:

> > > import keras
> > > tk = keras.preprocessing.text.Tokenizer()
> > > texts = ['I love you.', 'I love you, too.']
> > > tk.fit_on_texts(texts)
> > > tk.texts_to_matrix(texts, mode='tfidf')

The output will be:
array([[ 0.        , -1.09861229, -1.09861229, -1.09861229,  0.        ],
       [ 0.        , -1.38629436, -1.38629436, -1.38629436, -1.38629436]])

But tf-idf values seems should be non-negative?

By the way, is there a neat way to get the word by its index, or the vocabulary (in the order of word indices) of the Tokenizer() class?  Say, sometimes I want to know what's the most frequent word in the documents, then I want to access word with index 1.

I can do it by running:

> > > vocab = tk.word_index.items()
> > > vocab.sort(key=lambda x:x[1])

This gives:

> > > vocab

[('i', 1), ('you', 2), ('love', 3), ('too', 4)]
But is it somehow hacky?

Thank you!


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `keras/preprocessing/text.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 '''These preprocessing utilities would greatly benefit
3 from a fast Cython rewrite.
4 '''
5 from __future__ import absolute_import
6 
7 import string
8 import sys
9 import numpy as np
10 from six.moves import range
11 from six.moves import zip
12 
13 if sys.version_info < (3,):
14     maketrans = string.maketrans
15 else:
16     maketrans = str.maketrans
17 
18 
19 def base_filter():
20     f = string.punctuation
21     f = f.replace("'", '')
22     f += '\t\n'
23     return f
24 
25 
26 def text_to_word_sequence(text, filters=base_filter(), lower=True, split=" "):
27     '''prune: sequence of characters to filter out
28     '''
29     if lower:
30         text = text.lower()
31     text = text.translate(maketrans(filters, split*len(filters)))
32     seq = text.split(split)
33     return [_f for _f in seq if _f]
34 
35 
36 def one_hot(text, n, filters=base_filter(), lower=True, split=" "):
37     seq = text_to_word_sequence(text, filters=filters, lower=lower, split=split)
38     return [(abs(hash(w)) % (n - 1) + 1) for w in seq]
39 
40 
41 class Tokenizer(object):
42     def __init__(self, nb_words=None, filters=base_filter(),
43                  lower=True, split=' ', char_level=False):
44         '''The class allows to vectorize a text corpus, by turning each
45         text into either a sequence of integers (each integer being the index
46         of a token in a dictionary) or into a vector where the coefficient
47         for each token could be binary, based on word count, based on tf-idf...
48 
49         # Arguments
50             nb_words: the maximum number of words to keep, based
51                 on word frequency. Only the most common `nb_words` words will
52                 be kept.
53             filters: a string where each element is a character that will be
54                 filtered from the texts. The default is all punctuation, plus
55                 tabs and line breaks, minus the `'` character.
56             lower: boolean. Whether to convert the texts to lowercase.
57             split: character or string to use for token splitting.
58             char_level: if True, every character will be treated as a word.
59 
60         By default, all punctuation is removed, turning the texts into
61         space-separated sequences of words
62         (words maybe include the `'` character). These sequences are then
63         split into lists of tokens. They will then be indexed or vectorized.
64 
65         `0` is a reserved index that won't be assigned to any word.
66         '''
67         self.word_counts = {}
68         self.word_docs = {}
69         self.filters = filters
70         self.split = split
71         self.lower = lower
72         self.nb_words = nb_words
73         self.document_count = 0
74         self.char_level = char_level
75 
76     def fit_on_texts(self, texts):
77         '''Required before using texts_to_sequences or texts_to_matrix
78 
79         # Arguments
80             texts: can be a list of strings,
81                 or a generator of strings (for memory-efficiency)
82         '''
83         self.document_count = 0
84         for text in texts:
85             self.document_count += 1
86             seq = text if self.char_level else text_to_word_sequence(text, self.filters, self.lower, self.split)
87             for w in seq:
88                 if w in self.word_counts:
89                     self.word_counts[w] += 1
90                 else:
91                     self.word_counts[w] = 1
92             for w in set(seq):
93                 if w in self.word_docs:
94                     self.word_docs[w] += 1
95                 else:
96                     self.word_docs[w] = 1
97 
98         wcounts = list(self.word_counts.items())
99         wcounts.sort(key=lambda x: x[1], reverse=True)
100         sorted_voc = [wc[0] for wc in wcounts]
101         self.word_index = dict(list(zip(sorted_voc, list(range(1, len(sorted_voc) + 1)))))
102 
103         self.index_docs = {}
104         for w, c in list(self.word_docs.items()):
105             self.index_docs[self.word_index[w]] = c
106 
107     def fit_on_sequences(self, sequences):
108         '''Required before using sequences_to_matrix
109         (if fit_on_texts was never called)
110         '''
111         self.document_count = len(sequences)
112         self.index_docs = {}
113         for seq in sequences:
114             seq = set(seq)
115             for i in seq:
116                 if i not in self.index_docs:
117                     self.index_docs[i] = 1
118                 else:
119                     self.index_docs[i] += 1
120 
121     def texts_to_sequences(self, texts):
122         '''Transforms each text in texts in a sequence of integers.
123         Only top "nb_words" most frequent words will be taken into account.
124         Only words known by the tokenizer will be taken into account.
125 
126         Returns a list of sequences.
127         '''
128         res = []
129         for vect in self.texts_to_sequences_generator(texts):
130             res.append(vect)
131         return res
132 
133     def texts_to_sequences_generator(self, texts):
134         '''Transforms each text in texts in a sequence of integers.
135         Only top "nb_words" most frequent words will be taken into account.
136         Only words known by the tokenizer will be taken into account.
137 
138         Yields individual sequences.
139 
140         # Arguments:
141             texts: list of strings.
142         '''
143         nb_words = self.nb_words
144         for text in texts:
145             seq = text if self.char_level else text_to_word_sequence(text, self.filters, self.lower, self.split)
146             vect = []
147             for w in seq:
148                 i = self.word_index.get(w)
149                 if i is not None:
150                     if nb_words and i >= nb_words:
151                         continue
152                     else:
153                         vect.append(i)
154             yield vect
155 
156     def texts_to_matrix(self, texts, mode='binary'):
157         '''Convert a list of texts to a Numpy matrix,
158         according to some vectorization mode.
159 
160         # Arguments:
161             texts: list of strings.
162             modes: one of "binary", "count", "tfidf", "freq"
163         '''
164         sequences = self.texts_to_sequences(texts)
165         return self.sequences_to_matrix(sequences, mode=mode)
166 
167     def sequences_to_matrix(self, sequences, mode='binary'):
168         '''Converts a list of sequences into a Numpy matrix,
169         according to some vectorization mode.
170 
171         # Arguments:
172             sequences: list of sequences
173                 (a sequence is a list of integer word indices).
174             modes: one of "binary", "count", "tfidf", "freq"
175         '''
176         if not self.nb_words:
177             if self.word_index:
178                 nb_words = len(self.word_index) + 1
179             else:
180                 raise Exception('Specify a dimension (nb_words argument), '
181                                 'or fit on some text data first.')
182         else:
183             nb_words = self.nb_words
184 
185         if mode == 'tfidf' and not self.document_count:
186             raise Exception('Fit the Tokenizer on some data '
187                             'before using tfidf mode.')
188 
189         X = np.zeros((len(sequences), nb_words))
190         for i, seq in enumerate(sequences):
191             if not seq:
192                 continue
193             counts = {}
194             for j in seq:
195                 if j >= nb_words:
196                     continue
197                 if j not in counts:
198                     counts[j] = 1.
199                 else:
200                     counts[j] += 1
201             for j, c in list(counts.items()):
202                 if mode == 'count':
203                     X[i][j] = c
204                 elif mode == 'freq':
205                     X[i][j] = c / len(seq)
206                 elif mode == 'binary':
207                     X[i][j] = 1
208                 elif mode == 'tfidf':
209                     # Use weighting scheme 2 in
210                     #   https://en.wikipedia.org/wiki/Tf%E2%80%93idf
211                     tf = 1 + np.log(c)
212                     idf = np.log(1 + self.document_count / (1 + self.index_docs.get(j, 0)))
213                     X[i][j] = tf * idf
214                 else:
215                     raise Exception('Unknown vectorization mode: ' + str(mode))
216         return X
217
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/keras/preprocessing/text.py b/keras/preprocessing/text.py
--- a/keras/preprocessing/text.py
+++ b/keras/preprocessing/text.py
@@ -3,6 +3,7 @@
 from a fast Cython rewrite.
 '''
 from __future__ import absolute_import
+from __future__ import division
 
 import string
 import sys

{"golden_diff": "diff --git a/keras/preprocessing/text.py b/keras/preprocessing/text.py\n--- a/keras/preprocessing/text.py\n+++ b/keras/preprocessing/text.py\n@@ -3,6 +3,7 @@\n from a fast Cython rewrite.\n '''\n from __future__ import absolute_import\n+from __future__ import division\n \n import string\n import sys\n", "issue": "Why TF-IDF matrix generated by keras.preprocessing.text.Tokenizer() has negative values?\nSay, if run the following script:\n\n> > > import keras\n> > > tk = keras.preprocessing.text.Tokenizer()\n> > > texts = ['I love you.', 'I love you, too.']\n> > > tk.fit_on_texts(texts)\n> > > tk.texts_to_matrix(texts, mode='tfidf')\n\nThe output will be:\narray([[ 0.        , -1.09861229, -1.09861229, -1.09861229,  0.        ],\n       [ 0.        , -1.38629436, -1.38629436, -1.38629436, -1.38629436]])\n\nBut tf-idf values seems should be non-negative?\n\nBy the way, is there a neat way to get the word by its index, or the vocabulary (in the order of word indices) of the Tokenizer() class?  Say, sometimes I want to know what's the most frequent word in the documents, then I want to access word with index 1.\n\nI can do it by running:\n\n> > > vocab = tk.word_index.items()\n> > > vocab.sort(key=lambda x:x[1])\n\nThis gives:\n\n> > > vocab\n\n[('i', 1), ('you', 2), ('love', 3), ('too', 4)]\nBut is it somehow hacky?\n\nThank you!\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n'''These preprocessing utilities would greatly benefit\nfrom a fast Cython rewrite.\n'''\nfrom __future__ import absolute_import\n\nimport string\nimport sys\nimport numpy as np\nfrom six.moves import range\nfrom six.moves import zip\n\nif sys.version_info < (3,):\n    maketrans = string.maketrans\nelse:\n    maketrans = str.maketrans\n\n\ndef base_filter():\n    f = string.punctuation\n    f = f.replace(\"'\", '')\n    f += '\\t\\n'\n    return f\n\n\ndef text_to_word_sequence(text, filters=base_filter(), lower=True, split=\" \"):\n    '''prune: sequence of characters to filter out\n    '''\n    if lower:\n        text = text.lower()\n    text = text.translate(maketrans(filters, split*len(filters)))\n    seq = text.split(split)\n    return [_f for _f in seq if _f]\n\n\ndef one_hot(text, n, filters=base_filter(), lower=True, split=\" \"):\n    seq = text_to_word_sequence(text, filters=filters, lower=lower, split=split)\n    return [(abs(hash(w)) % (n - 1) + 1) for w in seq]\n\n\nclass Tokenizer(object):\n    def __init__(self, nb_words=None, filters=base_filter(),\n                 lower=True, split=' ', char_level=False):\n        '''The class allows to vectorize a text corpus, by turning each\n        text into either a sequence of integers (each integer being the index\n        of a token in a dictionary) or into a vector where the coefficient\n        for each token could be binary, based on word count, based on tf-idf...\n\n        # Arguments\n            nb_words: the maximum number of words to keep, based\n                on word frequency. Only the most common `nb_words` words will\n                be kept.\n            filters: a string where each element is a character that will be\n                filtered from the texts. The default is all punctuation, plus\n                tabs and line breaks, minus the `'` character.\n            lower: boolean. Whether to convert the texts to lowercase.\n            split: character or string to use for token splitting.\n            char_level: if True, every character will be treated as a word.\n\n        By default, all punctuation is removed, turning the texts into\n        space-separated sequences of words\n        (words maybe include the `'` character). These sequences are then\n        split into lists of tokens. They will then be indexed or vectorized.\n\n        `0` is a reserved index that won't be assigned to any word.\n        '''\n        self.word_counts = {}\n        self.word_docs = {}\n        self.filters = filters\n        self.split = split\n        self.lower = lower\n        self.nb_words = nb_words\n        self.document_count = 0\n        self.char_level = char_level\n\n    def fit_on_texts(self, texts):\n        '''Required before using texts_to_sequences or texts_to_matrix\n\n        # Arguments\n            texts: can be a list of strings,\n                or a generator of strings (for memory-efficiency)\n        '''\n        self.document_count = 0\n        for text in texts:\n            self.document_count += 1\n            seq = text if self.char_level else text_to_word_sequence(text, self.filters, self.lower, self.split)\n            for w in seq:\n                if w in self.word_counts:\n                    self.word_counts[w] += 1\n                else:\n                    self.word_counts[w] = 1\n            for w in set(seq):\n                if w in self.word_docs:\n                    self.word_docs[w] += 1\n                else:\n                    self.word_docs[w] = 1\n\n        wcounts = list(self.word_counts.items())\n        wcounts.sort(key=lambda x: x[1], reverse=True)\n        sorted_voc = [wc[0] for wc in wcounts]\n        self.word_index = dict(list(zip(sorted_voc, list(range(1, len(sorted_voc) + 1)))))\n\n        self.index_docs = {}\n        for w, c in list(self.word_docs.items()):\n            self.index_docs[self.word_index[w]] = c\n\n    def fit_on_sequences(self, sequences):\n        '''Required before using sequences_to_matrix\n        (if fit_on_texts was never called)\n        '''\n        self.document_count = len(sequences)\n        self.index_docs = {}\n        for seq in sequences:\n            seq = set(seq)\n            for i in seq:\n                if i not in self.index_docs:\n                    self.index_docs[i] = 1\n                else:\n                    self.index_docs[i] += 1\n\n    def texts_to_sequences(self, texts):\n        '''Transforms each text in texts in a sequence of integers.\n        Only top \"nb_words\" most frequent words will be taken into account.\n        Only words known by the tokenizer will be taken into account.\n\n        Returns a list of sequences.\n        '''\n        res = []\n        for vect in self.texts_to_sequences_generator(texts):\n            res.append(vect)\n        return res\n\n    def texts_to_sequences_generator(self, texts):\n        '''Transforms each text in texts in a sequence of integers.\n        Only top \"nb_words\" most frequent words will be taken into account.\n        Only words known by the tokenizer will be taken into account.\n\n        Yields individual sequences.\n\n        # Arguments:\n            texts: list of strings.\n        '''\n        nb_words = self.nb_words\n        for text in texts:\n            seq = text if self.char_level else text_to_word_sequence(text, self.filters, self.lower, self.split)\n            vect = []\n            for w in seq:\n                i = self.word_index.get(w)\n                if i is not None:\n                    if nb_words and i >= nb_words:\n                        continue\n                    else:\n                        vect.append(i)\n            yield vect\n\n    def texts_to_matrix(self, texts, mode='binary'):\n        '''Convert a list of texts to a Numpy matrix,\n        according to some vectorization mode.\n\n        # Arguments:\n            texts: list of strings.\n            modes: one of \"binary\", \"count\", \"tfidf\", \"freq\"\n        '''\n        sequences = self.texts_to_sequences(texts)\n        return self.sequences_to_matrix(sequences, mode=mode)\n\n    def sequences_to_matrix(self, sequences, mode='binary'):\n        '''Converts a list of sequences into a Numpy matrix,\n        according to some vectorization mode.\n\n        # Arguments:\n            sequences: list of sequences\n                (a sequence is a list of integer word indices).\n            modes: one of \"binary\", \"count\", \"tfidf\", \"freq\"\n        '''\n        if not self.nb_words:\n            if self.word_index:\n                nb_words = len(self.word_index) + 1\n            else:\n                raise Exception('Specify a dimension (nb_words argument), '\n                                'or fit on some text data first.')\n        else:\n            nb_words = self.nb_words\n\n        if mode == 'tfidf' and not self.document_count:\n            raise Exception('Fit the Tokenizer on some data '\n                            'before using tfidf mode.')\n\n        X = np.zeros((len(sequences), nb_words))\n        for i, seq in enumerate(sequences):\n            if not seq:\n                continue\n            counts = {}\n            for j in seq:\n                if j >= nb_words:\n                    continue\n                if j not in counts:\n                    counts[j] = 1.\n                else:\n                    counts[j] += 1\n            for j, c in list(counts.items()):\n                if mode == 'count':\n                    X[i][j] = c\n                elif mode == 'freq':\n                    X[i][j] = c / len(seq)\n                elif mode == 'binary':\n                    X[i][j] = 1\n                elif mode == 'tfidf':\n                    # Use weighting scheme 2 in\n                    #   https://en.wikipedia.org/wiki/Tf%E2%80%93idf\n                    tf = 1 + np.log(c)\n                    idf = np.log(1 + self.document_count / (1 + self.index_docs.get(j, 0)))\n                    X[i][j] = tf * idf\n                else:\n                    raise Exception('Unknown vectorization mode: ' + str(mode))\n        return X\n", "path": "keras/preprocessing/text.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n'''These preprocessing utilities would greatly benefit\nfrom a fast Cython rewrite.\n'''\nfrom __future__ import absolute_import\nfrom __future__ import division\n\nimport string\nimport sys\nimport numpy as np\nfrom six.moves import range\nfrom six.moves import zip\n\nif sys.version_info < (3,):\n    maketrans = string.maketrans\nelse:\n    maketrans = str.maketrans\n\n\ndef base_filter():\n    f = string.punctuation\n    f = f.replace(\"'\", '')\n    f += '\\t\\n'\n    return f\n\n\ndef text_to_word_sequence(text, filters=base_filter(), lower=True, split=\" \"):\n    '''prune: sequence of characters to filter out\n    '''\n    if lower:\n        text = text.lower()\n    text = text.translate(maketrans(filters, split*len(filters)))\n    seq = text.split(split)\n    return [_f for _f in seq if _f]\n\n\ndef one_hot(text, n, filters=base_filter(), lower=True, split=\" \"):\n    seq = text_to_word_sequence(text, filters=filters, lower=lower, split=split)\n    return [(abs(hash(w)) % (n - 1) + 1) for w in seq]\n\n\nclass Tokenizer(object):\n    def __init__(self, nb_words=None, filters=base_filter(),\n                 lower=True, split=' ', char_level=False):\n        '''The class allows to vectorize a text corpus, by turning each\n        text into either a sequence of integers (each integer being the index\n        of a token in a dictionary) or into a vector where the coefficient\n        for each token could be binary, based on word count, based on tf-idf...\n\n        # Arguments\n            nb_words: the maximum number of words to keep, based\n                on word frequency. Only the most common `nb_words` words will\n                be kept.\n            filters: a string where each element is a character that will be\n                filtered from the texts. The default is all punctuation, plus\n                tabs and line breaks, minus the `'` character.\n            lower: boolean. Whether to convert the texts to lowercase.\n            split: character or string to use for token splitting.\n            char_level: if True, every character will be treated as a word.\n\n        By default, all punctuation is removed, turning the texts into\n        space-separated sequences of words\n        (words maybe include the `'` character). These sequences are then\n        split into lists of tokens. They will then be indexed or vectorized.\n\n        `0` is a reserved index that won't be assigned to any word.\n        '''\n        self.word_counts = {}\n        self.word_docs = {}\n        self.filters = filters\n        self.split = split\n        self.lower = lower\n        self.nb_words = nb_words\n        self.document_count = 0\n        self.char_level = char_level\n\n    def fit_on_texts(self, texts):\n        '''Required before using texts_to_sequences or texts_to_matrix\n\n        # Arguments\n            texts: can be a list of strings,\n                or a generator of strings (for memory-efficiency)\n        '''\n        self.document_count = 0\n        for text in texts:\n            self.document_count += 1\n            seq = text if self.char_level else text_to_word_sequence(text, self.filters, self.lower, self.split)\n            for w in seq:\n                if w in self.word_counts:\n                    self.word_counts[w] += 1\n                else:\n                    self.word_counts[w] = 1\n            for w in set(seq):\n                if w in self.word_docs:\n                    self.word_docs[w] += 1\n                else:\n                    self.word_docs[w] = 1\n\n        wcounts = list(self.word_counts.items())\n        wcounts.sort(key=lambda x: x[1], reverse=True)\n        sorted_voc = [wc[0] for wc in wcounts]\n        self.word_index = dict(list(zip(sorted_voc, list(range(1, len(sorted_voc) + 1)))))\n\n        self.index_docs = {}\n        for w, c in list(self.word_docs.items()):\n            self.index_docs[self.word_index[w]] = c\n\n    def fit_on_sequences(self, sequences):\n        '''Required before using sequences_to_matrix\n        (if fit_on_texts was never called)\n        '''\n        self.document_count = len(sequences)\n        self.index_docs = {}\n        for seq in sequences:\n            seq = set(seq)\n            for i in seq:\n                if i not in self.index_docs:\n                    self.index_docs[i] = 1\n                else:\n                    self.index_docs[i] += 1\n\n    def texts_to_sequences(self, texts):\n        '''Transforms each text in texts in a sequence of integers.\n        Only top \"nb_words\" most frequent words will be taken into account.\n        Only words known by the tokenizer will be taken into account.\n\n        Returns a list of sequences.\n        '''\n        res = []\n        for vect in self.texts_to_sequences_generator(texts):\n            res.append(vect)\n        return res\n\n    def texts_to_sequences_generator(self, texts):\n        '''Transforms each text in texts in a sequence of integers.\n        Only top \"nb_words\" most frequent words will be taken into account.\n        Only words known by the tokenizer will be taken into account.\n\n        Yields individual sequences.\n\n        # Arguments:\n            texts: list of strings.\n        '''\n        nb_words = self.nb_words\n        for text in texts:\n            seq = text if self.char_level else text_to_word_sequence(text, self.filters, self.lower, self.split)\n            vect = []\n            for w in seq:\n                i = self.word_index.get(w)\n                if i is not None:\n                    if nb_words and i >= nb_words:\n                        continue\n                    else:\n                        vect.append(i)\n            yield vect\n\n    def texts_to_matrix(self, texts, mode='binary'):\n        '''Convert a list of texts to a Numpy matrix,\n        according to some vectorization mode.\n\n        # Arguments:\n            texts: list of strings.\n            modes: one of \"binary\", \"count\", \"tfidf\", \"freq\"\n        '''\n        sequences = self.texts_to_sequences(texts)\n        return self.sequences_to_matrix(sequences, mode=mode)\n\n    def sequences_to_matrix(self, sequences, mode='binary'):\n        '''Converts a list of sequences into a Numpy matrix,\n        according to some vectorization mode.\n\n        # Arguments:\n            sequences: list of sequences\n                (a sequence is a list of integer word indices).\n            modes: one of \"binary\", \"count\", \"tfidf\", \"freq\"\n        '''\n        if not self.nb_words:\n            if self.word_index:\n                nb_words = len(self.word_index) + 1\n            else:\n                raise Exception('Specify a dimension (nb_words argument), '\n                                'or fit on some text data first.')\n        else:\n            nb_words = self.nb_words\n\n        if mode == 'tfidf' and not self.document_count:\n            raise Exception('Fit the Tokenizer on some data '\n                            'before using tfidf mode.')\n\n        X = np.zeros((len(sequences), nb_words))\n        for i, seq in enumerate(sequences):\n            if not seq:\n                continue\n            counts = {}\n            for j in seq:\n                if j >= nb_words:\n                    continue\n                if j not in counts:\n                    counts[j] = 1.\n                else:\n                    counts[j] += 1\n            for j, c in list(counts.items()):\n                if mode == 'count':\n                    X[i][j] = c\n                elif mode == 'freq':\n                    X[i][j] = c / len(seq)\n                elif mode == 'binary':\n                    X[i][j] = 1\n                elif mode == 'tfidf':\n                    # Use weighting scheme 2 in\n                    #   https://en.wikipedia.org/wiki/Tf%E2%80%93idf\n                    tf = 1 + np.log(c)\n                    idf = np.log(1 + self.document_count / (1 + self.index_docs.get(j, 0)))\n                    X[i][j] = tf * idf\n                else:\n                    raise Exception('Unknown vectorization mode: ' + str(mode))\n        return X\n", "path": "keras/preprocessing/text.py"}]}