Skip to content

Parsing, Analyzing Text

A string is a sequence of characters, and Python has many methods for working with them. In this lab, you will write a program that checks whether a poem follows the rules of a haiku. Along the way, you will loop over the characters of a string to build up a result, and you will call the string methods isalpha(), isspace(), split(), and splitlines(). You will also get more practice with pytest and type hints from the previous two labs: each function in the starter code has type hints, and the provided tests tell you when your code is correct.

You can start before class

This lab takes about an hour, so get a head start by working on Steps 1–2 before class. If you get stuck, that's what class is for; we'll help you get unstuck!

Background

Haiku is a form of Japanese poetry. A haiku consists of 3 lines of text. The first line must have 5 syllables. The second line must have 7 syllables. And the third line must, again, have 5 syllables.

Example haiku

An old silent pond
A frog jumps into the pond—
Splash! Silence again.

by Matsuo Basho

Additional haiku examples can be found online. Not all English translations follow the 5-7-5 rule.

Step 1: Getting Started

Download the following files into your Week08 folder. (Right click each link, and "Save link as…")

Open a terminal in VS Code, and make sure the command line begins with (.venv). Then install pysyllables: pip install pysyllables.

Run pytest on test_haiku.py to make sure everything is set up right. The tests should run and fail, because the functions in haiku.py are not written yet. If you see a ModuleNotFoundError for pysyllables instead, the module was not installed in your venv.

Step 2: remove_symbols

Later in the lab, you will use the get_syllable_count() function in the pysyllables module. (The starter code already imports it at the top of haiku.py.) This function will tell you how many syllables an English word has. However, a word may not include any punctuation or other non-alphabetic characters. For example:

  • get_syllable_count("pond—") returns None
  • get_syllable_count("pond") returns 1
  • get_syllable_count("again.") returns None
  • get_syllable_count("again") returns 2

The remove_symbols() function should remove all characters from a string except for letters and spaces. Start by creating an empty result string. Then loop through the original line, one character at a time. If the character is alphabetic or space, append the character to the result. Use the isalpha() and isspace() string methods. Then return the result.

Keep every space, even when the characters around it are removed. For example, remove_symbols("Try: 1 + 2 = 3") returns "Try " (with five spaces at the end).

Step 3: check_syllables

This helper function will be called from validate_haiku() three times. The first parameter (str) represents one line of the haiku. The second parameter (int) is the required number of syllables. The return value is:

  • "too short" if the number of syllables is less than the number required
  • "valid" if the number of syllables is equal to the number required
  • "too long" if the number of syllables is greater than the number required

Hint: Start by calling remove_symbols() on the line. Then use the split() method to split the string into a list of words. Use a loop to add the syllable count of each word. Then decide which result string to return.

Unknown words

get_syllable_count() also returns None for a word that is not in its dictionary, such as a name or a made-up word. Skip those words (count them as 0 syllables). Adding None to an int would crash your program with a TypeError.

Step 4: validate_haiku

This is the main function of the lab. The parameter is a multiline string representing the haiku. If the string is a valid haiku (based on the definition above), the function should return "Valid haiku." (with a period at the end).

If the string has more or less than three lines, the function should return "Invalid haiku, not 3 lines.". If the string has three lines but is not a valid haiku, the function should return a detailed message about the invalid line(s).

  • The format should be "Line X too Y. ", where X is the line number, and Y is either long or short.
  • Each line should end with a single space after the period. Ex: "Line 1 too short. Line 3 too long. "
  • Include only the invalid lines, in order from line 1 to line 3.

Hint: Use the splitlines() method to split the original string into a list of lines. Then call the check_syllables() function once for each line, with the required number of syllables (5, 7, 5). Store each return value in its own variable: status1 = check_syllables(lines[0], 5). If all three status variables are "valid", return "Valid haiku.". Otherwise, use these variables to build the detailed return message.

Submission

Upload only haiku.py to Gradescope. The autograder runs the provided test_haiku.py.