| 1 | package Text::TransMetaphone::en_US; | ||
| 2 | use utf8; | ||
| 3 | |||
| 4 | BEGIN | ||
| 5 | { | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use vars qw( $VERSION $LocaleRange ); | ||
| 9 | |||
| 10 | $VERSION = '0.10'; | ||
| 11 | |||
| 12 | $LocaleRange = qr/\p{InBasicLatin}/; | ||
| 13 | } | ||
| 14 | |||
| 15 | |||
| 16 | my($primary,$secondary) = ("",""); | ||
| 17 | my $KEY_LIMIT = 4; | ||
| 18 | |||
| 19 | sub appendPhones | ||
| 20 | { | ||
| 21 | $primary .= $_[0]; | ||
| 22 | $secondary .= ( $_[1] ) ? $_[1] : $_[0]; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub stringAt | ||
| 26 | { | ||
| 27 | my($word,$start,$length) = (shift,shift,shift); | ||
| 28 | |||
| 29 | return 0 if( (0 > $start) || ($start >= length($word) ) ); | ||
| 30 | |||
| 31 | my $sub = substr( $word, $start, $length ); | ||
| 32 | |||
| 33 | foreach my $test ( @_ ) { | ||
| 34 | return 1 if( $sub eq $test ); | ||
| 35 | } | ||
| 36 | |||
| 37 | } | ||
| 38 | |||
| 39 | sub charAt | ||
| 40 | { | ||
| 41 | # string , index | ||
| 42 | substr( $_[0], $_[1], 1 ); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub isVowel | ||
| 46 | { | ||
| 47 | # string , index | ||
| 48 | substr( $_[0], $_[1], 1 ) =~ /[AEIOUY]/ ; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub SlavoGermanic | ||
| 52 | { | ||
| 53 | $_ = shift; | ||
| 54 | |||
| 55 | return ( /([KW])|(CZ)|(WITZ)/ ) ? 1 : 0 ; | ||
| 56 | } | ||
| 57 | |||
| 58 | # | ||
| 59 | # Note that the code below is converted from the C language | ||
| 60 | # and may still exhibit C language style and logic. | ||
| 61 | # | ||
| 62 | sub trans_metaphone | ||
| 63 | { | ||
| 64 | my ($original) = uc( $_[0] ); | ||
| 65 | |||
| 66 | my $length = length($original); | ||
| 67 | my $last = $length - 1; | ||
| 68 | my $current = 0; | ||
| 69 | |||
| 70 | |||
| 71 | # Initial 'X' is pronounced 'Z' e.g. 'Xavier' | ||
| 72 | if( charAt($original, 0) eq 'X' ) { | ||
| 73 | appendPhones( "s" ); # 'Z' maps to 'S' | ||
| 74 | $current += 1; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | while ( ($KEY_LIMIT < length($primary)) || (length($secondary) < $KEY_LIMIT) ) { | ||
| 79 | |||
| 80 | next if( $current >= $length); | ||
| 81 | |||
| 82 | my $ch = charAt($original, $current); | ||
| 83 | if( $ch =~ /[AEIOUY]/ ) { | ||
| 84 | if( $current == 0) { | ||
| 85 | # all init vowels now map to 'A' | ||
| 86 | appendPhones( "a" ); | ||
| 87 | } | ||
| 88 | $current += 1; | ||
| 89 | } | ||
| 90 | elsif( $ch eq 'B' ) { | ||
| 91 | # "-mb", e.g", "dumb", already skipped over... | ||
| 92 | appendPhones( "p" ); | ||
| 93 | |||
| 94 | $current += ( charAt($original, ($current + 1)) eq 'B' ) | ||
| 95 | ? 2 | ||
| 96 | : 1 | ||
| 97 | ; | ||
| 98 | } | ||
| 99 | elsif( $ch eq 'Ç' ) { | ||
| 100 | appendPhones( "s" ); | ||
| 101 | $current += 1; | ||
| 102 | } | ||
| 103 | elsif( $ch eq 'C' ) { | ||
| 104 | # various germanic | ||
| 105 | if( ($current > 1) | ||
| 106 | && !isVowel($original, $current - 2) | ||
| 107 | && stringAt($original, ($current - 1), 3, "ACH", "") | ||
| 108 | && ((charAt($original, $current + 2) != 'I') | ||
| 109 | && ((charAt($original, $current + 2) != 'E') | ||
| 110 | || stringAt($original, ($current - 2), 6, "BACHER", "MACHER", "")))) | ||
| 111 | { | ||
| 112 | appendPhones( "k" ); | ||
| 113 | $current += 2; | ||
| 114 | } | ||
| 115 | |||
| 116 | # special case 'caesar' | ||
| 117 | if( ($current == 0) && stringAt($original, $current, 6, "CAESAR", "")) { | ||
| 118 | appendPhones( "s" ); | ||
| 119 | $current += 2; | ||
| 120 | } | ||
| 121 | |||
| 122 | # italian 'chianti' | ||
| 123 | if( stringAt($original, $current, 4, "CHIA", "")) { | ||
| 124 | appendPhones( "k" ); | ||
| 125 | $current += 2; | ||
| 126 | } | ||
| 127 | |||
| 128 | if( stringAt($original, $current, 2, "CH", "")) { | ||
| 129 | # find 'michael' | ||
| 130 | if( ($current > 0) && stringAt($original, $current, 4, "CHAE", "")) { | ||
| 131 | appendPhones( "k", "ʃ" ); | ||
| 132 | $current += 2; | ||
| 133 | } | ||
| 134 | |||
| 135 | # greek roots e.g. 'chemistry', 'chorus' | ||
| 136 | if( ($current == 0) | ||
| 137 | && (stringAt($original, ($current + 1), 5, "HARAC", "HARIS", "") | ||
| 138 | || stringAt($original, ($current + 1), 3, "HOR", "HYM", "HIA", "HEM", "")) | ||
| 139 | && !stringAt($original, 0, 5, "CHORE", "")) | ||
| 140 | { | ||
| 141 | appendPhones( "k" ); | ||
| 142 | $current += 2; | ||
| 143 | next; | ||
| 144 | } | ||
| 145 | |||
| 146 | # germanic, greek, or otherwise 'ch' for 'kh' sound | ||
| 147 | if( (stringAt($original, 0, 4, "VAN ", "VON ", "") | ||
| 148 | || stringAt($original, 0, 3, "SCH", "")) | ||
| 149 | # 'architect but not 'arch', 'orchestra', 'orchid' | ||
| 150 | || stringAt($original, ($current - 2), 6, "ORCHES", "ARCHIT", "ORCHID", "") | ||
| 151 | || stringAt($original, ($current + 2), 1, "T", "S", "") | ||
| 152 | || ((stringAt($original, ($current - 1), 1, "A", "O", "U", "E", "") | ||
| 153 | || ($current == 0)) | ||
| 154 | # e.g., 'wachtler', 'wechsler', but not 'tichner' | ||
| 155 | && stringAt($original, ($current + 2), 1, "L", "R", "N", "M", "B", "H", "F", "V", "W", " ", "")) ) | ||
| 156 | { | ||
| 157 | appendPhones( "k" ); | ||
| 158 | } | ||
| 159 | elsif( $current > 0) { | ||
| 160 | if( stringAt($original, 0, 2, "MC", "")) { | ||
| 161 | # e.g., "McHugh" | ||
| 162 | appendPhones( "k" ); | ||
| 163 | } else { | ||
| 164 | appendPhones( "ʧ", "k" ); | ||
| 165 | } | ||
| 166 | } else { | ||
| 167 | appendPhones( "ʃ" ); | ||
| 168 | } | ||
| 169 | $current += 2; | ||
| 170 | next; | ||
| 171 | } | ||
| 172 | # e.g, 'czerny' | ||
| 173 | if( stringAt($original, $current, 2, "CZ", "") | ||
| 174 | && !stringAt($original, ($current - 2), 4, "WICZ", "")) | ||
| 175 | { | ||
| 176 | appendPhones( "s", "ʃ" ); | ||
| 177 | $current += 2; | ||
| 178 | next; | ||
| 179 | } | ||
| 180 | |||
| 181 | # e.g., 'focaccia' | ||
| 182 | if( stringAt($original, ($current + 1), 3, "CIA", "")) { | ||
| 183 | appendPhones( "ʃ" ); | ||
| 184 | $current += 3; | ||
| 185 | next; | ||
| 186 | } | ||
| 187 | |||
| 188 | # double 'C', but not if e.g. 'McClellan' | ||
| 189 | if( stringAt($original, $current, 2, "CC", "") | ||
| 190 | && !(($current == 1) && (charAt($original, 0) eq 'M'))) | ||
| 191 | # 'bellocchio' but not 'bacchus' | ||
| 192 | { | ||
| 193 | if( stringAt($original, ($current + 2), 1, "I", "E", "H", "") | ||
| 194 | && !stringAt($original, ($current + 2), 2, "HU", "")) | ||
| 195 | { | ||
| 196 | # 'accident', 'accede' 'succeed' | ||
| 197 | if( (($current == 1) | ||
| 198 | && (charAt($original, $current - 1) eq 'A')) | ||
| 199 | || stringAt($original, ($current - 1), 5, "UCCEE", "UCCES", "")) | ||
| 200 | { | ||
| 201 | appendPhones( "ks" ); | ||
| 202 | # 'bacci', 'bertucci', other italian | ||
| 203 | } | ||
| 204 | else { | ||
| 205 | appendPhones( "ʃ" ); | ||
| 206 | } | ||
| 207 | $current += 3; | ||
| 208 | next; | ||
| 209 | } | ||
| 210 | else { # Pierce's rule | ||
| 211 | appendPhones( "k" ); | ||
| 212 | $current += 2; | ||
| 213 | next; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | if( stringAt($original, $current, 2, "CK", "CG", "CQ", "")) { | ||
| 218 | appendPhones( "k" ); | ||
| 219 | $current += 2; | ||
| 220 | next; | ||
| 221 | } | ||
| 222 | |||
| 223 | if( stringAt($original, $current, 2, "CI", "CE", "CY", "")) { | ||
| 224 | # italian vs. english | ||
| 225 | if( stringAt($original, $current, 3, "CIO", "CIE", "CIA", "")) { | ||
| 226 | appendPhones( "s", "ʃ" ); | ||
| 227 | } else { | ||
| 228 | appendPhones( "s" ); | ||
| 229 | } | ||
| 230 | $current += 2; | ||
| 231 | next; | ||
| 232 | } | ||
| 233 | |||
| 234 | # else | ||
| 235 | appendPhones( "k" ); | ||
| 236 | |||
| 237 | # name sent in 'mac caffrey', 'mac gregor | ||
| 238 | if( stringAt($original, ($current + 1), 2, " C", " Q", " G", "")) { | ||
| 239 | $current += 3; | ||
| 240 | } elsif( stringAt($original, ($current + 1), 1, "C", "K", "Q", "") | ||
| 241 | && !stringAt($original, ($current + 1), 2, "CE", "CI", "")) { | ||
| 242 | $current += 2; | ||
| 243 | } else { | ||
| 244 | $current += 1; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | elsif( $ch eq 'D' ) { | ||
| 248 | if( stringAt($original, $current, 2, "DG", "") ) { | ||
| 249 | if( stringAt($original, ($current + 2), 1, "I", "E", "Y", "") ) { | ||
| 250 | # e.g. 'edge' | ||
| 251 | appendPhones( "ʤ", "j" ); | ||
| 252 | $current += 3; | ||
| 253 | } else { | ||
| 254 | # e.g. 'edgar' | ||
| 255 | appendPhones( "tk" ); | ||
| 256 | $current += 2; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | elsif( stringAt($original, $current, 2, "DT", "DD", "")) { | ||
| 260 | appendPhones( "t" ); | ||
| 261 | $current += 2; | ||
| 262 | } else { | ||
| 263 | appendPhones( "t" ); | ||
| 264 | $current += 1; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | elsif( $ch eq 'F' ) { | ||
| 268 | $current += ( charAt($original, $current + 1) eq 'F' ) | ||
| 269 | ? 2 | ||
| 270 | : 1 | ||
| 271 | ; | ||
| 272 | appendPhones( "f" ); | ||
| 273 | } | ||
| 274 | elsif( $ch eq 'G' ) { | ||
| 275 | if( charAt($original, $current + 1) eq 'H' ) { | ||
| 276 | if( ($current > 0) && !isVowel($original, $current - 1) ) { | ||
| 277 | appendPhones( "k" ); | ||
| 278 | $current += 2; | ||
| 279 | next; | ||
| 280 | } | ||
| 281 | if( $current < 3) { | ||
| 282 | # 'ghislane', ghiradelli | ||
| 283 | if( $current == 0) { | ||
| 284 | if( charAt($original, $current + 2) eq 'I') { | ||
| 285 | appendPhones( "j" ); | ||
| 286 | } else { | ||
| 287 | appendPhones( "k" ); | ||
| 288 | } | ||
| 289 | $current += 2; | ||
| 290 | next; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | # Parker's rule (with some further refinements) - e.g., 'hugh' | ||
| 294 | if( (($current > 1) | ||
| 295 | && stringAt($original, ($current - 2), 1, "B", "H", "D", "")) | ||
| 296 | # e.g., 'bough' | ||
| 297 | || (($current > 2) | ||
| 298 | && stringAt($original, ($current - 3), 1, "B", "H", "D", "")) | ||
| 299 | # e.g., 'broughton' | ||
| 300 | || (($current > 3) | ||
| 301 | && stringAt($original, ($current - 4), 1, "B", "H", ""))) | ||
| 302 | { | ||
| 303 | $current += 2; | ||
| 304 | next; | ||
| 305 | } | ||
| 306 | else | ||
| 307 | { | ||
| 308 | # e.g., 'laugh', 'McLaughlin', 'cough', 'gough', 'rough', 'tough' | ||
| 309 | if( ($current > 2) | ||
| 310 | && (charAt($original, $current - 1) eq 'U') | ||
| 311 | && stringAt($original, ($current - 3), 1, "C", "G", "L", "R", "T", "")) | ||
| 312 | { | ||
| 313 | appendPhones( "f" ); | ||
| 314 | } | ||
| 315 | elsif( ($current > 0) && charAt($original, $current - 1) != 'I' ) | ||
| 316 | { | ||
| 317 | appendPhones( "k" ); | ||
| 318 | } | ||
| 319 | |||
| 320 | $current += 2; | ||
| 321 | next; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | if( charAt($original, $current + 1) eq 'N') { | ||
| 326 | if( ($current == 1) && isVowel($original, 0) && !SlavoGermanic($original)) { | ||
| 327 | appendPhones( "kn", "n" ); | ||
| 328 | } | ||
| 329 | # not e.g. 'cagney' | ||
| 330 | elsif( !stringAt($original, ($current + 2), 2, "EY", "") | ||
| 331 | && (charAt($original, $current + 1) != 'Y') | ||
| 332 | && !SlavoGermanic($original)) | ||
| 333 | { | ||
| 334 | appendPhones( "n", "kn" ); | ||
| 335 | } else { | ||
| 336 | appendPhones( "kn" ); | ||
| 337 | } | ||
| 338 | $current += 2; | ||
| 339 | next; | ||
| 340 | } | ||
| 341 | |||
| 342 | # 'tagliaro' | ||
| 343 | if( stringAt($original, ($current + 1), 2, "LI", "") | ||
| 344 | && !SlavoGermanic($original)) | ||
| 345 | { | ||
| 346 | appendPhones( "kl", "l" ); | ||
| 347 | $current += 2; | ||
| 348 | next; | ||
| 349 | } | ||
| 350 | |||
| 351 | # -ges-,-gep-,-gel-, -gie- at beginning | ||
| 352 | if( ($current == 0) | ||
| 353 | && ((charAt($original, $current + 1) eq 'Y') | ||
| 354 | || stringAt($original, ($current + 1), 2, "ES", "EP", "EB", "EL", "EY", "IB", "IL", "IN", "IE", "EI", "ER", ""))) | ||
| 355 | { | ||
| 356 | appendPhones( "k", "j" ); | ||
| 357 | $current += 2; | ||
| 358 | next; | ||
| 359 | } | ||
| 360 | |||
| 361 | # -ger-, -gy- | ||
| 362 | if( (stringAt($original, ($current + 1), 2, "ER", "") | ||
| 363 | || (charAt($original, $current + 1) eq 'Y')) | ||
| 364 | && !stringAt($original, 0, 6, "DANGER", "RANGER", "MANGER", "") | ||
| 365 | && !stringAt($original, ($current - 1), 1, "E", "I", "") | ||
| 366 | && !stringAt($original, ($current - 1), 3, "RGY", "OGY", "")) | ||
| 367 | { | ||
| 368 | appendPhones( "k", "j" ); | ||
| 369 | $current += 2; | ||
| 370 | next; | ||
| 371 | } | ||
| 372 | |||
| 373 | # italian e.g, 'biaggi' | ||
| 374 | if( stringAt($original, ($current + 1), 1, "E", "I", "Y", "") | ||
| 375 | || stringAt($original, ($current - 1), 4, "AGGI", "OGGI", "")) | ||
| 376 | { | ||
| 377 | # obvious germanic | ||
| 378 | if( (stringAt($original, 0, 4, "VAN ", "VON ", "") | ||
| 379 | || stringAt($original, 0, 3, "SCH", "")) | ||
| 380 | || stringAt($original, ($current + 1), 2, "ET", "")) | ||
| 381 | { | ||
| 382 | appendPhones( "k" ); | ||
| 383 | } | ||
| 384 | elsif( stringAt ($original, ($current + 1), 4, "IER ", "")) { | ||
| 385 | # always soft if french ending | ||
| 386 | apendPhones( "j" ); | ||
| 387 | } else { | ||
| 388 | endPhones( "j", "k" ); | ||
| 389 | } | ||
| 390 | $current += 2; | ||
| 391 | next; | ||
| 392 | } | ||
| 393 | |||
| 394 | $current += (charAt($original, $current + 1) eq 'G') | ||
| 395 | ? 2 | ||
| 396 | : 1 | ||
| 397 | ; | ||
| 398 | appendPhones( "k" ); | ||
| 399 | } | ||
| 400 | elsif( $ch eq 'H' ) { | ||
| 401 | # only keep if first & before vowel or btw. 2 vowels | ||
| 402 | if( (($current == 0) || isVowel($original, $current - 1)) | ||
| 403 | && isVowel($original, $current + 1)) | ||
| 404 | { | ||
| 405 | appendPhones( "h" ); | ||
| 406 | $current += 2; | ||
| 407 | } | ||
| 408 | else { # also takes care of 'HH' | ||
| 409 | $current += 1; | ||
| 410 | |||
| 411 | } | ||
| 412 | } | ||
| 413 | elsif( $ch eq 'J' ) { | ||
| 414 | # obvious spanish, 'jose', 'san jacinto' | ||
| 415 | if( stringAt($original, $current, 4, "JOSE", "") | ||
| 416 | || stringAt($original, 0, 4, "SAN ", "") ) | ||
| 417 | { | ||
| 418 | if( (($current == 0) | ||
| 419 | && (charAt($original, $current + 4) == ' ')) | ||
| 420 | || stringAt($original, 0, 4, "SAN ", "") ) | ||
| 421 | { | ||
| 422 | appendPhones( "h" ); | ||
| 423 | } | ||
| 424 | else { | ||
| 425 | appendPhones( "j", "h" ); | ||
| 426 | } | ||
| 427 | $current += 1; | ||
| 428 | next; | ||
| 429 | } | ||
| 430 | |||
| 431 | if( ($current == 0) && !stringAt($original, $current, 4, "JOSE", "") ) { | ||
| 432 | # Yankelovich/Jankelowicz | ||
| 433 | appendPhones( "ʤ", "a" ); | ||
| 434 | } elsif( # spanish pron. of e.g. 'bajador' | ||
| 435 | IsVowel($original, $current - 1) | ||
| 436 | && !SlavoGermanic($original) | ||
| 437 | && ((charAt($original, $current + 1) eq 'A') | ||
| 438 | || (charAt($original, $current + 1) == 'O')) ) | ||
| 439 | { | ||
| 440 | appendPhones( "j", "h" ); | ||
| 441 | } | ||
| 442 | elsif( $current == $last) { | ||
| 443 | appendPhones( "ʤ", "" ); | ||
| 444 | } | ||
| 445 | elsif( !stringAt($original, ($current + 1), 1, "L", "T", "K", "S", "N", "M", "B", "Z", "") | ||
| 446 | && !stringAt($original, ($current - 1), 1, "S", "K", "L", "")) | ||
| 447 | { | ||
| 448 | appendPhones( "j" ); | ||
| 449 | } | ||
| 450 | |||
| 451 | $current += (charAt($original, $current + 1) eq 'J') # it could happen! | ||
| 452 | ? 2 | ||
| 453 | : 1 | ||
| 454 | ; | ||
| 455 | } | ||
| 456 | elsif( $ch eq 'K' ) { | ||
| 457 | if( charAt($original, $current + 1) != 'H') { | ||
| 458 | $current += (charAt($original, $current + 1) eq 'K') | ||
| 459 | ? 2 | ||
| 460 | : 1 | ||
| 461 | ; | ||
| 462 | $primary .= "k"; | ||
| 463 | } | ||
| 464 | else { | ||
| 465 | # husky "kh" from arabic | ||
| 466 | $secondary .= "x"; | ||
| 467 | $current += 2; | ||
| 468 | } | ||
| 469 | $secondary .= "k"; | ||
| 470 | } | ||
| 471 | elsif( $ch eq 'L' ) { | ||
| 472 | if( charAt($original, $current + 1) eq 'L') { | ||
| 473 | # spanish e.g. 'cabrillo', 'gallegos' | ||
| 474 | if( (($current == ($length - 3)) | ||
| 475 | && stringAt($original, ($current - 1), 4, "ILLO", "ILLA", "ALLE", "")) | ||
| 476 | || ((stringAt($original, ($last - 1), 2, "AS", "OS", "") | ||
| 477 | || stringAt($original, $last, 1, "A", "O", "")) | ||
| 478 | && stringAt($original, ($current - 1), 4, "ALLE", ""))) | ||
| 479 | { | ||
| 480 | appendPhones( "l", "" ); | ||
| 481 | $current += 2; | ||
| 482 | next; | ||
| 483 | } | ||
| 484 | $current += 2; | ||
| 485 | } | ||
| 486 | else { | ||
| 487 | $current += 1; | ||
| 488 | } | ||
| 489 | appendPhones( "l" ); | ||
| 490 | } | ||
| 491 | elsif( $ch eq 'M' ) { | ||
| 492 | if( (stringAt($original, ($current - 1), 3, "UMB", "") | ||
| 493 | && ((($current + 1) == $last) | ||
| 494 | || stringAt($original, ($current + 2), 2, "ER", ""))) | ||
| 495 | # 'dumb','thumb' | ||
| 496 | || (charAt($original, $current + 1) eq 'M')) | ||
| 497 | { | ||
| 498 | $current += 2; | ||
| 499 | } else { | ||
| 500 | $current += 1; | ||
| 501 | } | ||
| 502 | appendPhones( "m" ); | ||
| 503 | } | ||
| 504 | elsif( $ch eq 'N' ) { | ||
| 505 | if( charAt($original, $current + 1) eq 'Y') { | ||
| 506 | $primary .= "ɲ"; | ||
| 507 | $current += 2; | ||
| 508 | } else { | ||
| 509 | $current += (charAt($original, $current + 1) eq 'N') | ||
| 510 | ? 2 | ||
| 511 | : 1 | ||
| 512 | ; | ||
| 513 | $primary .= "n"; | ||
| 514 | } | ||
| 515 | $secondary .= "n"; | ||
| 516 | } | ||
| 517 | elsif( $ch eq 'Ñ' ) { | ||
| 518 | appendPhones( "ɲ" ); | ||
| 519 | $current += 1; | ||
| 520 | } | ||
| 521 | elsif( $ch eq 'P' ) { | ||
| 522 | if( charAt($original, $current + 1) eq 'H') { | ||
| 523 | appendPhones( "f" ); | ||
| 524 | $current += 2; | ||
| 525 | } | ||
| 526 | # also account for "campbell", "raspberry" | ||
| 527 | elsif( stringAt($original, ($current + 1), 1, "P", "B", "")) { | ||
| 528 | $current += 2; | ||
| 529 | } else { | ||
| 530 | $current += 1; | ||
| 531 | } | ||
| 532 | |||
| 533 | appendPhones( "p" ); | ||
| 534 | } | ||
| 535 | elsif( $ch eq 'Q' ) { | ||
| 536 | if( charAt($original, $current + 1) eq 'U') { | ||
| 537 | $primary .= "kw"; | ||
| 538 | $current += 1; # total of 2 | ||
| 539 | } | ||
| 540 | else { | ||
| 541 | $current += (charAt($original, $current + 1) eq 'Q') | ||
| 542 | ? 2 | ||
| 543 | : 1 | ||
| 544 | ; | ||
| 545 | $primary .= "k'"; | ||
| 546 | } | ||
| 547 | |||
| 548 | $secondary .= "k"; | ||
| 549 | } | ||
| 550 | elsif( $ch eq 'R' ) { | ||
| 551 | # french e.g. 'rogier', but exclude 'hochmeier' | ||
| 552 | if( ($current == $last) | ||
| 553 | && !SlavoGermanic($original) | ||
| 554 | && stringAt($original, ($current - 2), 2, "IE", "") | ||
| 555 | && !stringAt($original, ($current - 4), 2, "ME", "MA", "")) | ||
| 556 | { | ||
| 557 | appendPhones( "", "r" ); | ||
| 558 | } | ||
| 559 | else | ||
| 560 | { | ||
| 561 | appendPhones( "r" ); | ||
| 562 | } | ||
| 563 | |||
| 564 | $current += (charAt($original, $current + 1) eq 'R') | ||
| 565 | ? 2 | ||
| 566 | : 1 | ||
| 567 | ; | ||
| 568 | } | ||
| 569 | elsif( $ch eq 'S' ) { | ||
| 570 | # special cases 'island', 'isle', 'carlisle', 'carlysle' | ||
| 571 | if( stringAt($original, ($current - 1), 3, "ISL", "YSL", "")) | ||
| 572 | { | ||
| 573 | $current += 1; | ||
| 574 | next; | ||
| 575 | } | ||
| 576 | |||
| 577 | # special case 'sugar-' | ||
| 578 | if( ($current == 0) && stringAt($original, $current, 5, "SUGAR", "") ) { | ||
| 579 | appendPhones( "ʃ", "s" ); | ||
| 580 | $current += 1; | ||
| 581 | next; | ||
| 582 | } | ||
| 583 | |||
| 584 | if( stringAt($original, $current, 2, "SH", "") ) { | ||
| 585 | # germanic | ||
| 586 | if( stringAt ($original, ($current + 1), 4, "HEIM", "HOEK", "HOLM", "HOLZ", "")) { | ||
| 587 | appendPhones( "s" ); | ||
| 588 | } else { | ||
| 589 | appendPhones( "ʃ" ); | ||
| 590 | } | ||
| 591 | $current += 2; | ||
| 592 | next; | ||
| 593 | } | ||
| 594 | |||
| 595 | # italian & armenian | ||
| 596 | if( stringAt($original, $current, 3, "SIO", "SIA", "") || stringAt($original, $current, 4, "SIAN", "")) { | ||
| 597 | if( !SlavoGermanic($original)) { | ||
| 598 | appendPhones( "s", "ʃ" ); | ||
| 599 | } else { | ||
| 600 | appendPhones( "s" ); | ||
| 601 | } | ||
| 602 | $current += 3; | ||
| 603 | next; | ||
| 604 | } | ||
| 605 | |||
| 606 | # german & anglicisations, e.g. 'smith' match 'schmidt', 'snider' match 'schneider' | ||
| 607 | # also, -sz- in slavic language altho in hungarian it is pronounced 's' | ||
| 608 | if( (($current == 0) | ||
| 609 | && stringAt($original, ($current + 1), 1, "M", "N", "L", "W", "")) | ||
| 610 | || stringAt($original, ($current + 1), 1, "Z", "")) | ||
| 611 | { | ||
| 612 | appendPhones( "s", "ʃ" ); | ||
| 613 | $current += (stringAt($original, ($current + 1), 1, "Z", "")) | ||
| 614 | ? 2 | ||
| 615 | : 1 | ||
| 616 | ; | ||
| 617 | next; | ||
| 618 | } | ||
| 619 | |||
| 620 | if( stringAt($original, $current, 2, "SC", "")) { | ||
| 621 | # Schlesinger's rule | ||
| 622 | if( charAt($original, $current + 2) eq 'H') { | ||
| 623 | # dutch origin, e.g. 'school', 'schooner' | ||
| 624 | if( stringAt($original, ($current + 3), 2, "OO", "ER", "EN", "UY", "ED", "EM", "")) { | ||
| 625 | # 'schermerhorn', 'schenker' | ||
| 626 | if( stringAt($original, ($current + 3), 2, "ER", "EN", "")) { | ||
| 627 | appendPhones( "ʃ", "sk" ); | ||
| 628 | } else { | ||
| 629 | appendPhones( "sk" ); | ||
| 630 | } | ||
| 631 | $current += 3; | ||
| 632 | next; | ||
| 633 | } | ||
| 634 | elsif( ($current == 0) && !IsVowel($original, 3) && (charAt($original, 3) != 'W')) { | ||
| 635 | appendPhones( "ʃ", "s" ); | ||
| 636 | } else { | ||
| 637 | appendPhones( "ʃ" ); | ||
| 638 | } | ||
| 639 | $current += 3; | ||
| 640 | next; | ||
| 641 | } | ||
| 642 | |||
| 643 | if( stringAt($original, ($current + 2), 1, "I", "E", "Y", "")) { | ||
| 644 | appendPhones( "S", "s" ); | ||
| 645 | $current += 3; | ||
| 646 | next; | ||
| 647 | } | ||
| 648 | |||
| 649 | # else | ||
| 650 | |||
| 651 | appendPhones( "sk" ); | ||
| 652 | $current += 3; | ||
| 653 | next; | ||
| 654 | } | ||
| 655 | |||
| 656 | # french e.g. 'resnais', 'artois' | ||
| 657 | if( ($current == $last) && stringAt($original, ($current - 2), 2, "AI", "OI", "") ) { | ||
| 658 | appendPhones( "", "s" ); | ||
| 659 | } else { | ||
| 660 | appendPhones( "s" ); | ||
| 661 | } | ||
| 662 | |||
| 663 | $current += (stringAt($original, ($current + 1), 1, "S", "Z", "")) | ||
| 664 | ? 2 | ||
| 665 | : 1 | ||
| 666 | ; | ||
| 667 | } | ||
| 668 | elsif( $ch eq 'T' ) { | ||
| 669 | if( stringAt($original, $current, 4, "TION", "")) { | ||
| 670 | appendPhones( "ʃ" ); | ||
| 671 | $current += 3; | ||
| 672 | } | ||
| 673 | elsif( stringAt($original, $current, 3, "TIA", "TCH", "")) { | ||
| 674 | appendPhones( "ʃ" ); | ||
| 675 | $current += 3; | ||
| 676 | } | ||
| 677 | |||
| 678 | if( stringAt($original, $current, 2, "TH", "" ) | ||
| 679 | || stringAt($original, $current, 3, "TTH", "")) | ||
| 680 | { | ||
| 681 | # special case 'thomas', 'thames' or germanic | ||
| 682 | if( stringAt($original, ($current + 2), 2, "OM", "AM", "") | ||
| 683 | || stringAt($original, 0, 4, "VAN ", "VON ", "") | ||
| 684 | || stringAt($original, 0, 3, "SCH", "")) | ||
| 685 | { | ||
| 686 | appendPhones( "t" ); | ||
| 687 | } | ||
| 688 | else | ||
| 689 | { | ||
| 690 | appendPhones( "Θ", "t" ); | ||
| 691 | } | ||
| 692 | $current += 2; | ||
| 693 | } | ||
| 694 | elsif( stringAt($original, ($current + 1), 1, "T", "D", "")) { | ||
| 695 | $current += 2; | ||
| 696 | } else { | ||
| 697 | $current += 1; | ||
| 698 | appendPhones( "t" ); | ||
| 699 | } | ||
| 700 | } | ||
| 701 | elsif( $ch eq 'V' ) { | ||
| 702 | $current += (charAt($original, $current + 1) eq 'V') | ||
| 703 | ? 2 | ||
| 704 | : 1 | ||
| 705 | ; | ||
| 706 | appendPhones( "f" ); | ||
| 707 | } | ||
| 708 | elsif( $ch eq 'W' ) { | ||
| 709 | # can also be in middle of word | ||
| 710 | if( stringAt($original, $current, 2, "WR", "") ) { | ||
| 711 | appendPhones( "r" ); | ||
| 712 | $current += 2; | ||
| 713 | } | ||
| 714 | elsif( ($current == 0) | ||
| 715 | && (IsVowel($original, $current + 1) | ||
| 716 | || stringAt($original, $current, 2, "WH", ""))) | ||
| 717 | { | ||
| 718 | # Wasserman should match Vasserman | ||
| 719 | if( IsVowel($original, $current + 1)) { | ||
| 720 | appendPhones( "a", "f" ); | ||
| 721 | } else { | ||
| 722 | # need Uomo to match Womo | ||
| 723 | appendPhones( "a" ); | ||
| 724 | } | ||
| 725 | } | ||
| 726 | |||
| 727 | # Arnow should match Arnoff | ||
| 728 | elsif( (($current == $last) && IsVowel($original, $current - 1)) | ||
| 729 | || stringAt($original, ($current - 1), 5, "EWSKI", "EWSKY", "OWSKI", "OWSKY", "") | ||
| 730 | || stringAt($original, 0, 3, "SCH", "")) | ||
| 731 | { | ||
| 732 | appendPhones( "", "f" ); | ||
| 733 | $current += 1; | ||
| 734 | } | ||
| 735 | |||
| 736 | # polish e.g. 'filipowicz' | ||
| 737 | elsif( stringAt($original, $current, 4, "WICZ", "WITZ", "") ) { | ||
| 738 | appendPhones( "ts", "fx" ); | ||
| 739 | $current += 4; | ||
| 740 | next; | ||
| 741 | } | ||
| 742 | |||
| 743 | # else skip it | ||
| 744 | $current += 1; | ||
| 745 | } | ||
| 746 | elsif( $ch eq 'X' ) { | ||
| 747 | # french e.g. breaux | ||
| 748 | if( !(($current == $last) | ||
| 749 | && (stringAt($original, ($current - 3), 3, "IAU", "EAU", "") | ||
| 750 | || stringAt($original, ($current - 2), 2, "AU", "OU", "")))) | ||
| 751 | { | ||
| 752 | appendPhones( "ks" ); | ||
| 753 | } | ||
| 754 | |||
| 755 | $current += ( stringAt($original, ($current + 1), 1, "C", "X", "") ) | ||
| 756 | ? 2 | ||
| 757 | : 1 | ||
| 758 | ; | ||
| 759 | } | ||
| 760 | elsif( $ch eq 'Z' ) { | ||
| 761 | # chinese pinyin e.g. 'zhao' | ||
| 762 | if( charAt($original, $current + 1) eq 'H') { | ||
| 763 | appendPhones( "j" ); | ||
| 764 | $current += 2; | ||
| 765 | next; | ||
| 766 | } elsif( stringAt($original, ($current + 1), 2, "ZO", "ZI", "ZA", "") | ||
| 767 | || (SlavoGermanic($original) | ||
| 768 | && (($current > 0) | ||
| 769 | && charAt($original, $current - 1) != 'T'))) | ||
| 770 | { | ||
| 771 | appendPhones( "s", "ts" ); | ||
| 772 | } | ||
| 773 | else { | ||
| 774 | appendPhones( "s" ); | ||
| 775 | } | ||
| 776 | |||
| 777 | $current += (charAt($original, $current + 1) eq 'Z') | ||
| 778 | ? 2 | ||
| 779 | : 1 | ||
| 780 | ; | ||
| 781 | next; | ||
| 782 | } | ||
| 783 | else { | ||
| 784 | $current += 1; | ||
| 785 | } | ||
| 786 | } | ||
| 787 | |||
| 788 | ( $primary, $secondary ); | ||
| 789 | |||
| 790 | } | ||
| 791 | |||
| 792 | sub reverse_key | ||
| 793 | { | ||
| 794 | print STDERR, "not implemented.\n"; | ||
| 795 | } | ||
| 796 | |||
| 797 | |||
| 798 | |||
| 799 | ######################################################### | ||
| 800 | # Do not change this, Do not put anything below this. | ||
| 801 | # File must return "true" value at termination | ||
| 802 | 1; | ||
| 803 | ########################################################## | ||
| 804 | |||
| 805 | __END__ |