1

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>
DRG
  • 35
  • 1
  • 5

2 Answers2

12

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')]
maxymoo
  • 35,286
  • 11
  • 92
  • 119
1
<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')]
McNets
  • 10,352
  • 3
  • 32
  • 61
Atiqa
  • 11
  • 1