| 1 | package Lingua::NO::Syllable; | ||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use feature 'unicode_strings'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 2 | require Exporter; | |||||||||||
| 8 | |||||||||||||
| 9 | 2 | ▶ | use Unicode::Normalize; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 2 | our @ISA = qw/ Exporter /; | |||||||||||
| 12 | 2 | our @EXPORT = qw/ syllable syllables /; | |||||||||||
| 13 | |||||||||||||
| 14 | 2 | 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 | 2 | 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<Lingua::NO::Syllable::syllables($word)> 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 | ▶ | return syllables( @_ ); | ||||||||||
|
Subroutine
syllable: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | sub syllables { | ||||||||||||
| 54 | 10 | ▶ | my $word = shift // ''; | ||||||||||
|
Subroutine
syllables: called CC=7 SCAR=19.5
Pod
undocumented
Condition50%1/2
shift() // ""
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 55 | |||||||||||||
| 56 | # Remove surrounding spaces. | ||||||||||||
| 57 | 10 | $word =~ s/^\s+//; | |||||||||||
| 58 | 10 | $word =~ s/\s+$//; | |||||||||||
| 59 | |||||||||||||
| 60 | # If the word is one character long, consider it to contain only one | ||||||||||||
| 61 | # syllable. | ||||||||||||
| 62 | 10 | ▶ | return 1 if ( length($word) == 1 ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | |||||||||||||
| 64 | # Lowercase the word. | ||||||||||||
| 65 | 10 | $word = lc( $word ); | |||||||||||
| 66 | |||||||||||||
| 67 | # Normalize the word, i.e. convert accented characters to their normalized | ||||||||||||
| 68 | # representation. | ||||||||||||
| 69 | 10 | $word = NFD( $word ); | |||||||||||
| 70 | |||||||||||||
| 71 | # Create an array of the word's characters. | ||||||||||||
| 72 | 10 | 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 | my $syllables = 0; | |||||||||||
| 77 | |||||||||||||
| 78 | 10 | foreach my $vowel ( @VOWELS ) { | |||||||||||
| 79 | 90 | foreach my $char ( @chars ) { | |||||||||||
| 80 | 612 | ▶ | if ( $vowel eq $char ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | 34 | $syllables++; | |||||||||||
| 82 | } | ||||||||||||
| 83 | } | ||||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | # Certain vowel combinations doesn't classify as "syllable separator". | ||||||||||||
| 87 | 10 | foreach ( qw(ai au ay ei eu ey oi ou oy øi øu øy æi æu æy åi åu åy) ) { | |||||||||||
| 88 | 180 | my $occurences = $word =~ m/$_/g; | |||||||||||
| 89 | 180 | $syllables -= $occurences; | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | # Return | ||||||||||||
| 93 | 10 | return $syllables; | |||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 2 | 1; | |||||||||||
| 97 | |||||||||||||
| 98 | =head1 ACCENTED CHARACTERS | ||||||||||||
| 99 | |||||||||||||
| 100 | Accented characters, like é, à etc., are normalized (see L<Unicode::Normalize>) | ||||||||||||
| 101 | before the number of syllables are calculated. | ||||||||||||
| 102 | |||||||||||||
| 103 | =head1 SEE ALSO | ||||||||||||
| 104 | |||||||||||||
| 105 | =over 4 | ||||||||||||
| 106 | |||||||||||||
| 107 | =item * L<Lingua::EN::Syllable> | ||||||||||||
| 108 | |||||||||||||
| 109 | =back | ||||||||||||
| 110 | |||||||||||||
| 111 | =head1 AUTHOR | ||||||||||||
| 112 | |||||||||||||
| 113 | Tore Aursand, C<< <toreau at gmail.com> >> | ||||||||||||
| 114 | |||||||||||||
| 115 | =head1 BUGS | ||||||||||||
| 116 | |||||||||||||
| 117 | Please report any bugs or feature requests to the web interface at L<https://rt.cpan.org/Dist/Display.html?Name=Lingua-NO-Syllable> | ||||||||||||
| 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<http://annocpan.org/dist/Lingua-NO-Syllable> | ||||||||||||
| 132 | |||||||||||||
| 133 | =item * CPAN Ratings | ||||||||||||
| 134 | |||||||||||||
| 135 | L<http://cpanratings.perl.org/d/Lingua-NO-Syllable> | ||||||||||||
| 136 | |||||||||||||
| 137 | =item * Search CPAN | ||||||||||||
| 138 | |||||||||||||
| 139 | L<http://search.cpan.org/dist/Lingua-NO-Syllable/> | ||||||||||||
| 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<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||
| 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. | ||||||||||||