Lab - Haiku Checker
Background¶
Haikus are a form of Japanese poetry. They consist of 3 lines of text. The first line of the haiku must contain 5 syllables. The second line must have 7 syllables. Finally, the third line again contains 5 syllables.
Here is an example of a valid haiku:
An old silent pond
A frog jumps into the pond—
Splash! Silence again.
by Matsuo Basho
Objectives¶
- Practice conditionals
- Practice functions
- Practice using a helper function
- Practice loops
- More practice with lists and strings
Program the Solution¶
Start with the file called haiku_checker.py
, which contains two functions for you to write. The docstrings are completed for you so that you can focus on solving the problem. The functions both return an incorrect result so that the test in the main will run - you must replace the function bodies with the correct code, as described below.
The first function will be a helper function that is called check_n_syllables
. This function must take input of a list of words on a line and an int number of syllables to validate. You should use the pysyllables library to make this easier. You will need to install the pysyllables
package through the Thonny package manager. Make sure you also do an import at the top of your file:
from pysyllables import get_syllable_count
You can then use the get_syllable_count(word)
function from pysyllables
to get the number of syllables in a word. Note that it returns None
if you pass it an unknown word, so either test with real words, or, if you want to, handle this condition (return -1 if an unknown word is encountered).
The return value of the check_n_syllables()
function should be as follows:
- This function should return either -1 if the total syllables is less than the given number n to validate against,
- or 0 if the total syllables is equal to the number n,
- or 1 if the total syllables is greater than n.
The second function should be called validate_haiku
. It will take a single parameter of a list of lists. The list will contain 3 lines for the haiku. Each line will be a sub list of the words in the line. The function will return a String value.
The return values should follow these rules:
- It should be set to
"Valid haiku"
if this is a valid haiku that has a first line with 5 syllables, second line with 7 syllables, and third line with 5 syllables. - If it has more or less than 3 lines it should return:
"Invalid haiku, not 3 lines."
. - Otherwise it will return a message with the type of error for each line. For example: if the first line has 7 syllables it would return:
"Line 1 too long. "
[note the space after the period]. If the second line has 3 syllables it would return"Line 2 too short. "
. If the third line is too long it would return:"Line 3 too long. "
.
However, your program should allow multiple error messages, 1 for each line that is wrong. If there are line errors, all the error messages should be in a single string to be returned. There is no message for a line being valid. Note: Only return one continuous String with all error messages as the final return String.
Submission
Submit your program called haiku_checker.py
into gradescope.
Acknowledgements
This lab was developed by Prof. Alvin Chao.