NLTK Brown Corpus: Tabulate Function does not work

84 views Asked by At

The code is excerpted from Natural Language Processing with Python, page 119. Frequency of modals in different sections of the Brown Corpus. My issue turns out to be that it fails to tabulate like the book describes. Basically I don't know why this would happen. My Python version is 3.7.9 64-bit. All extensions goes well.

Frequency of modals in different sections of Brown corpus

def tabulate(cfdist, words, categories):
    print('%-16s' % 'Category')
    for word in words:                  # column headings
        print('%6s' % word,)
    print()
    for category in categories:
        print('%-16s' % category,)      # row headings
        for word in words:              # for each word
            print('%6d' % cfdist[category][word])   # print table cell
        print()                         # end the row

cfd = nltk.ConditionalFreqDist(
        (genre, word)
        for genre in brown.categories()
        for word in brown.words(categories=genre))
genres = ['news', 'religion', 'hobbies', 'science_fiction', 'romance', 'humor']
modals = ['can', 'could', 'may', 'might', 'must', 'will']
tabulate(cfd, modals, genres)
0

There are 0 answers