Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # script to detect syllables in a word
- # count the vowels in the word.
- # subtract any silent vowels, (like the silent e at the end of a word, or the second vowel when two vowels are together in a syllable)
- # subtract one vowel from every diphthong (diphthongs only count as one vowel sound.)
- # the number of vowels sounds left is the same as the number of syllables.
- # usage: detect_syllables [word] ([word] [word] ...)
- # exit when no argument is given
- if [ $# -lt 1 ]; then
- echo "$(basename $0): no argument given." >&2
- exit 1
- fi
- # continue if there is an argument
- full_count=""
- for arg in "$@"; do
- # convert to lowercase
- word="$( tr [:upper:] [:lower:] <<< $arg )"
- # cleanup of the word for syllable-matching
- # '!' special character, translates into a single vowel.
- # '-' special character, splits up words, translates to nothing.
- clean=$(
- sed '
- ####### --- PREREQUISITES
- ####### stuff which needs to be done beforehand.
- s/\(.*\)/\1-/
- # A1. append a - at the end of every word, to denote its ending
- s/^..$/!/
- # A2. 2-letter words are 1 vowel
- ####### --- STARTERS
- ####### catching common beginnings of composite words, splitting them up.
- s/^reu/re-u/
- # S1. starting with reu-: split into 2 vowels
- s/^coor/co-or/
- # S2a. starting with coor-: split into 2 vowels
- s/^cooperat/co-operat/
- # S2b. starting with cooperat-: split into 2 vowels
- s/^\(sem\)i\|^\(ant\)i\|^\(mult\)i/\1\2\3i-/
- # S3. starting with anti-/multi-: split
- s/^\(aut\)o\|^\(psych\)o\|^\(pseud\)o/\1\2\3o-/
- # S4a. starting with auto-/psycho-/pseudo-: split
- s/^\(macr\)o\|^\(micr\)o\|^\(neur\)o\([^i]\)/\1\2\3o-\4/
- # S4b. starting with macro-/micro-/neuro-: split
- s/^\(phot\)o\|^\(aer\)o\|^\(therm\)o/\1\2\3o-/
- # S4c. starting with photo-/aero-/thermo-: split
- s/^\(retr\)o\|^\(hom\)o\|^\(heter\)o/\1\2\3o-/
- # S4d. starting with retro-/homo-/hetero-: split
- s/^\(ferr\)o\|^\(gastr\)o\|^\(hydr\)o/\1\2\3o-/
- # S4e. starting with ferro-/gastro-/hydro-: split
- s/^\(rad\)io/\1!o-/
- # S4f. starting with radio-: make io safe for dipthong rules, split
- ####### --- COMPOSITE RULES
- ####### split off certain composite words,
- ####### to separate them and allow for other rules to work regardless
- ####### (no fake diphthongs) -- silent e removal has to work solo,
- ####### not through diphthong-removal; ageing > age-ing
- s/i\(ng[s]\?\)/-i\1/g
- # C1a. -ing(s): split off
- s/i\(sh\)$\|i\(sm[s]\?\)$\|i\(st[s]\?\)$/-i\1\2\3/g
- # C1b. -ish,-ism(s),-ist(s): split off
- # !Exc: hoist, waist
- s/i\(c[s]\?\)$/-i\1/
- # C1c. -ic(s): split off
- s/\([^q][aeiou]\)ity/\1-it!/g
- # C1d. -ity: split off
- s/\([^q][aeiou]\)ities/\1-it!s/g
- # C1e. -ities: split off
- s/able/-abl!/
- # C2. -able: make 2 vowels, split off
- # !Exc: equable
- s/ful/-ful/
- # C3. -ful: split off
- s/\([ai]c\)ally/\1-ll!/
- # C4a. -ically: make 1 vowel, split
- s/ly/-ly/
- # C4b. -ly: split off
- s/ment/-ment/g
- # C5. -ment: split off
- ####### --- ENDINGS
- s/ion[s]\?-/!n-/
- # E1a. ending in -ion(s): 1 vowel
- s/ioned-/!nd-/
- # E1b. ending in -ioned: 1 vowel
- # !Exc: dandelion, lion, scion, ion, pion, axion (consider D.-rules as well)
- s/\([auoiey]\)re\([s]\)\?-/\1r\2-/
- # E2a. ending in -re(s): 1 vowel
- s/\([dfkmnptvw]\)e\([s]\?\)-/\1\2-/
- # E2b. ending in -[dfkmnptvw]e(s): 0 vowels
- s/\([cghsxz]\)e-/\1-/
- # E2c. ending in -[cghsxz]e: 0 vowels
- # E2c. ending in -[cghsxz]es: 1 vowel
- s/ye-/!-/
- # E2d. ending in -ye: 1 vowel
- s/\([aeiou]\)le-\|\([aeiou]\)be-/\1-/
- # E2e. ending in (vowel) -le/-be: 0 vowels
- # E2e. ending in (consonant) -le/-be: 1 vowel
- # !Exc: gimme, recipe, coyote
- # !Exc: fiance, blase, *aches
- # !Exc: eye
- s/\([^bcdfgknpstxz]\)led-/\1ld-/
- # E3a. NOT ending in -[bcdfgkpstxz]led: 0 vowels
- s/\(.*[^edtl]\)ed-/\1d-/
- # E3b. ending in -ed: 0 vowels
- s/\([aeiou]\)yer-/\1!-/
- # E3c. ending in -yer: 1 vowel (y because of rule X1.)
- s/[uio]er-/!!-/
- # E3d. ending in -[uio]er: 2 vowels
- # !Exc: embed, seabed, flatbed, roadbed, sickbed, deathbed, slugabed, waterbed, flowerbed
- # !Exc: unfed, malfed, overfed, underfed, breastfed
- # !Exc:) unshed, cowshed, -thed
- # !Exc: naked
- # !Exc: -[gq]uer, cashier, pier, soldier
- # !Exc: tattooer, shampooer (consider D1.)
- s/[ui]ary-/!!r!-/
- # E4a. ending in -[ui]ary: 3 vowels
- s/ie\(st\)-/!!\1-/
- # E4b. ending in -iest: 2 vowels
- # !Exc: priest
- s/aic-/!!c-/
- # E5. ending in -aic: 2 vowels
- s/\([ct]\)ient-/\1!nt-/
- # E6. ending in -[ct]ient: 1 vowel
- s/\(..\{1,\}\)ea-/\1!!-/
- # E7. 3+ letters and ending in -ea: 2 vowels
- # !Exc: idea, nausea
- s/\([gq]\)ue-/\1-/
- # E8. ending in -que: 0 vowels
- # --- Y-RULES
- s/\([aeiou]\)y/\1j/g
- # Y1a. cases where y=j
- s/yard/jard/
- # Y1b. yard: y=j
- s/\([bcdfghklmnprstvwxz]\)y/\1!/g
- # Y2. [bcdfghklmnprstvwxz]y: y=i
- # !Exc: canyon, lawyer
- ####### --- DIPHTHONGS
- ####### > 2 letters ([] means done, ** means partly done):
- ####### [aa],[ae],[ai],[ao],[au],*ea*,[ee],[ei],[eo],[eu],[ia],[ie],
- ####### [ii],[io],[iu],[oa],[oe],[oi],[oo],[ou],[ua],[ue],[ui],[uo],[uu]
- ####### > 3 letters:
- ####### [aea],[aei],[aeo],[aeu],[aia],[aie],[aio],[aou],[auu],[eae],[eai],
- ####### [eau],[eea],[eei],[eia],[eii],[eio],[eoa],[eoe],[eoi],[eoo],[eou],
- ####### [iae],[iai],[iau],[ieu],[ioa],[ioe],[ioi],[iou],[oau],[oea],[oei],
- ####### [oeu],[oia],[oie],[ooe],[ooi],[oua],[oue],[oui],[uae],[uai],[uea],
- ####### [uee],[uei],[ueo],[ueu],[uia],[uie],[uio],[uiu],[uoi]
- #######
- ####### !!! keep an eye out for 3-letter diphthongs: paranoia
- ####### !!! mind the pattern of substitution:
- ####### 1st strong/common, 2nd weak/uncommon diphthongs (oi before ia)
- #######
- ####### any diphthong starting with an o suffers from the co-construct
- ####### any diphthong starting with an e suffers from the re-construct
- ####### (3-letter diphthongs):
- ####### ----------------------
- s/\([cgtx]\)[ie]ous/\1!s/
- # D31. [cgtx][ie]ous: 1 vowel
- s/deio/d!!!/
- # D32. deio-: 3 vowels
- s/aeoa/!!!/
- # D33. aeoa: 3 vowels
- s/geois/g!s/
- # D34a. -geois: 1 vowel
- s/eois/!!!s/
- # D34b. -eois: 3 vowels
- s/ooed/!d/
- # D35. -ooed: 1 vowel
- s/oui\(l\)\|oui\(e\)/!\1\2/
- # D36. -ouil/-ouie: 1 vowel
- s/cuee/c!!/
- # D37. -cuee: 2 vowels
- s/aei\|aou\|eau\|ieu\|oua\|uae\|uai\|uea\|uee\|ueo\|ueu\|uio\|uoi/!/
- # D38. certain vowel trios: 1 vowel
- s/aea\|aia\|aie\|aio\|aeo\|aeu\|auu\|oeu/!!/
- # D39a. certain vowel trios (a): 2 vowels
- s/eae\|eai\|eea\|eei\|eia\|eii\|eio\|eoa\|eoe\|eoi/!!/
- # D39b. certain vowel trios (e): 2 vowels
- s/iae\|iai\|iau\|ioi/!!/
- # D39c. certain vowel trios (i): 2 vowels
- s/oau\|oea\|oei\|oia\|oie\|ooe\|ooi\|oue\|oui/!!/
- # D39d. certain vowel trios (o): 2 vowels
- s/uei\|uia\|uie\|uiu/!!/
- # D39e. certain vowel trios (u): 2 vowels
- s/eoo\|ioa\|ioe/!!!/
- # D40. certain vowel trios: 3 vowels
- ####### (2-letter diphthongs):
- ####### ----------------------
- s/\([abcdeflnrtxy]\)ua/\1!!/g
- # D6a. -ua: 2 vowels
- s/sua\([^dgsv]\)/s!!\1/g
- # D6b. -sua: 2 vowels
- s/\([bdfhkmnprsvxz]\)ia/\1!!/g
- # D9a. -ia: 2 vowels
- # (-cia: keep it simple, 1 vowel)
- s/\([iu]\)l\(l\?\)ia\([nr]\)/\1\2\!\3/
- # D9b. -[iu]l(l)ia[nr]: 1 vowel
- s/lia\([bcdglnrst]\)/l!!\1/
- # D9c. lia[bcdglnrst]: 2 vowels
- # !Exc: plagiarize, giant, valiant, carriage
- s/\([dlnptv]\)ui/\1!!/g
- # D12. -ui: 2 vowels
- # !Exc: nuisance(s), suicide, ruins, incongruities, sluice, conduit
- s/\([n]\)ea/\1!!/g
- # D13a. -nea: 2 vowels (+ exceptions)
- s/react/r!!ct/
- # D13b. -react: 2 vowels
- ####### mind E7. as well
- # !Exc: beneath, inearth, knead, near,
- # !Exc: neat, near, sneak, un(earth/easy/eaten), realignment, reaction
- s/\([bq]\)uo/\1!/g
- # D14. -[bq]uo: 1 vowel
- s/\([lrt]\)uen/\1!!n/g
- # D15. -[lrt]uen: 2 vowels
- # !Exc: duet, truer, truest
- s/rei\([dmn]\)/r!!\1/
- # D16a. rei[dmn]: 2 vowels (re-constructs)
- s/ei\([cf]\)/!!\1/
- # D16b. -ei[cf]: 2 vowels
- # D16b. -ei[bdgklmnprstvz]: 1 vowel
- # !Exc: reissue, albeit
- s/\(fr\)ien\|\([oa]t\)ien/\1\2!n/
- # D17a. friend, [oa]tien: 1 vowel
- s/\([bdlmprstv]\)ien/\1!!n/
- # D17b. -[bdlmprstv]ien: 2 vowels
- s/ie\([tz]\)/!!\1/
- # D17c. -ie[tz]: 2 vowels
- # !Exc: science
- s/coa\(ch\)\|coa\(st\)\|coa\([mrt]\)/c!\1\2\3/
- # D18a. coach, coast, coa[mrt]: 1 vowel (co-constructs)
- s/coal/c!l/
- # D18b. -coal: 1 vowel
- s/\([cdjknz]\)oa/\1!!/
- # D18c. -[djknz]oa: 2 vowels
- # !Exc: boa, goa, hypoallergenic, coalescence, cocoanut
- s/coes/c!s/
- # D19a. -coe (co-constructs)
- s/doe\([ds]\)/d!\1/
- # D19b. -doe[ds]: 1 vowel
- s/\([bfhjkmnrtvw]\)oe/\1!/
- # D19c. -[bfhjkmnrtvw]oe: 1 vowel
- s/\([rtz]\)oic/\1!!c/
- # D19d: -[rtz]oic: 2 vowels
- s/\([ltx]\)ion/\1!n/g
- # D7a. [ltx]ion: 1 vowel
- s/vior/v!r/g
- # D7b. vior: 1 vowel
- s/nio\([nr]\)/n!\1/
- # D7c. -nio[nr]: 1 vowel
- s/\([ghsz]\)io/\1!/
- # D7b. [ghsz]io: 1 vowel
- # !Exc: lion
- s/aa\|ae\|ai\|au\|ea\|ee\|ei\|eu\|ia\|ie\|oa\|oi\|oo\|ou\|ua\|ue\|ui/!/g
- # D1. certain vowel-pairs: 1 vowel
- # (some of these excepted in earlier rules)
- s/ao\|eo\|ii\|io\|iu\|oe\|uo\|uu/!!/g
- # D2. certain vowel-pairs: 2 vowels
- # !Exc: ibogaine, deaminate, heroin
- # !Exc: bluest, truest
- # !Exc: cacao, inertia, voiceover, fadeout, hideout
- # !Exc: surgeon, bourgeon, curmudgeon, bludgeon, pidgeon, jeopardy,
- # !Exc: makeover, strikeout, leopard, timeout, someone, people,
- # !Exc: moreover, whereof, whiteout, eyeopener
- ####### --- SPECIALS
- s/!/i/g
- # X1. translate special char ! into i
- #s/-//g
- # X2. translate special char - into nothing.
- ' <<< $word
- )
- # debug:
- echo -n "$clean, "
- # --- PROBLEM WORDS:
- # LSD, DMT (etc) < will currently be counted as 1 vowel
- # count the syllables
- syll_count=$(grep -io [aeiou] <<< $clean | wc -w)
- # when syllable count returns 0, it must be 1
- if [ $syll_count -eq 0 ]; then
- syll_count=1
- fi
- # add the syllable count to the full count
- full_count="$full_count $syll_count"
- done
- echo $full_count | tail -c +1
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment