File Coverage

blib/lib/Lingua/NO/Syllable.pm
Criterion Covered Total %
statement 31 32 96.8
branch 4 4 100.0
condition 1 2 50.0
subroutine 6 7 85.7
pod 0 2 0.0
total 42 47 89.3


line stmt bran cond sub pod time code
1             package Lingua::NO::Syllable;
2 2     2   27731 use strict;
  2         3  
  2         48  
3 2     2   7 use warnings;
  2         3  
  2         40  
4 2     2   638 use utf8;
  2         13  
  2         8  
5 2     2   44 use feature 'unicode_strings';
  2         0  
  2         174  
6              
7             require Exporter;
8              
9 2     2   948 use Unicode::Normalize;
  2         3000  
  2         617  
10              
11             our @ISA = qw/ Exporter /;
12             our @EXPORT = qw/ syllable syllables /;
13              
14             our @VOWELS = qw( a e i o u y æ ø å );
15              
16             =encoding utf-8
17              
18             =head1 NAME
19              
20             Lingua::NO::Syllable - Count the number of syllables in Norwegian words.
21              
22             =head1 VERSION
23              
24             Version 0.06.
25              
26             =cut
27              
28             our $VERSION = '0.06';
29              
30             =head1 SYNOPSIS
31              
32             use Lingua::NO::Syllable;
33              
34             my $count = syllables( 'Tyrannosaurus' ); # 5, because:
35             # Tyr-ann-o-sau-rus
36              
37             =head1 DESCRIPTION
38              
39             C estimates the number of syllables in
40             the C<$word> passed to it. It's an estimate, because the algorithm is quick
41             and dirty, non-alpha characters aren't considered etc. Don't expect this
42             module to give you a 100% correct answer.
43              
44             As the Norwegian and the Danish languages are quite similar, at least written,
45             this module might work for the Danish language as well.
46              
47             =cut
48              
49             sub syllable {
50 0     0 0 0 return syllables( @_ );
51             }
52              
53             sub syllables {
54 17   50 17 0 6188 my $word = shift // '';
55              
56             # Remove surrounding spaces.
57 17         35 $word =~ s/^\s+//;
58 17         32 $word =~ s/\s+$//;
59              
60             # If the word is one character long, consider it to contain only one
61             # syllable.
62 17 100       36 return 1 if ( length($word) == 1 );
63              
64             # Lowercase the word.
65 16         30 $word = lc( $word );
66              
67             # Normalize the word, i.e. convert accented characters to their normalized
68             # representation.
69 16         75 $word = NFD( $word );
70              
71             # Create an array of the word's characters.
72 16         47 my @chars = split( //, $word );
73              
74             # The basic rule is that the number of syllables in a word equals the
75             # number of vowels.
76 16         14 my $syllables = 0;
77              
78 16         20 foreach my $vowel ( @VOWELS ) {
79 144         113 foreach my $char ( @chars ) {
80 1017 100       1215 if ( $vowel eq $char ) {
81 52         34 $syllables++;
82             }
83             }
84             }
85              
86             # Certain vowel combinations doesn't classify as "syllable separator".
87 16         24 foreach ( qw(ai au ay ei eu ey oi ou oy øi øu øy æi æu æy åi åu åy) ) {
88 288         1209 my $occurences = $word =~ m/$_/g;
89 288         289 $syllables -= $occurences;
90             }
91              
92             # Return
93 16         68 return $syllables;
94             }
95              
96             1;
97              
98             =head1 ACCENTED CHARACTERS
99              
100             Accented characters, like é, à etc., are normalized (using L)
101             before the number of syllables are counted.
102              
103             =head1 SEE ALSO
104              
105             =over 4
106              
107             =item * L
108              
109             =back
110              
111             =head1 AUTHOR
112              
113             Tore Aursand, C<< >>
114              
115             =head1 BUGS
116              
117             Please report any bugs or feature requests to the web interface at L
118              
119             =head1 SUPPORT
120              
121             You can find documentation for this module with the perldoc command.
122              
123             perldoc Lingua::NO::Syllable
124              
125             You can also look for information at:
126              
127             =over 4
128              
129             =item * AnnoCPAN: Annotated CPAN documentation
130              
131             L
132              
133             =item * CPAN Ratings
134              
135             L
136              
137             =item * Search CPAN
138              
139             L
140              
141             =back
142              
143             =head1 LICENSE AND COPYRIGHT
144              
145             Copyright 2015-2016 Tore Aursand.
146              
147             This program is free software; you can redistribute it and/or modify it
148             under the terms of the the Artistic License (2.0). You may obtain a
149             copy of the full license at:
150              
151             L
152              
153             Any use, modification, and distribution of the Standard or Modified
154             Versions is governed by this Artistic License. By using, modifying or
155             distributing the Package, you accept this license. Do not use, modify,
156             or distribute the Package, if you do not accept this license.
157              
158             If your Modified Version has been derived from a Modified Version made
159             by someone other than you, you are nevertheless required to ensure that
160             your Modified Version complies with the requirements of this license.
161              
162             This license does not grant you the right to use any trademark, service
163             mark, tradename, or logo of the Copyright Holder.
164              
165             This license includes the non-exclusive, worldwide, free-of-charge
166             patent license to make, have made, use, offer to sell, sell, import and
167             otherwise transfer the Package with respect to any patent claims
168             licensable by the Copyright Holder that are necessarily infringed by the
169             Package. If you institute patent litigation (including a cross-claim or
170             counterclaim) against any party alleging that the Package constitutes
171             direct or contributory patent infringement, then this Artistic License
172             to you shall terminate on the date that such litigation is filed.
173              
174             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
175             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
176             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
177             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
178             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
179             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
180             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
181             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.