DRG
DRG

Reputation: 35

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>

Upvotes: 1

Views: 8098

Answers (2)

maxymoo
maxymoo

Reputation: 36545

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']))
[('more', 'is'), ('is', 'said'), ('said', 'than'), ('than', 'done')]

Upvotes: 12

Atiqa
Atiqa

Reputation: 11

<generator object bigrams at 0x0000000002E64240>

when this instruction appears it means that your bigrams are created and they are ready to be displayed. Now if you want them to display just put your instruction as:

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

which means that you need bigrams as output in the form of list and you will get:

[('more', 'is'), ('is', 'said'), ('said', 'than'), ('than', 'done')]

Upvotes: 1

Related Questions