Prateek Dewan Prateek Dewan - 1 year ago 97
Python Question

Is there a way to directly obtain ngrams using python nltk instead of obtaining a generator object first, and having to iterate over it?

I need ngrams. I know nltk.utils.ngrams can be used to obtain ngrams, but in practice, the ngrams function returns a generator object. I can always iterate over it and store the ngrams in a list. But is there another, more direct way to obtaining these ngrams in a list without having to iterate over them?

Answer Source

@georg's comment pretty much nails it.

In [12]: from nltk.util import ngrams

In [13]: g = ngrams([1,2,3,4,5], 3)

In [14]: list(g)
Out[14]: [(1, 2, 3), (2, 3, 4), (3, 4, 5)]

In [15]: g = ngrams([1,2,3,4,5], 3)

In [16]: map(lambda x: x, g)
Out[16]: [(1, 2, 3), (2, 3, 4), (3, 4, 5)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download