Frequency Distributions
How can we automatically identify the words of a text that are most informative about
the topic and genre of the text? Imagine how you might go about finding the 50 most
frequent words of a book. One method would be to keep a tally for each vocabulary
item, like that shown in Figure 1-3. The tally would need thousands of rows, and it
would be an exceedingly laborious process—so laborious that we would rather assign
the task to a machine.
It is a “distribution” since it tells us how the total number of word tokens in the text are distributed across the vocabulary items. Since we often need frequency distributions in language processing, NLTK provides built-in support for them. Let’s use a FreqDist to find the 50 most frequent words of Moby Dick. Try to work out what is going on here, then read the explanation that follows. >>> fdist1 = FreqDist(text1) >>> fdist1 >>> vocabulary1 = fdist1.keys() >>> vocabulary1[:50] [‘,’, ‘the’, ‘.’, ‘of’, ‘and’, ‘a’, ‘to’, ‘;’, ‘in’, ‘that’, “‘”, ‘-‘, ‘his’, ‘it’, ‘I’, ‘s’, ‘is’, ‘he’, ‘with’, ‘was’, ‘as’, ‘”‘, ‘all’, ‘for’, ‘this’, ‘!’, ‘at’, ‘by’, ‘but’, ‘not’, ‘–‘, ‘him’, ‘from’, ‘be’, ‘on’, ‘so’, ‘whale’, ‘one’, ‘you’, ‘had’, ‘have’, ‘there’, ‘But’, ‘or’, ‘were’, ‘now’, ‘which’, ‘?’, ‘me’, ‘like’] >>> fdist1[‘whale’] 906 >>>
