DRG DRG - 19 days ago 7
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/
True
>>> from nltk import bigrams
>>> bigrams(['more', 'is', 'said', 'than', 'done'])
<generator object bigrams at 0x0000000002E64240>

Answer

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')]
Comments