object of type 'generator' has no len()
<p>I have just started to learn python. I want to write a program in NLTK that breaks a text into unigrams, bigrams.
For example if the input text is...</p>
<pre><code>&quot;I am feeling sad and disappointed due to errors&quot;
</code></pre>
<p>... my function should generate text like:</p>
<pre><code>I am--&gt;am feeling--&gt;feeling sad--&gt;sad and--&gt;and disappointed--&gt;disppointed due--&gt;due to--&gt;to errors
</code></pre>
<p>I have written code to input text into the program. Here's the function I'm trying:</p>
<pre><code>def gen_bigrams(text):
    token = nltk.word_tokenize(review)
    bigrams = ngrams(token, 2)
    #print Counter(bigrams)
    bigram_list = &quot;&quot;
    for x in range(0, len(bigrams)):
        words = bigrams[x]
        bigram_list = bigram_list + words[0]+ &quot; &quot; + words[1]+&quot;--&gt;&quot;
    return bigram_list
</code></pre>
<p>The error I'm getting is...</p>
<pre><code>for x in range(0, len(bigrams)):

TypeError: object of type 'generator' has no len()
</code></pre>
<p>As the <code>ngrams</code> function returns a generator, I tried using <code>len(list(bigrams))</code> but it returns 0 value, so I'm getting the same error. I have referred to other questions on StackExchange but I am still not getting around how to resolve this. I am stuck at this error. Any workaround, suggestion?</p>