Prateek Dewan Prateek Dewan -4 years ago 124
Python Question

Is there a way to get multiple ngram orders using NTLK instead of obtaining a iterating over a generator?

I need ngrams. I know

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