| 1 | # ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | package Text::Phonetic::Koeln; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | extends qw(Text::Phonetic); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | our $VERSION = $Text::Phonetic::VERSION; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 81 | ▶ | sub _do_encode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_do_encode: called CC=24 SCAR=31.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 81 | my ($self,$string) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 81 | my (@character_list,$result,$last_match); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 81 | $string = uc($string); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # Replace umlaut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 81 | $string =~ s/ß/S/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 81 | $string =~ s/Ä/AE/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 81 | $string =~ s/Ö/OE/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 81 | $string =~ s/Ü/UE/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # Replace double consonants | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | #$string =~ s/([BCDFGHJKLMNPQRSTVWXZ])\1+/$1/g; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # Convert string to array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 81 | @character_list = split //,$string; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 81 | $result = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # Handle initial sounds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 81 | ▶ | if ($character_list[0] eq 'C') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 28 | ▶ | if (Text::Phonetic::_is_inlist($character_list[1],qw(A H K L O Q R U X))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 10 | $result .= 4; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 18 | $result .= 8; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 28 | $last_match = shift @character_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # Loop all characters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 81 | ▶ | while (scalar(@character_list)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # A,E,I,J,O,U,Y => 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 293 | ▶ | if (Text::Phonetic::_is_inlist($character_list[0],qw(A E I J Y O U))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 12 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 80 | $result .= 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 80 | $last_match = shift @character_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # B => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } elsif ($character_list[0] eq 'B') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 34 | $result .= 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 34 | $last_match = shift @character_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # P in front of H => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # P => 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } elsif ($character_list[0] eq 'P') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | ▶ | if (defined($character_list[1]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $character_list[1] and $character_list[1] eq "H"
MC/DC0%0/2
defined $character_list[1] and $character_list[1] eq "H"
defined $character_list[1]
$character_list[1] eq "H"
Truth table66%2/3
A and B
A= defined $character_list[1]B= $character_list[1] eq "H"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | && $character_list[1] eq 'H') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $result .= 3; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | $result .= 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 3 | $last_match = shift @character_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # D,T in front of C,S,Z => 8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # D,T => 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } elsif (Text::Phonetic::_is_inlist($character_list[0],qw(D T))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 15 | ▶ | if (defined($character_list[1]) && $character_list[1] =~ m/[CSZ]/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $character_list[1] and $character_list[1] =~ /[CSZ]/
MC/DC100%2/2
defined $character_list[1] and $character_list[1] =~ /[CSZ]/
defined $character_list[1]
$character_list[1] =~ /[CSZ]/
Truth table100%3/3
A and B
A= defined $character_list[1]B= $character_list[1] =~ /[CSZ]/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 3 | $result .= 8; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 12 | $result .= 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 15 | $last_match = shift @character_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # F,V,W => 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } elsif (Text::Phonetic::_is_inlist($character_list[0],qw(F V W))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 18 | $result .= 3; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 18 | $last_match = shift @character_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # C in front of A,H,K,O,Q,U,X => 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # C after S,Z => 8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } elsif ($character_list[0] eq 'C') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 43 | ▶ | if (Text::Phonetic::_is_inlist($last_match,qw(S Z))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 12 | $result .= 8; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | ▶ | } elsif (defined($character_list[1]) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
defined $character_list[1] and Text::Phonetic::_is_inlist($character_list[1], ("A", "H", "K", "O", "Q", "U", "X"))
MC/DC100%2/2
defined $character_list[1] and Text::Phonetic::_is_inlist($character_list[1], ("A", "H", "K", "O", "Q", "U", "X"))
defined $character_list[1]
Text::Phonetic::_is_inlist($character_list[1], ("A", "H", "K", "O", "Q", "U", "X"))
Truth table100%3/3
A and B
A= defined $character_list[1]B= Text::Phonetic::_is_inlist($character_list[1], ("A", "H", "K", "O", "Q", "U", "X"))
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | && Text::Phonetic::_is_inlist($character_list[1],qw(A H K O Q U X))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 10 | $result .= 4; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 21 | $result .= 8; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 43 | $last_match = shift @character_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # G,K,Q => 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } elsif (Text::Phonetic::_is_inlist($character_list[0],qw(G Q K))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 11 | $result .= 4; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 11 | $last_match = shift @character_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # X not after C,K,Q => 48 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # X after C,K,Q => 8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } elsif ($character_list[0] eq 'X') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 5 | ▶ | if (Text::Phonetic::_is_inlist($last_match,qw(C K Q))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 4 | $result .= 8; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | $result .= 48; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 5 | $last_match = shift @character_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # L => 5 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } elsif ($character_list[0] eq 'L') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 9 | $result .= 5; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 9 | $last_match = shift @character_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # M,N => 6 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } elsif (Text::Phonetic::_is_inlist($character_list[0],qw(M N))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 21 | $result .= 6; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 21 | $last_match = shift @character_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # R => 7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } elsif ($character_list[0] eq 'R') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 18 | $result .= 7; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 18 | $last_match = shift @character_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # S,Z => 8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } elsif (Text::Phonetic::_is_inlist($character_list[0],qw(S Z))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 21 | $result .= 8; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 21 | $last_match = shift @character_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # No rule matched | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 15 | $last_match = shift @character_list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | # Replace consecutive codes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 81 | $result =~ s/(\d)\1+/$1/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # Replace zero code (except for first position) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 81 | $result =~ s/([1-9])0+/$1/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 81 | return $result | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | Text::Phonetic::Koeln - Kölner Phonetik algorithm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | The "Kölner Phonetik" is a phonetic algorithm for indexing names by sound, as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | pronounced in German. The goal is for names with the same pronunciation to be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | encoded to the same representation so that they can be matched despite minor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | differences in spelling. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | In contrast to Soundex this algorithm is suitable for long names since the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | length of the encoded result is not limited. This algorithm is able to find | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | allmost all ortographic variations in names, but also produces many false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | positives. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | The result is always a sequence of numbers. Special characters and whitespaces | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | are ignored. If your text might contain non-latin characters (except for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | German umlaute and 'ß') you should unaccent it prior to creating a phonetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | Maroš Kollár | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | CPAN ID: MAROS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | maros [at] k-1.com | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | http://www.k-1.com | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | Text::Phonetic::Koeln is Copyright (c) 2006,2007 Maroš. Kollár. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | The full text of the license can be found in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | LICENSE file included with this module. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | Description of the algorithm can be found at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | L<http://de.wikipedia.org/wiki/K%C3%B6lner_Phonetik> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | Hans Joachim Postel: Die Kölner Phonetik. Ein Verfahren zur Identifizierung | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | von Personennamen auf der Grundlage der Gestaltanalyse. in: IBM-Nachrichten, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 19. Jahrgang, 1969, S. 925-931 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||