DRG DRG - 5 months ago 81
Python Question

The function 'bigrams' in python nltk not working

The function bigrams from nltk is returning the following message,

even though nltk is imported and other functions from it are working. Any ideas? Thanks.

>>> import nltk
>>> nltk.download()
showing info http://www.nltk.org/nltk_data/
>>> from nltk import bigrams
>>> bigrams(['more', 'is', 'said', 'than', 'done'])
<generator object bigrams at 0x0000000002E64240>


The function bigrams has returned a "generator" object; this is a Python data type which is like a List but which only creates its elements as they are needed. If you want to realise a generator as a list, you need to explicitly cast it as a list:

list(bigrams(['more', 'is', 'said', 'than', 'done']))
Out[205]: [('more', 'is'), ('is', 'said'), ('said', 'than'), ('than', 'done')]