# How to create a table to display relative frequencies of selected words (eg. with, can, will) from any text corpus in nltk package in python [closed]

Interested words are ['with', 'can', 'will']
gutenberg corpus is the corpus we would like to search on.

Expected output is

                           can  could    may  might should   will  would
austen-emma.txt    270    825    213    322    366    559    815
austen-persuasion.txt    100    444     87    166    185    162    351
austen-sense.txt    206    568    169    215    228    354    507
bible-kjv.txt    213    165   1024    475    768   3807    443
blake-poems.txt     20      3      5      2      6      3      3
bryant-stories.txt     75    154     18     23     38    144    110
burgess-busterbrown.txt     23     56      3     17     13     19     46
carroll-alice.txt     57     73     11     28     27     24     70
chesterton-ball.txt    131    117     90     69     75    198    139
chesterton-brown.txt    126    170     47     71     56    111    132
chesterton-thursday.txt    117    148     56     71     54    109    116
edgeworth-parents.txt    340    420    160    127    271    517    503
melville-moby_dick.txt    220    215    230    183    181    379    421
milton-paradise.txt    107     62    116     98     55    161     49
shakespeare-caesar.txt     16     18     35     12     38    129     40
shakespeare-hamlet.txt     33     26     56     28     52    131     60
shakespeare-macbeth.txt     21     15     30      5     41     62     42
whitman-leaves.txt     88     49     85     26     42    261     85


Reference: https://www.nltk.org/book/ch02.html

Finding frequency of words for the Gutenberg Corpus. You need to just put Gutenberg text files as input.

cfd = nltk.ConditionalFreqDist(
(fileids, word)
for fileids in gutenberg.fileids()
for word in gutenberg.words(fileids))
modals = ['with', 'can', 'will']
cfd.tabulate(fileids, samples=modals)


Output :

                        with  can will
austen-emma.txt 1187  270  559
austen-persuasion.txt  643  100  162
austen-sense.txt  971  206  354
bible-kjv.txt 5961  213 3807
blake-poems.txt   59   20    3
bryant-stories.txt  276   75  144
burgess-busterbrown.txt   59   23   19
carroll-alice.txt  175   57   24
chesterton-ball.txt  799  131  198
chesterton-brown.txt  726  126  111
chesterton-thursday.txt  544  117  109
edgeworth-parents.txt 1304  340  517
melville-moby_dick.txt 1659  220  379