File Coverage

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


line stmt bran cond sub pod time code
1             package Lingua::NO::Syllable;
2 2     2   60755 use strict;
  2         8  
  2         141  
3 2     2   16 use warnings;
  2         4  
  2         99  
4 2     2   2051 use utf8;
  2         21  
  2         19  
5 2     2   111 use feature 'unicode_strings';
  2         5  
  2         391  
6              
7             require Exporter;
8              
9 2     2   3066 use Unicode::Normalize;
  2         601968  
  2         1292  
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.04.
25              
26             =cut
27              
28             our $VERSION = '0.04';
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 10   50 10 0 4557 my $word = shift // '';
55              
56             # Remove surrounding spaces.
57 10         26 $word =~ s/^\s+//;
58 10         22 $word =~ s/\s+$//;
59              
60             # If the word is one character long, consider it to contain only one
61             # syllable.
62 10 50       23 return 1 if ( length($word) == 1 );
63              
64             # Lowercase the word.
65 10         27 $word = lc( $word );
66              
67             # Normalize the word, i.e. convert accented characters to their normalized
68             # representation.
69 10         27 $word = NFD( $word );
70              
71             # Create an array of the word's characters.
72 10         553 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 10         14 my $syllables = 0;
77              
78 10         14 foreach my $vowel ( @VOWELS ) {
79 90         101 foreach my $char ( @chars ) {
80 612 100       1036 if ( $vowel eq $char ) {
81 34         42 $syllables++;
82             }
83             }
84             }
85              
86             # Certain vowel combinations doesn't classify as "syllable separator".
87 10         20 foreach ( qw(ai au ay ei eu ey oi ou oy øi øu øy æi æu æy åi åu åy) ) {
88 180         1119 my $occurences = $word =~ m/$_/g;
89 180         299 $syllables -= $occurences;
90             }
91              
92             # Return
93 10         41 return $syllables;
94             }
95              
96             1;
97              
98             =head1 ACCENTED CHARACTERS
99              
100             Accented characters, like é, à etc., are normalized (see L)
101             before the number of syllables are calculated.
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 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.