Course 4.1Lesson 5 of 5
In this project we build a text toolkit: small functions for messy text that each do one job, and a table of tests that runs them all and shows PASS or FAIL. The functions give the tidy words of a text, count the words, find the most common word with a rule for ties, write a name in title case, and shift a text for a Caesar code. We plan the expected answers by hand, build the functions from the tools of the course, run the table of tests, and extend the toolkit with a palindrome test, a word frequency report and a code breaker.
Try the code and the simulations beside the lesson.
After this lesson you can
You will clean, count and change text often. Instead of writing the same loops again, make a toolkit: small functions, each with one job, that you can use in any program. And a table of tests, so that you know that each one is right.
The tools:
tidy_words(text): the words of a text, in small letters, with no punctuation.word_counts(text): a dictionary of word and count.most_common(text): the most common word. When two words tie, the one that comes first in the alphabet.title_case(name): a name with a capital at the start of each word, and one space between the words.shift_text(text, shift): the Caesar shift.Work them out by hand.
| Tool | Input | Expected |
|---|---|---|
tidy_words | "The Cat, the HAT!" | ["the", "cat", "the", "hat"] |
word_counts | "a b a" | {"a": 2, "b": 1} |
most_common | "the cat and the hat and the bat" | "the" |
most_common (a tie) | "b a" | "a" |
title_case | " aSHA rao " | "Asha Rao" |
shift_text | "abc xyz" with 3 | "def abc" |
tidy_words (empty) | "" | [] |
A table of tests is only useful if it catches mistakes. Try these.
if best is None or counts[word] > counts[best] to >=. The tie test fails, because the later word now wins.letter == " " from tidy_words. All the words stick together, and several tests fail.% 26. The test with xyz fails.After each, put the code back. This is the habit: write the tests, then change the code without fear.
This is why the toolkit cleans the text first. Every count after that treats A and a as one letter.
Pick one or two, and do them without looking at a solution. Add each to the table, with the expected answer worked out by hand first.
is_palindrome(text), ignoring capitals, spaces and marks.top_words(text, 3): the three most common words, with their counts, the most common first.crack(secret): guess the shift from the most common letter, as in the next course.initials("asha kumari rao") gives "AKR".tidy_words, used by the others.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
most_common("b a") give, and why?title_case("aSHA") give Asha?tidy_words keep?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.