FreqDist.keys() returning a list not in frequency order.

1,864 views
Skip to first unread message

AZiffelle

unread,
Mar 23, 2015, 8:52:17 PM3/23/15
to nltk-...@googlegroups.com

I'm going through the book, Natural Language Processing with Python by Steven Bird, Ewan Klein & Edward Loper, in which it claims that FreqDist.keys() will return a list that is ordered by frequency. In my environment, that isn't happening (Python 2.7.9 on Mac OS X 10.6.8).

>>> pprint.pprint(tag_fd)
FreqDist({u'NN': 13162, u'IN': 10616, u'AT': 8893, u'NP': 6866, u',': 5133, u'NNS': 5066, u'.': 4452, u'JJ': 4392, u'CC': 2664, u'VBD': 2524, ...})
>>> tag_fd.keys()[:5]
[u'BE', u'BEZ-HL', u'NP$', u'WQL', u'AT-TL']

Instead, I should expect [u'NN', u'IN', u'AT', u'NP', u',']?

This problem exhibits itself all the time, not just with unicode keys.

Steven Bird

unread,
Mar 23, 2015, 9:11:24 PM3/23/15
to nltk-...@googlegroups.com

This is a change introduced in NLTK 3.

Please use sorted(fdist) instead of fdist.keys().

--
You received this message because you are subscribed to the Google Groups "nltk-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nltk-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages