From 5078146c6a2da24260b68a308aa89828bc41d94d Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 15 Jul 2016 15:35:17 +1200 Subject: Clean up and document syl(6) --- games/syl | 81 ++++++++++++++++++++++++++++++++++++++++++++++++---------- man/man6/syl.6 | 18 +++++++++++++ 2 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 man/man6/syl.6 diff --git a/games/syl b/games/syl index 0580a293..d83187c6 100755 --- a/games/syl +++ b/games/syl @@ -1,18 +1,71 @@ #!/usr/bin/env bash -word=$1 -syls=0 -vs=0 -for ((i=0;i<${#word};i++)); do - if [[ ${word:i:1} == [aeiouy] ]] ; then - ((vs!=1)) && ((syls++)) - ((vs++)) - else - ((vs=0)) - fi + +# Apply some dumb heuristics to guess at the number of syllables in the English +# word given as the sole required argument +word=${1:?Need a word} +lcword=$(printf %s "$word" | tr '[:upper:]' '[:lower:]') + +# Start counting syllables for the word and vowels for the current vowel group +declare -i sylc vowc + +# Iterate through the letters in the word +for ((i = 0; i < ${#word}; i++)); do + case ${word:i:1} in + + # If it's a vowel or a y, we might be adding a syllable. We here + # include all the vowels I got out of /usr/share/dict/words on my + # system. + [aeiouyáâäåèéêíóôöûü]) + + # Bump the number of vowels so far in this group + ((vowc++)) + + # On every odd vowel, we'll add another syllable, so that "e" and + # "ei" are each one syllable, but "eia" and "eiau" are two. + ((vowc % 2)) && ((sylc++)) + ;; + + # If it's not a vowel or a y, reset the vowel count + *) + ((vowc = 0)) + ;; + esac done -case $word in - *[aeiou][^aeiou]e|*[lc]ed) - ((syls > 1)) && ((syls--)) + +# As a special case, if the word ends with a consonant and then "e" and has +# more than one syllable, subtract one syllable as it's probably a silent "e" +if ((sylc > 1)) ; then + case $lcword in + + # Exceptions first + *[bcdgptx]le|*c[mn]e|*phe) + ;; + *[!aeiouy]e) + ((sylc--)) + ;; + + # "pined", "loved", but not "wasted", "devoted" + *[bcdgptx]led) + ;; + *[!aeiotuy]ed) + ((sylc--)) + ;; + + # Plural forms of the above + *[bc]les|*c[mn]es|*phes) + ;; + *[!aeiousy]es) + ((sylc--)) + ;; + esac +fi + +# Add a syllable for an "ism" suffix +case $lcword in + *ism|*isms) + ((sylc++)) ;; esac -printf '%u\n' "$syls" + +# Print the determined syllable count +printf '%u\n' "$sylc" diff --git a/man/man6/syl.6 b/man/man6/syl.6 new file mode 100644 index 00000000..98b90e11 --- /dev/null +++ b/man/man6/syl.6 @@ -0,0 +1,18 @@ +.TH SYL 6 "June 2016" "Manual page for syl" +.SH NAME +.B syl +\- make a crude guess as to the number of syllables of a word +.SH USAGE +.B syl +syllable +.br +.B syl +canapé +.SH DESCRIPTION +.B syl +applies some very crude heuristics to guess at the number of syllables that +would be used if the Latin-alphabet word on the command line were spoken out +loud. It is far too inaccurate for any serious use, particularly for languages +other than English; the author uses it for poetry generation. +.SH AUTHOR +Tom Ryder -- cgit v1.2.3