APPENDIXPRACTICE PROJECT SOLUTIONS

Image

This appendix contains solutions to the practice projects in each chapter. Digital versions are available on the book’s website at https://nostarch.com/real-world-python/.

Chapter 2: Attributing Authorship with Stylometry

Hunting the Hound with Dispersion

practice_hound_dispersion.py
"""Use NLP (nltk) to make dispersion plot."""
import nltk

def text_to_string(filename):
    strings = []
    with open(filename) as f:
        strings.append(f.read())
    return '\n'.join(strings)

corpus = text_to_string('hound.txt')
tokens = nltk.word_tokenize(corpus)
tokens = nltk.Text(tokens)  # NLTK wrapper for automatic text analysis.
dispersion ...

Get Real-World Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.