rhowaldt

detect_syllables_bash_v0.33

Dec 9th, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.78 KB | None | 0 0
  1. #!/bin/bash
  2. # script to detect syllables in a word
  3. # count the vowels in the word.
  4. # 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)
  5. # subtract one vowel from every diphthong (diphthongs only count as one vowel sound.)
  6. # the number of vowels sounds left is the same as the number of syllables.
  7. # usage: detect_syllables [word] ([word] [word] ...)
  8.  
  9. # exit when no argument is given
  10. if [ $# -lt 1 ]; then
  11.     echo "$(basename $0): no argument given." >&2
  12.     exit 1
  13. fi
  14. # continue if there is an argument
  15. full_count=""
  16. for arg in "$@"; do
  17. # convert to lowercase
  18. word="$( tr [:upper:] [:lower:] <<< $arg )"
  19.  
  20. # cleanup of the word for syllable-matching
  21. # '!' special character, translates into a single vowel.
  22. # '-' special character, splits up words, translates to nothing.
  23. clean=$(
  24.    sed '
  25. ####### --- PREREQUISITES
  26. ####### stuff which needs to be done beforehand.
  27.      s/\(.*\)/\1-/
  28.      # A1. append a - at the end of every word, to denote its ending
  29.      s/^..$/!/
  30.      # A2. 2-letter words are 1 vowel
  31.  
  32. ####### --- STARTERS
  33. ####### catching common beginnings of composite words, splitting them up.
  34.      s/^reu/re-u/
  35.      # S1. starting with reu-: split into 2 vowels
  36.      s/^coor/co-or/
  37.      # S2a. starting with coor-: split into 2 vowels
  38.      s/^cooperat/co-operat/
  39.      # S2b. starting with cooperat-: split into 2 vowels
  40.      s/^\(sem\)i\|^\(ant\)i\|^\(mult\)i/\1\2\3i-/
  41.      # S3. starting with anti-/multi-: split
  42.      s/^\(aut\)o\|^\(psych\)o\|^\(pseud\)o/\1\2\3o-/
  43.      # S4a. starting with auto-/psycho-/pseudo-: split
  44.      s/^\(macr\)o\|^\(micr\)o\|^\(neur\)o\([^i]\)/\1\2\3o-\4/
  45.      # S4b. starting with macro-/micro-/neuro-: split
  46.      s/^\(phot\)o\|^\(aer\)o\|^\(therm\)o/\1\2\3o-/
  47.      # S4c. starting with photo-/aero-/thermo-: split
  48.      s/^\(retr\)o\|^\(hom\)o\|^\(heter\)o/\1\2\3o-/
  49.      # S4d. starting with retro-/homo-/hetero-: split
  50.      s/^\(ferr\)o\|^\(gastr\)o\|^\(hydr\)o/\1\2\3o-/
  51.      # S4e. starting with ferro-/gastro-/hydro-: split
  52.      s/^\(rad\)io/\1!o-/
  53.      # S4f. starting with radio-: make io safe for dipthong rules, split
  54.            
  55. ####### --- COMPOSITE RULES
  56. ####### split off certain composite words,
  57. ####### to separate them and allow for other rules to work regardless
  58. ####### (no fake diphthongs) -- silent e removal has to work solo,
  59. ####### not through diphthong-removal; ageing > age-ing    
  60.      s/i\(ng[s]\?\)/-i\1/g
  61.      # C1a. -ing(s): split off
  62.      s/i\(sh\)$\|i\(sm[s]\?\)$\|i\(st[s]\?\)$/-i\1\2\3/g
  63.      # C1b. -ish,-ism(s),-ist(s): split off
  64. # !Exc: hoist, waist      
  65.      s/i\(c[s]\?\)$/-i\1/
  66.      # C1c. -ic(s): split off
  67.      s/\([^q][aeiou]\)ity/\1-it!/g
  68.      # C1d. -ity: split off
  69.      s/\([^q][aeiou]\)ities/\1-it!s/g
  70.      # C1e. -ities: split off
  71.      s/able/-abl!/
  72.      # C2. -able: make 2 vowels, split off
  73. # !Exc: equable
  74.      s/ful/-ful/
  75.      # C3. -ful: split off
  76.      s/\([ai]c\)ally/\1-ll!/
  77.      # C4a. -ically: make 1 vowel, split
  78.      s/ly/-ly/
  79.      # C4b. -ly: split off
  80.      s/ment/-ment/g
  81.      # C5. -ment: split off
  82.      
  83. ####### --- ENDINGS
  84.      s/ion[s]\?-/!n-/
  85.      # E1a. ending in -ion(s): 1 vowel
  86.      s/ioned-/!nd-/
  87.      # E1b. ending in -ioned: 1 vowel
  88. # !Exc: dandelion, lion, scion, ion, pion, axion (consider D.-rules as well)
  89.  
  90.      s/\([auoiey]\)re\([s]\)\?-/\1r\2-/
  91.      # E2a. ending in -re(s): 1 vowel
  92.      s/\([dfkmnptvw]\)e\([s]\?\)-/\1\2-/
  93.      # E2b. ending in -[dfkmnptvw]e(s): 0 vowels
  94.      s/\([cghsxz]\)e-/\1-/
  95.      # E2c. ending in -[cghsxz]e: 0 vowels
  96.      # E2c. ending in -[cghsxz]es: 1 vowel
  97.      s/ye-/!-/
  98.      # E2d. ending in -ye: 1 vowel      
  99.      s/\([aeiou]\)le-\|\([aeiou]\)be-/\1-/
  100.      # E2e. ending in (vowel) -le/-be: 0 vowels
  101.      # E2e. ending in (consonant) -le/-be: 1 vowel
  102. # !Exc: gimme, recipe, coyote
  103. # !Exc: fiance, blase, *aches
  104. # !Exc: eye
  105.      s/\([^bcdfgknpstxz]\)led-/\1ld-/
  106.      # E3a. NOT ending in -[bcdfgkpstxz]led: 0 vowels
  107.      s/\(.*[^edtl]\)ed-/\1d-/
  108.      # E3b. ending in -ed: 0 vowels
  109.      s/\([aeiou]\)yer-/\1!-/
  110.      # E3c. ending in -yer: 1 vowel (y because of rule X1.)
  111.      s/[uio]er-/!!-/
  112.      # E3d. ending in -[uio]er: 2 vowels
  113. # !Exc: embed, seabed, flatbed, roadbed, sickbed, deathbed, slugabed, waterbed, flowerbed
  114. # !Exc: unfed, malfed, overfed, underfed, breastfed
  115. # !Exc:) unshed, cowshed, -thed
  116. # !Exc: naked
  117. # !Exc: -[gq]uer, cashier, pier, soldier
  118. # !Exc: tattooer, shampooer (consider D1.)
  119.      s/[ui]ary-/!!r!-/
  120.      # E4a. ending in -[ui]ary: 3 vowels
  121.      s/ie\(st\)-/!!\1-/
  122.      # E4b. ending in -iest: 2 vowels
  123. # !Exc: priest      
  124.      s/aic-/!!c-/
  125.      # E5. ending in -aic: 2 vowels
  126.      s/\([ct]\)ient-/\1!nt-/
  127.      # E6. ending in -[ct]ient: 1 vowel
  128.      s/\(..\{1,\}\)ea-/\1!!-/
  129.      # E7. 3+ letters and ending in -ea: 2 vowels
  130. # !Exc: idea, nausea      
  131.      s/\([gq]\)ue-/\1-/
  132.      # E8. ending in -que: 0 vowels
  133.  
  134.      # --- Y-RULES
  135.      s/\([aeiou]\)y/\1j/g
  136.      # Y1a. cases where y=j
  137.      s/yard/jard/
  138.      # Y1b. yard: y=j
  139.      s/\([bcdfghklmnprstvwxz]\)y/\1!/g
  140.      # Y2. [bcdfghklmnprstvwxz]y: y=i
  141. # !Exc: canyon, lawyer                
  142.  
  143. ####### --- DIPHTHONGS
  144. ####### > 2 letters ([] means done, ** means partly done):
  145. ####### [aa],[ae],[ai],[ao],[au],*ea*,[ee],[ei],[eo],[eu],[ia],[ie],
  146. ####### [ii],[io],[iu],[oa],[oe],[oi],[oo],[ou],[ua],[ue],[ui],[uo],[uu]
  147. ####### > 3 letters:
  148. ####### [aea],[aei],[aeo],[aeu],[aia],[aie],[aio],[aou],[auu],[eae],[eai],
  149. ####### [eau],[eea],[eei],[eia],[eii],[eio],[eoa],[eoe],[eoi],[eoo],[eou],
  150. ####### [iae],[iai],[iau],[ieu],[ioa],[ioe],[ioi],[iou],[oau],[oea],[oei],
  151. ####### [oeu],[oia],[oie],[ooe],[ooi],[oua],[oue],[oui],[uae],[uai],[uea],
  152. ####### [uee],[uei],[ueo],[ueu],[uia],[uie],[uio],[uiu],[uoi]
  153. #######
  154. ####### !!! keep an eye out for 3-letter diphthongs: paranoia
  155. ####### !!! mind the pattern of substitution:
  156. ####### 1st strong/common, 2nd weak/uncommon diphthongs (oi before ia)
  157. #######
  158. ####### any diphthong starting with an o suffers from the co-construct
  159. ####### any diphthong starting with an e suffers from the re-construct
  160.  
  161. ####### (3-letter diphthongs):
  162. ####### ----------------------
  163.      s/\([cgtx]\)[ie]ous/\1!s/
  164.      # D31. [cgtx][ie]ous: 1 vowel
  165.      s/deio/d!!!/
  166.      # D32. deio-: 3 vowels
  167.      s/aeoa/!!!/
  168.      # D33. aeoa: 3 vowels    
  169.      s/geois/g!s/
  170.      # D34a. -geois: 1 vowel
  171.      s/eois/!!!s/
  172.      # D34b. -eois: 3 vowels
  173.      s/ooed/!d/
  174.      # D35. -ooed: 1 vowel
  175.      s/oui\(l\)\|oui\(e\)/!\1\2/
  176.      # D36. -ouil/-ouie: 1 vowel
  177.      s/cuee/c!!/
  178.      # D37. -cuee: 2 vowels    
  179.      s/aei\|aou\|eau\|ieu\|oua\|uae\|uai\|uea\|uee\|ueo\|ueu\|uio\|uoi/!/
  180.      # D38. certain vowel trios: 1 vowel
  181.      s/aea\|aia\|aie\|aio\|aeo\|aeu\|auu\|oeu/!!/
  182.      # D39a. certain vowel trios (a): 2 vowels
  183.      s/eae\|eai\|eea\|eei\|eia\|eii\|eio\|eoa\|eoe\|eoi/!!/
  184.      # D39b. certain vowel trios (e): 2 vowels
  185.      s/iae\|iai\|iau\|ioi/!!/
  186.      # D39c. certain vowel trios (i): 2 vowels
  187.      s/oau\|oea\|oei\|oia\|oie\|ooe\|ooi\|oue\|oui/!!/
  188.      # D39d. certain vowel trios (o): 2 vowels
  189.      s/uei\|uia\|uie\|uiu/!!/
  190.      # D39e. certain vowel trios (u): 2 vowels
  191.      s/eoo\|ioa\|ioe/!!!/
  192.      # D40. certain vowel trios: 3 vowels      
  193.      
  194. ####### (2-letter diphthongs):
  195. ####### ----------------------
  196.      s/\([abcdeflnrtxy]\)ua/\1!!/g
  197.      # D6a. -ua: 2 vowels
  198.      s/sua\([^dgsv]\)/s!!\1/g
  199.      # D6b. -sua: 2 vowels
  200.      s/\([bdfhkmnprsvxz]\)ia/\1!!/g
  201.      # D9a. -ia: 2 vowels
  202.      # (-cia: keep it simple, 1 vowel)
  203.      s/\([iu]\)l\(l\?\)ia\([nr]\)/\1\2\!\3/
  204.      # D9b. -[iu]l(l)ia[nr]: 1 vowel  
  205.      s/lia\([bcdglnrst]\)/l!!\1/
  206.      # D9c. lia[bcdglnrst]: 2 vowels  
  207. # !Exc: plagiarize, giant, valiant, carriage            
  208.      s/\([dlnptv]\)ui/\1!!/g
  209.      # D12. -ui: 2 vowels
  210. # !Exc: nuisance(s), suicide, ruins, incongruities, sluice, conduit      
  211.      s/\([n]\)ea/\1!!/g
  212.      # D13a. -nea: 2 vowels (+ exceptions)
  213.      s/react/r!!ct/
  214.      # D13b. -react: 2 vowels
  215.      
  216. ####### mind E7. as well
  217.  
  218. # !Exc: beneath, inearth, knead, near,
  219. # !Exc: neat, near, sneak, un(earth/easy/eaten), realignment, reaction      
  220.      s/\([bq]\)uo/\1!/g
  221.      # D14. -[bq]uo: 1 vowel
  222.      s/\([lrt]\)uen/\1!!n/g
  223.      # D15. -[lrt]uen: 2 vowels
  224. # !Exc: duet, truer, truest      
  225.      s/rei\([dmn]\)/r!!\1/
  226.      # D16a. rei[dmn]: 2 vowels (re-constructs)
  227.      s/ei\([cf]\)/!!\1/
  228.      # D16b. -ei[cf]: 2 vowels
  229.      # D16b. -ei[bdgklmnprstvz]: 1 vowel    
  230. # !Exc: reissue, albeit      
  231.      s/\(fr\)ien\|\([oa]t\)ien/\1\2!n/
  232.      # D17a. friend, [oa]tien: 1 vowel      
  233.      s/\([bdlmprstv]\)ien/\1!!n/
  234.      # D17b. -[bdlmprstv]ien: 2 vowels
  235.      s/ie\([tz]\)/!!\1/
  236.      # D17c. -ie[tz]: 2 vowels
  237. # !Exc: science      
  238.      s/coa\(ch\)\|coa\(st\)\|coa\([mrt]\)/c!\1\2\3/
  239.      # D18a. coach, coast, coa[mrt]: 1 vowel (co-constructs)
  240.      s/coal/c!l/
  241.      # D18b. -coal: 1 vowel
  242.      s/\([cdjknz]\)oa/\1!!/
  243.      # D18c. -[djknz]oa: 2 vowels
  244. # !Exc: boa, goa, hypoallergenic, coalescence, cocoanut      
  245.      s/coes/c!s/
  246.      # D19a. -coe (co-constructs)
  247.      s/doe\([ds]\)/d!\1/
  248.      # D19b. -doe[ds]: 1 vowel
  249.      s/\([bfhjkmnrtvw]\)oe/\1!/
  250.      # D19c. -[bfhjkmnrtvw]oe: 1 vowel
  251.      s/\([rtz]\)oic/\1!!c/
  252.      # D19d: -[rtz]oic: 2 vowels
  253.      s/\([ltx]\)ion/\1!n/g
  254.      # D7a. [ltx]ion: 1 vowel
  255.      s/vior/v!r/g
  256.      # D7b. vior: 1 vowel
  257.      s/nio\([nr]\)/n!\1/
  258.      # D7c. -nio[nr]: 1 vowel
  259.      s/\([ghsz]\)io/\1!/
  260.      # D7b. [ghsz]io: 1 vowel
  261. # !Exc: lion          
  262.      
  263.      s/aa\|ae\|ai\|au\|ea\|ee\|ei\|eu\|ia\|ie\|oa\|oi\|oo\|ou\|ua\|ue\|ui/!/g
  264.      # D1. certain vowel-pairs: 1 vowel
  265.      # (some of these excepted in earlier rules)
  266.      s/ao\|eo\|ii\|io\|iu\|oe\|uo\|uu/!!/g
  267.      # D2. certain vowel-pairs: 2 vowels
  268. # !Exc: ibogaine, deaminate, heroin      
  269. # !Exc: bluest, truest
  270. # !Exc: cacao, inertia, voiceover, fadeout, hideout
  271. # !Exc: surgeon, bourgeon, curmudgeon, bludgeon, pidgeon, jeopardy,
  272. # !Exc: makeover, strikeout, leopard, timeout, someone, people,
  273. # !Exc: moreover, whereof, whiteout, eyeopener
  274.      
  275. ####### --- SPECIALS        
  276.      s/!/i/g
  277.      # X1. translate special char ! into i
  278.      #s/-//g
  279.      # X2. translate special char - into nothing.          
  280.   ' <<< $word
  281. )
  282.  
  283. # debug:
  284.  echo -n "$clean, "
  285.  
  286. # --- PROBLEM WORDS:
  287. # LSD, DMT (etc) < will currently be counted as 1 vowel
  288.  
  289.  
  290. # count the syllables
  291. syll_count=$(grep -io [aeiou] <<< $clean | wc -w)
  292. # when syllable count returns 0, it must be 1
  293. if [ $syll_count -eq 0 ]; then
  294.     syll_count=1
  295. fi
  296. # add the syllable count to the full count
  297. full_count="$full_count $syll_count"
  298.  
  299. done
  300.  
  301. echo $full_count | tail -c +1
  302. exit 0
  303.  
Advertisement
Add Comment
Please, Sign In to add comment