line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Lingua::EN::Syllable; |
2
|
|
|
|
|
|
|
$Lingua::EN::Syllable::VERSION = '0.31'; |
3
|
|
|
|
|
|
|
# ABSTRACT: count the number of syllables in English words |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
707
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
6
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
16
|
|
7
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
405
|
|
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
|
|
|
|
|
|
|
require Exporter; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our @ISA = qw/ Exporter /; |
16
|
|
|
|
|
|
|
our @EXPORT = qw/ syllable /; |
17
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ @AddSyl @SubSyl /; |
18
|
|
|
|
|
|
|
our @AddSyl; |
19
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
sub syllable { |
71
|
31
|
|
|
31
|
0
|
13026
|
my $word = shift; |
72
|
31
|
|
|
|
|
42
|
my(@scrugg,$syl); |
73
|
|
|
|
|
|
|
|
74
|
31
|
|
|
|
|
59
|
$word =~ tr/A-Z/a-z/; |
75
|
31
|
100
|
|
|
|
73
|
return 2 if $word eq 'w'; |
76
|
30
|
100
|
|
|
|
56
|
return 1 if length($word) == 1; |
77
|
28
|
|
|
|
|
54
|
$word =~ s/\'//g; # fold contractions. not very effective. |
78
|
28
|
|
|
|
|
35
|
$word =~ s/e$//; |
79
|
28
|
|
|
|
|
142
|
@scrugg = split(/[^aeiouy]+/, $word); # '-' should perhaps be added? |
80
|
28
|
100
|
|
|
|
58
|
shift(@scrugg) unless ($scrugg[0]); |
81
|
28
|
|
|
|
|
39
|
$syl = 0; |
82
|
|
|
|
|
|
|
# special cases |
83
|
28
|
|
|
|
|
41
|
foreach (@SubSyl) { |
84
|
280
|
100
|
|
|
|
1780
|
$syl-- if $word=~/$_/; |
85
|
|
|
|
|
|
|
} |
86
|
28
|
|
|
|
|
52
|
foreach (@AddSyl) { |
87
|
476
|
100
|
|
|
|
3352
|
$syl++ if $word=~/$_/; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
# count vowel groupings |
90
|
28
|
|
|
|
|
49
|
$syl += scalar(@scrugg); |
91
|
28
|
100
|
|
|
|
45
|
$syl=1 if $syl==0; # got no vowels? ("the", "crwth") |
92
|
28
|
|
|
|
|
61
|
return $syl; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
# syllable |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
1; |
98
|
|
|
|
|
|
|
__END__ |