| 1 | package Lingua::EN::Syllable; | ||||||||||
| 2 | 1 | $Lingua::EN::Syllable::VERSION = '0.31'; | |||||||||
| 3 | # ABSTRACT: count the number of syllables in English words | ||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | # note that this is not infallible. it does fail for some percentage of | ||||||||||
| 10 | # words (10% seems a good guess)... so it's useful for approximation, but | ||||||||||
| 11 | # don't use this for running your nuclear reactor... | ||||||||||
| 12 | |||||||||||
| 13 | 1 | require Exporter; | |||||||||
| 14 | |||||||||||
| 15 | 1 | our @ISA = qw/ Exporter /; | |||||||||
| 16 | 1 | our @EXPORT = qw/ syllable /; | |||||||||
| 17 | 1 | our @EXPORT_OK = qw/ @AddSyl @SubSyl /; | |||||||||
| 18 | 1 | our @AddSyl; | |||||||||
| 19 | 1 | our @SubSyl; | |||||||||
| 20 | |||||||||||
| 21 | # basic algortithm: | ||||||||||
| 22 | # each vowel-group indicates a syllable, except for: | ||||||||||
| 23 | # final (silent) e | ||||||||||
| 24 | # 'ia' ind two syl | ||||||||||
| 25 | |||||||||||
| 26 | # @AddSyl and @SubSyl list regexps to massage the basic count. | ||||||||||
| 27 | # Each match from @AddSyl adds 1 to the basic count, each @SubSyl match -1 | ||||||||||
| 28 | # Keep in mind that when the regexps are checked, any final 'e' will have | ||||||||||
| 29 | # been removed, and all '\'' will have been removed. | ||||||||||
| 30 | |||||||||||
| 31 | 1 | @SubSyl = ( | |||||||||
| 32 | 'cial', | ||||||||||
| 33 | 'tia', | ||||||||||
| 34 | 'cius', | ||||||||||
| 35 | 'cious', | ||||||||||
| 36 | 'giu', # belgium! | ||||||||||
| 37 | 'ion', | ||||||||||
| 38 | 'iou', | ||||||||||
| 39 | 'sia$', | ||||||||||
| 40 | '.ely$', # absolutely! (but not ely!) | ||||||||||
| 41 | '[^td]ed$', # accused is 2, but executed is 4 | ||||||||||
| 42 | ); | ||||||||||
| 43 | 1 | @AddSyl = ( | |||||||||
| 44 | 'ia', | ||||||||||
| 45 | 'riet', | ||||||||||
| 46 | 'dien', | ||||||||||
| 47 | 'iu', | ||||||||||
| 48 | 'io', | ||||||||||
| 49 | 'ii', | ||||||||||
| 50 | 'microor', | ||||||||||
| 51 | '[aeiouym]bl$', # -Vble, plus -mble | ||||||||||
| 52 | '[aeiou]{3}', # agreeable | ||||||||||
| 53 | '^mc', | ||||||||||
| 54 | 'ism$', # -ism | ||||||||||
| 55 | 'isms$', # -isms | ||||||||||
| 56 | '([^aeiouy])\1l$', # middle twiddle battle bottle, etc. | ||||||||||
| 57 | '[^l]lien', # alien, salient [1] | ||||||||||
| 58 | '^coa[dglx].', # [2] | ||||||||||
| 59 | '[^gq]ua[^auieo]', # i think this fixes more than it breaks | ||||||||||
| 60 | 'dnt$', # couldn't | ||||||||||
| 61 | ); | ||||||||||
| 62 | |||||||||||
| 63 | # (comments refer to titan's /usr/dict/words) | ||||||||||
| 64 | # [1] alien, salient, but not lien or ebbullient... | ||||||||||
| 65 | # (those are the only 2 exceptions i found, there may be others) | ||||||||||
| 66 | # [2] exception for 7 words: | ||||||||||
| 67 | # coadjutor coagulable coagulate coalesce coalescent coalition coaxial | ||||||||||
| 68 | |||||||||||
| 69 | #---------------------------------------- | ||||||||||
| 70 | 31 | ▶ | sub syllable { | ||||||||
|
Subroutine
syllable: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||
| 71 | 31 | my $word = shift; | |||||||||
| 72 | 31 | my(@scrugg,$syl); | |||||||||
| 73 | |||||||||||
| 74 | 31 | $word =~ tr/A-Z/a-z/; | |||||||||
| 75 | 31 | ▶ | return 2 if $word eq 'w'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 30 | ▶ | return 1 if length($word) == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 28 | $word =~ s/\'//g; # fold contractions. not very effective. | |||||||||
| 78 | 28 | $word =~ s/e$//; | |||||||||
| 79 | 28 | @scrugg = split(/[^aeiouy]+/, $word); # '-' should perhaps be added? | |||||||||
| 80 | 28 | ▶ | shift(@scrugg) unless ($scrugg[0]); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 28 | $syl = 0; | |||||||||
| 82 | # special cases | ||||||||||
| 83 | 28 | foreach (@SubSyl) { | |||||||||
| 84 | 280 | ▶ | $syl-- if $word=~/$_/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | } | ||||||||||
| 86 | 28 | foreach (@AddSyl) { | |||||||||
| 87 | 476 | ▶ | $syl++ if $word=~/$_/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | } | ||||||||||
| 89 | # count vowel groupings | ||||||||||
| 90 | 28 | $syl += scalar(@scrugg); | |||||||||
| 91 | 28 | ▶ | $syl=1 if $syl==0; # got no vowels? ("the", "crwth") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 28 | return $syl; | |||||||||
| 93 | } | ||||||||||
| 94 | # syllable | ||||||||||
| 95 | |||||||||||
| 96 | |||||||||||
| 97 | 1 | 1; | |||||||||
| 98 | __END__ | ||||||||||