Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 15

Count word frequencies in a text

Medium

18 minute session

Summary

Parse a text and count how often each word appears, normalizing case and ignoring punctuation.

Problem statement

Given an input string, return a dictionary where keys are words (normalized to lowercase) and values are counts of how many times each word appears. Words should be sequences of alphanumeric characters and underscores (use word boundaries). Punctuation and other non-word characters should be ignored. Preserve the order of first appearance of each distinct word in the text when constructing the resulting dictionary.

Task

Create a function that returns a dictionary mapping words to their occurrence counts in a given text. Treat words case-insensitively and ignore punctuation.

Examples

Simple case-insensitive count

Input

count_word_frequencies('Hello world hello')

Output

{'hello': 2, 'world': 1}

Explanation

The function lowercases words and counts occurrences: 'hello' appears twice, 'world' once. The first appearance order is 'hello', then 'world'.

Input format

A single string containing the text to analyze. Example: "This is a test."

Output format

A dictionary mapping normalized lowercase words to integer counts.

Constraints

- Use only Python standard library. - Treat words as sequences matched by the regex \b\w+\b (letters, digits, underscores). - Return an empty dictionary for empty or punctuation-only strings. - Preserve first-seen word insertion order in the result.

Samples

Sample input 0

count_word_frequencies("It's a test. A test!")

Sample output 0

{'it': 1, 's': 1, 'a': 2, 'test': 2}

Explanation 0

Tokenization splits "It's" into 'it' and 's' using the word regex; all words are lowercased and counted.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

10:47 PM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.