TLP - Programming Maintenance
Week 5 - Estimating syllables in a word
Background
If you have taught elementary school or have had kids go through one recently, you are probably familiar with AR scores. These are the estimates for what grade level a book is targeted for. For example:
- Green Eggs and Ham is a 1.5
- Henry and Beezus is a 4.6
- Harry Potter and the Chamber of Secrets is a 6.7
- The Great Gatsby is a 7.3
- Moby Dick is a 10.3
The first number represents the grade level and the second number represents how far into the school year the book is appropriate. So "Green Eggs and Ham would be appropriate halfway through first grade and Moby Dick is appropriate towards the start of 10th grade.
We aren't ready to calculate that full score (we will attempt that in week 14). But this week we can take the first part - estimating how many syllables are in a single word.
While this is complicated in theory, there is actually a technique that is fairly accurate:
- Count the number of vowels or vowel "clusters" in the word (including y).
- In other words, a vowel on it's own is 1 and the first vowel in a string of vowels counts as 1
- The other way to look at this is you should NOT count a vowel if it comes after another vowel
- Do not count an āeā at the end of a word
- Do not return a score of 0. Always return at least a 1.
Task
- Write a function called syllables()
- Input
- Takes in one string assumed to be a single word
- Output
- Returns an integer estimating how many syllables are in the word using the formula above
- Here are several examples that use the first rule:
- Here are several examples that use the second and third rule
- Notice that "unite" and "syllable" both only score 2 syllables because we don't count the 'e' at the end
- Yes, I am aware that syllable is 3 syllables not 2. That is just one of the few times this formula is wrong
- Notice that "we" SHOULD be 0 syllables (because we don't count the e at the end) but we can't return a 0 so it was changed to 1.
- Notice that "unite" and "syllable" both only score 2 syllables because we don't count the 'e' at the end
Hints
- Don't tackle all four ideas at once (rule 1 has two ideas). Break it down one idea at a time and test it well as you go.
- Rule 1b - words with multiple vowels in a row like "overlook" and "beautiful" are the hardest part to get. There are several ways to do this and each has a gotcha you have to watch for.