I'm running Python-3.x
on a virtualenv
, trying to process text with nltk
.
I saw this post What are ngram counts... and the most upvoted answer has a bit of code using the count()
method. but when I copy/paste it into mine:
import nltk
from nltk import bigrams
from nltk import trigrams
text="""Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ornare
tempor lacus, quis pellentesque diam tempus vitae. Morbi justo mauris,
congue sit amet imperdiet ipsum dolor sit amet, consectetur adipiscing elit. Nullam ornare
tempor lacus, quis pellentesque diam"""
tokens = nltk.word_tokenize(text)
tokens = [token.lower() for token in tokens if len(token) > 1]
bi_tokens = bigrams(tokens)
tri_tokens = trigrams(tokens)
print [(item, tri_tokens.count(item)) for item in sorted(set(tri_tokens))]
I receive this message:
AttributeError: 'generator' object has no attribute 'count'
I see this other post on a monkeypatch for a count method but feel like that's somehow not related. Any idea what I might be doing wrong?