🎓 A1Learner
🔍

Python Project: Text Report

A word counter is a program that counts how often each word appears in a text. The counter reads a story file and prints a report.

First, create story.txt

Create this file first, with this exact text. The program in the last example reads this file.

the sun rose over the green hill
the birds sang songs in the morning
a happy dog ran over the hill

Count lines and words

strip() trims the ends of the text. split() then cuts it into lines or into words.

text = "the sun rose over the hill\nthe dog ran fast\n"
lines = text.strip().split("\n")
print(len(lines))
words = text.split()
print(len(words))

Count each word

counts.get(word, 0) gives the count so far for each word.

words = ["the", "sun", "the", "moon"]
counts = {}
for word in words:
    counts[word] = counts.get(word, 0) + 1
print(counts["the"])
print(len(counts))

Find the longest word

The loop keeps the longest word so far. A word takes the lead only when it is longer.

words = ["sun", "morning", "dog"]
longest = ""
for word in words:
    if len(word) > len(longest):
        longest = word
print(longest)

Find the most common word

sorted(counts) visits the words in alphabet order. A tie keeps the earlier word in the lead.

counts = {"sun": 2, "moon": 2, "dog": 1}
best = ""
best_count = 0
for word in sorted(counts):
    if counts[word] > best_count:
        best = word
        best_count = counts[word]
print(best, best_count)

Example

def read_text(filename):
    with open(filename) as file:
        return file.read()


def get_words(text):
    return text.lower().split()


def count_words(words):
    counts = {}
    for word in words:
        counts[word] = counts.get(word, 0) + 1
    return counts


def longest_word(words):
    longest = ""
    for word in words:
        if len(word) > len(longest):
            longest = word
    return longest


def most_common_word(counts):
    best = ""
    best_count = 0
    for word in sorted(counts):
        if counts[word] > best_count:
            best = word
            best_count = counts[word]
    return best, best_count


text = read_text("story.txt")
lines = text.strip().split("\n")
words = get_words(text)
counts = count_words(words)
best, best_count = most_common_word(counts)
print("Lines:", len(lines))
print("Words:", len(words))
print("Unique words:", len(counts))
print("Longest word:", longest_word(words))
print("Most common word:", best, "(" + str(best_count) + " times)")

Key points

  • Use open() with with to read the whole story file
  • Use split() to turn text into a list of words
  • Use a dict to count how often each word appears
  • counts.get(word, 0) does the counting lookup, as in the Dictionaries section
  • Use sorted(counts) to visit the words in alphabet order
  • Functions split the counting job into small steps.

In this example, the program reports on the story.txt file from the first snippet. read_text opens the file with with and returns the whole text. The line count comes from cutting the trimmed text at each new line. get_words lowercases the text and splits it into 21 words. count_words builds the counts dict with get, one word at a time. The dict holds 15 keys, so the story has 15 unique words. longest_word keeps morning in the lead, as no word is longer. most_common_word visits the words in alphabet order. It finds the word the at a count of 5.

Note

The alphabet order breaks ties fairly.

The first word with the top count wins the tie.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

Two words both appear 4 times. Which one does most_common_word report?