Home › Learn › Python › Errors, classes, and a project

Project: text statistics

Lesson 40 of 40. Assemble the parts you built along the way into a report on a paragraph of text.

Every function this project needs already exists: you wrote them, one per module, and they are saved as the module textstats. The workspace lists them and says which version it is using (a lesson you skipped contributes the reference version until you do it). The from textstats import ... line at the top of the starter brings them in, the same way you would import any module of your own.

Work in steps. Get analyze returning a Report with counts set, run, then add one attribute at a time. When a check fails, its message shows what it expected. If a part misbehaves, fix it in its own lesson; the project picks up the new version on the next run.

Your task

Write analyze(text) that returns a Report built from clean_words(text), with five extra attributes set on it: counts (from word_counts), most_common (the word with the highest count, the first to reach it, or "" when there are no words), longest (the three longest words from longest_words), sentence_count (how many sentences sentences yields), and kinds, a dictionary with the keys short, medium, and long holding how many words word_kind puts in each.

Then write describe(text) returning a list of five lines: the report's summary(), then sentences: <n>, vowels: <n> (from count_vowels on the raw text), most common: <word>, and longest: <words> with the longest words joined by a comma and a space.

This is the final project. It is built from the 8 parts you made along the way; the workspace lists them and which version it is using.

Hint 1

report.counts = word_counts(words); report.most_common = max(report.counts, key=report.counts.get) if report.counts else ""

Hint 2

report.kinds = {"short": 0, "medium": 0, "long": 0}, then for w in words: report.kinds[word_kind(w)] += 1. Count the sentences with sum(1 for _ in sentences(text)), and join the longest words with ", ".join(report.longest).

Show the solution
from textstats import word_kind, count_vowels, longest_words, word_counts, clean_words, sentences, Report


def analyze(text):
    words = clean_words(text)
    report = Report(words)
    report.counts = word_counts(words)
    report.most_common = max(report.counts, key=report.counts.get) if report.counts else ""
    report.longest = longest_words(words, 3)
    report.sentence_count = sum(1 for _ in sentences(text))
    report.kinds = {"short": 0, "medium": 0, "long": 0}
    for w in words:
        report.kinds[word_kind(w)] += 1
    return report


def describe(text):
    report = analyze(text)
    return [
        report.summary(),
        f"sentences: {report.sentence_count}",
        f"vowels: {count_vowels(text)}",
        f"most common: {report.most_common}",
        f"longest: {', '.join(report.longest)}",
    ]
Your partsChecking your parts…

    They are saved as the module textstats, imported at the top of the starter.

      Runs CPython compiled to WebAssembly in this tab (12 MB download the first time, then cached); nothing is sent anywhere.