-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmetaphone.c
1470 lines (1287 loc) · 33.1 KB
/
dmetaphone.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This is a port of the Double Metaphone algorithm for use in PostgreSQL.
*
* $PostgreSQL: pgsql/contrib/fuzzystrmatch/dmetaphone.c,v 1.11 2007/02/27 23:48:05 tgl Exp $
*
* Double Metaphone computes 2 "sounds like" strings - a primary and an
* alternate. In most cases they are the same, but for foreign names
* especially they can be a bit different, depending on pronunciation.
*
* Information on using Double Metaphone can be found at
* http://www.codeproject.com/string/dmetaphone1.asp
* and the original article describing it can be found at
* http://www.cuj.com/documents/s=8038/cuj0006philips/
*
* For PostgreSQL we provide 2 functions - one for the primary and one for
* the alternate. That way the functions are pure text->text mappings that
* are useful in functional indexes. These are 'dmetaphone' for the
* primary and 'dmetaphone_alt' for the alternate.
*
* Assuming that dmetaphone.so is in $libdir, the SQL to set up the
* functions looks like this:
*
* CREATE FUNCTION dmetaphone (text) RETURNS text
* LANGUAGE C IMMUTABLE STRICT
* AS '$libdir/dmetaphone', 'dmetaphone';
*
* CREATE FUNCTION dmetaphone_alt (text) RETURNS text
* LANGUAGE C IMMUTABLE STRICT
* AS '$libdir/dmetaphone', 'dmetaphone_alt';
*
* Note that you have to declare the functions IMMUTABLE if you want to
* use them in functional indexes, and you have to declare them as STRICT
* as they do not check for NULL input, and will segfault if given NULL input.
* (See below for alternative ) Declaring them as STRICT means PostgreSQL
* will never call them with NULL, but instead assume the result is NULL,
* which is what we (I) want.
*
* Alternatively, compile with -DDMETAPHONE_NOSTRICT and the functions
* will detect NULL input and return NULL. The you don't have to declare them
* as STRICT.
*
* There is a small inefficiency here - each function call actually computes
* both the primary and the alternate and then throws away the one it doesn't
* need. That's the way the perl module was written, because perl can handle
* a list return more easily than we can in PostgreSQL. The result has been
* fast enough for my needs, but it could maybe be optimized a bit to remove
* that behaviour.
*
*/
/***************************** COPYRIGHT NOTICES ***********************
Most of this code is directly from the Text::DoubleMetaphone perl module
version 0.05 available from http://www.cpan.org.
It bears this copyright notice:
Copyright 2000, Maurice Aubrey <[email protected]>.
All rights reserved.
This code is based heavily on the C++ implementation by
Lawrence Philips and incorporates several bug fixes courtesy
of Kevin Atkinson <[email protected]>.
This module is free software; you may redistribute it and/or
modify it under the same terms as Perl itself.
The remaining code is authored by Andrew Dunstan <[email protected]> and
<[email protected]> and is covered this copyright:
Copyright 2003, North Carolina State Highway Patrol.
All rights reserved.
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose, without fee, and without a written agreement
is hereby granted, provided that the above copyright notice and this
paragraph and the following two paragraphs appear in all copies.
IN NO EVENT SHALL THE NORTH CAROLINA STATE HIGHWAY PATROL BE LIABLE TO ANY
PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
DOCUMENTATION, EVEN IF THE NORTH CAROLINA STATE HIGHWAY PATROL HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE NORTH CAROLINA STATE HIGHWAY PATROL SPECIFICALLY DISCLAIMS ANY
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED
HEREUNDER IS ON AN "AS IS" BASIS, AND THE NORTH CAROLINA STATE HIGHWAY PATROL
HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
MODIFICATIONS.
***********************************************************************/
/* include these first, according to the docs */
#ifndef DMETAPHONE_MAIN
#include "postgres.h"
#include "fmgr.h"
/* turn off assertions for embedded function */
#define NDEBUG
#endif
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
extern Datum dmetaphone(PG_FUNCTION_ARGS);
extern Datum dmetaphone_alt(PG_FUNCTION_ARGS);
/* prototype for the main function we got from the perl module */
void DoubleMetaphone(char *, char **);
#ifndef DMETAPHONE_MAIN
/*
* The PostgreSQL visible dmetaphone function.
*
*/
PG_FUNCTION_INFO_V1(dmetaphone);
Datum
dmetaphone(PG_FUNCTION_ARGS)
{
text *arg,
*result;
int alen,
rsize;
char *aptr,
*codes[2],
*code,
*rptr;
#ifdef DMETAPHONE_NOSTRICT
if (PG_ARGISNULL(0))
PG_RETURNNULL();
#endif
arg = PG_GETARG_TEXT_P(0);
alen = VARSIZE(arg) - VARHDRSZ;
/*
* Postgres' string values might not have trailing nuls. The VARSIZE will
* not include the nul in any case so we copy things out and add a
* trailing nul. When we copy back we ignore the nul (and we don't make
* space for it).
*/
aptr = palloc(alen + 1);
memcpy(aptr, VARDATA(arg), alen);
aptr[alen] = 0;
DoubleMetaphone(aptr, codes);
code = codes[0];
if (!code)
code = "";
rsize = VARHDRSZ + strlen(code);
result = (text *) palloc(rsize);
rptr = VARDATA(result);
memcpy(rptr, code, rsize - VARHDRSZ);
SET_VARSIZE(result, rsize);
PG_RETURN_TEXT_P(result);
}
/*
* The PostgreSQL visible dmetaphone_alt function.
*
*/
PG_FUNCTION_INFO_V1(dmetaphone_alt);
Datum
dmetaphone_alt(PG_FUNCTION_ARGS)
{
text *arg,
*result;
int alen,
rsize;
char *aptr,
*codes[2],
*code,
*rptr;
#ifdef DMETAPHONE_NOSTRICT
if (PG_ARGISNULL(0))
PG_RETURNNULL();
#endif
arg = PG_GETARG_TEXT_P(0);
alen = VARSIZE(arg) - VARHDRSZ;
aptr = palloc(alen + 1);
memcpy(aptr, VARDATA(arg), alen);
aptr[alen] = 0;
DoubleMetaphone(aptr, codes);
code = codes[1];
if (!code)
code = "";
rsize = VARHDRSZ + strlen(code);
result = (text *) palloc(rsize);
rptr = VARDATA(result);
memcpy(rptr, code, rsize - VARHDRSZ);
SET_VARSIZE(result, rsize);
PG_RETURN_TEXT_P(result);
}
/* here is where we start the code imported from the perl module */
/* all memory handling is done with these macros */
#define META_MALLOC(v,n,t) \
(v = (t*)palloc(((n)*sizeof(t))))
#define META_REALLOC(v,n,t) \
(v = (t*)repalloc((v),((n)*sizeof(t))))
/*
* Don't do pfree - it seems to cause a segv sometimes - which might have just
* been caused by reloading the module in development.
* So we rely on context cleanup - Tom Lane says pfree shouldn't be necessary
* in a case like this.
*/
#define META_FREE(x) /* pfree((x)) */
#else /* not defined DMETAPHONE_MAIN */
/* use the standard malloc library when not running in PostgreSQL */
#define META_MALLOC(v,n,t) \
(v = (t*)malloc(((n)*sizeof(t))))
#define META_REALLOC(v,n,t) \
(v = (t*)realloc((v),((n)*sizeof(t))))
#define META_FREE(x) free((x))
#endif /* defined DMETAPHONE_MAIN */
/* this typedef was originally in the perl module's .h file */
typedef struct
{
char *str;
int length;
int bufsize;
int free_string_on_destroy;
} metastring;
/*
* remaining perl module funcs unchanged except for declaring them static
* and reformatting to PostgreSQL indentation and to fit in 80 cols.
*
*/
static metastring *
NewMetaString(char *init_str)
{
metastring *s;
char empty_string[] = "";
META_MALLOC(s, 1, metastring);
assert(s != NULL);
if (init_str == NULL)
init_str = empty_string;
s->length = strlen(init_str);
/* preallocate a bit more for potential growth */
s->bufsize = s->length + 7;
META_MALLOC(s->str, s->bufsize, char);
assert(s->str != NULL);
strncpy(s->str, init_str, s->length + 1);
s->free_string_on_destroy = 1;
return s;
}
static void
DestroyMetaString(metastring * s)
{
if (s == NULL)
return;
if (s->free_string_on_destroy && (s->str != NULL))
META_FREE(s->str);
META_FREE(s);
}
static void
IncreaseBuffer(metastring * s, int chars_needed)
{
META_REALLOC(s->str, (s->bufsize + chars_needed + 10), char);
assert(s->str != NULL);
s->bufsize = s->bufsize + chars_needed + 10;
}
static void
MakeUpper(metastring * s)
{
char *i;
for (i = s->str; *i; i++)
*i = toupper((unsigned char) *i);
}
static int
IsVowel(metastring * s, int pos)
{
char c;
if ((pos < 0) || (pos >= s->length))
return 0;
c = *(s->str + pos);
if ((c == 'A') || (c == 'E') || (c == 'I') || (c == 'O') ||
(c == 'U') || (c == 'Y'))
return 1;
return 0;
}
static int
SlavoGermanic(metastring * s)
{
if ((char *) strstr(s->str, "W"))
return 1;
else if ((char *) strstr(s->str, "K"))
return 1;
else if ((char *) strstr(s->str, "CZ"))
return 1;
else if ((char *) strstr(s->str, "WITZ"))
return 1;
else
return 0;
}
static char
GetAt(metastring * s, int pos)
{
if ((pos < 0) || (pos >= s->length))
return '\0';
return ((char) *(s->str + pos));
}
static void
SetAt(metastring * s, int pos, char c)
{
if ((pos < 0) || (pos >= s->length))
return;
*(s->str + pos) = c;
}
/*
Caveats: the START value is 0 based
*/
static int
StringAt(metastring * s, int start, int length,...)
{
char *test;
char *pos;
va_list ap;
if ((start < 0) || (start >= s->length))
return 0;
pos = (s->str + start);
va_start(ap, length);
do
{
test = va_arg(ap, char *);
if (*test && (strncmp(pos, test, length) == 0))
return 1;
}
while (strcmp(test, ""));
va_end(ap);
return 0;
}
static void
MetaphAdd(metastring * s, char *new_str)
{
int add_length;
if (new_str == NULL)
return;
add_length = strlen(new_str);
if ((s->length + add_length) > (s->bufsize - 1))
IncreaseBuffer(s, add_length);
strcat(s->str, new_str);
s->length += add_length;
}
void
DoubleMetaphone(char *str, char **codes)
{
int length;
metastring *original;
metastring *primary;
metastring *secondary;
int current;
int last;
current = 0;
/* we need the real length and last prior to padding */
length = strlen(str);
last = length - 1;
original = NewMetaString(str);
/* Pad original so we can index beyond end */
MetaphAdd(original, " ");
primary = NewMetaString("");
secondary = NewMetaString("");
primary->free_string_on_destroy = 0;
secondary->free_string_on_destroy = 0;
MakeUpper(original);
/* skip these when at start of word */
if (StringAt(original, 0, 2, "GN", "KN", "PN", "WR", "PS", ""))
current += 1;
/* Initial 'X' is pronounced 'Z' e.g. 'Xavier' */
if (GetAt(original, 0) == 'X')
{
MetaphAdd(primary, "S"); /* 'Z' maps to 'S' */
MetaphAdd(secondary, "S");
current += 1;
}
/* main loop */
while ((primary->length < 4) || (secondary->length < 4))
{
if (current >= length)
break;
switch (GetAt(original, current))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
if (current == 0)
{
/* all init vowels now map to 'A' */
MetaphAdd(primary, "A");
MetaphAdd(secondary, "A");
}
current += 1;
break;
case 'B':
/* "-mb", e.g", "dumb", already skipped over... */
MetaphAdd(primary, "P");
MetaphAdd(secondary, "P");
if (GetAt(original, current + 1) == 'B')
current += 2;
else
current += 1;
break;
case 'Ç':
MetaphAdd(primary, "S");
MetaphAdd(secondary, "S");
current += 1;
break;
case 'C':
/* various germanic */
if ((current > 1)
&& !IsVowel(original, current - 2)
&& StringAt(original, (current - 1), 3, "ACH", "")
&& ((GetAt(original, current + 2) != 'I')
&& ((GetAt(original, current + 2) != 'E')
|| StringAt(original, (current - 2), 6, "BACHER",
"MACHER", ""))))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
current += 2;
break;
}
/* special case 'caesar' */
if ((current == 0)
&& StringAt(original, current, 6, "CAESAR", ""))
{
MetaphAdd(primary, "S");
MetaphAdd(secondary, "S");
current += 2;
break;
}
/* italian 'chianti' */
if (StringAt(original, current, 4, "CHIA", ""))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
current += 2;
break;
}
if (StringAt(original, current, 2, "CH", ""))
{
/* find 'michael' */
if ((current > 0)
&& StringAt(original, current, 4, "CHAE", ""))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "X");
current += 2;
break;
}
/* greek roots e.g. 'chemistry', 'chorus' */
if ((current == 0)
&& (StringAt(original, (current + 1), 5,
"HARAC", "HARIS", "")
|| StringAt(original, (current + 1), 3, "HOR",
"HYM", "HIA", "HEM", ""))
&& !StringAt(original, 0, 5, "CHORE", ""))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
current += 2;
break;
}
/* germanic, greek, or otherwise 'ch' for 'kh' sound */
if (
(StringAt(original, 0, 4, "VAN ", "VON ", "")
|| StringAt(original, 0, 3, "SCH", ""))
/* 'architect but not 'arch', 'orchestra', 'orchid' */
|| StringAt(original, (current - 2), 6, "ORCHES",
"ARCHIT", "ORCHID", "")
|| StringAt(original, (current + 2), 1, "T", "S",
"")
|| ((StringAt(original, (current - 1), 1,
"A", "O", "U", "E", "")
|| (current == 0))
/*
* e.g., 'wachtler', 'wechsler', but not 'tichner'
*/
&& StringAt(original, (current + 2), 1, "L", "R",
"N", "M", "B", "H", "F", "V", "W",
" ", "")))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
}
else
{
if (current > 0)
{
if (StringAt(original, 0, 2, "MC", ""))
{
/* e.g., "McHugh" */
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
}
else
{
MetaphAdd(primary, "X");
MetaphAdd(secondary, "K");
}
}
else
{
MetaphAdd(primary, "X");
MetaphAdd(secondary, "X");
}
}
current += 2;
break;
}
/* e.g, 'czerny' */
if (StringAt(original, current, 2, "CZ", "")
&& !StringAt(original, (current - 2), 4, "WICZ", ""))
{
MetaphAdd(primary, "S");
MetaphAdd(secondary, "X");
current += 2;
break;
}
/* e.g., 'focaccia' */
if (StringAt(original, (current + 1), 3, "CIA", ""))
{
MetaphAdd(primary, "X");
MetaphAdd(secondary, "X");
current += 3;
break;
}
/* double 'C', but not if e.g. 'McClellan' */
if (StringAt(original, current, 2, "CC", "")
&& !((current == 1) && (GetAt(original, 0) == 'M')))
{
/* 'bellocchio' but not 'bacchus' */
if (StringAt(original, (current + 2), 1, "I", "E", "H", "")
&& !StringAt(original, (current + 2), 2, "HU", ""))
{
/* 'accident', 'accede' 'succeed' */
if (
((current == 1)
&& (GetAt(original, current - 1) == 'A'))
|| StringAt(original, (current - 1), 5, "UCCEE",
"UCCES", ""))
{
MetaphAdd(primary, "KS");
MetaphAdd(secondary, "KS");
/* 'bacci', 'bertucci', other italian */
}
else
{
MetaphAdd(primary, "X");
MetaphAdd(secondary, "X");
}
current += 3;
break;
}
else
{ /* Pierce's rule */
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
current += 2;
break;
}
}
if (StringAt(original, current, 2, "CK", "CG", "CQ", ""))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
current += 2;
break;
}
if (StringAt(original, current, 2, "CI", "CE", "CY", ""))
{
/* italian vs. english */
if (StringAt
(original, current, 3, "CIO", "CIE", "CIA", ""))
{
MetaphAdd(primary, "S");
MetaphAdd(secondary, "X");
}
else
{
MetaphAdd(primary, "S");
MetaphAdd(secondary, "S");
}
current += 2;
break;
}
/* else */
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
/* name sent in 'mac caffrey', 'mac gregor */
if (StringAt(original, (current + 1), 2, " C", " Q", " G", ""))
current += 3;
else if (StringAt(original, (current + 1), 1, "C", "K", "Q", "")
&& !StringAt(original, (current + 1), 2,
"CE", "CI", ""))
current += 2;
else
current += 1;
break;
case 'D':
if (StringAt(original, current, 2, "DG", ""))
{
if (StringAt(original, (current + 2), 1,
"I", "E", "Y", ""))
{
/* e.g. 'edge' */
MetaphAdd(primary, "J");
MetaphAdd(secondary, "J");
current += 3;
break;
}
else
{
/* e.g. 'edgar' */
MetaphAdd(primary, "TK");
MetaphAdd(secondary, "TK");
current += 2;
break;
}
}
if (StringAt(original, current, 2, "DT", "DD", ""))
{
MetaphAdd(primary, "T");
MetaphAdd(secondary, "T");
current += 2;
break;
}
/* else */
MetaphAdd(primary, "T");
MetaphAdd(secondary, "T");
current += 1;
break;
case 'F':
if (GetAt(original, current + 1) == 'F')
current += 2;
else
current += 1;
MetaphAdd(primary, "F");
MetaphAdd(secondary, "F");
break;
case 'G':
if (GetAt(original, current + 1) == 'H')
{
if ((current > 0) && !IsVowel(original, current - 1))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
current += 2;
break;
}
if (current < 3)
{
/* 'ghislane', ghiradelli */
if (current == 0)
{
if (GetAt(original, current + 2) == 'I')
{
MetaphAdd(primary, "J");
MetaphAdd(secondary, "J");
}
else
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
}
current += 2;
break;
}
}
/*
* Parker's rule (with some further refinements) - e.g.,
* 'hugh'
*/
if (
((current > 1)
&& StringAt(original, (current - 2), 1,
"B", "H", "D", ""))
/* e.g., 'bough' */
|| ((current > 2)
&& StringAt(original, (current - 3), 1,
"B", "H", "D", ""))
/* e.g., 'broughton' */
|| ((current > 3)
&& StringAt(original, (current - 4), 1,
"B", "H", "")))
{
current += 2;
break;
}
else
{
/*
* e.g., 'laugh', 'McLaughlin', 'cough', 'gough',
* 'rough', 'tough'
*/
if ((current > 2)
&& (GetAt(original, current - 1) == 'U')
&& StringAt(original, (current - 3), 1, "C",
"G", "L", "R", "T", ""))
{
MetaphAdd(primary, "F");
MetaphAdd(secondary, "F");
}
else if ((current > 0)
&& GetAt(original, current - 1) != 'I')
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
}
current += 2;
break;
}
}
if (GetAt(original, current + 1) == 'N')
{
if ((current == 1) && IsVowel(original, 0)
&& !SlavoGermanic(original))
{
MetaphAdd(primary, "KN");
MetaphAdd(secondary, "N");
}
else
/* not e.g. 'cagney' */
if (!StringAt(original, (current + 2), 2, "EY", "")
&& (GetAt(original, current + 1) != 'Y')
&& !SlavoGermanic(original))
{
MetaphAdd(primary, "N");
MetaphAdd(secondary, "KN");
}
else
{
MetaphAdd(primary, "KN");
MetaphAdd(secondary, "KN");
}
current += 2;
break;
}
/* 'tagliaro' */
if (StringAt(original, (current + 1), 2, "LI", "")
&& !SlavoGermanic(original))
{
MetaphAdd(primary, "KL");
MetaphAdd(secondary, "L");
current += 2;
break;
}
/* -ges-,-gep-,-gel-, -gie- at beginning */
if ((current == 0)
&& ((GetAt(original, current + 1) == 'Y')
|| StringAt(original, (current + 1), 2, "ES", "EP",
"EB", "EL", "EY", "IB", "IL", "IN", "IE",
"EI", "ER", "")))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "J");
current += 2;
break;
}
/* -ger-, -gy- */
if (
(StringAt(original, (current + 1), 2, "ER", "")
|| (GetAt(original, current + 1) == 'Y'))
&& !StringAt(original, 0, 6,
"DANGER", "RANGER", "MANGER", "")
&& !StringAt(original, (current - 1), 1, "E", "I", "")
&& !StringAt(original, (current - 1), 3, "RGY", "OGY",
""))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "J");
current += 2;
break;
}
/* italian e.g, 'biaggi' */
if (StringAt(original, (current + 1), 1, "E", "I", "Y", "")
|| StringAt(original, (current - 1), 4,
"AGGI", "OGGI", ""))
{
/* obvious germanic */
if (
(StringAt(original, 0, 4, "VAN ", "VON ", "")
|| StringAt(original, 0, 3, "SCH", ""))
|| StringAt(original, (current + 1), 2, "ET", ""))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
}
else
{
/* always soft if french ending */
if (StringAt
(original, (current + 1), 4, "IER ", ""))
{
MetaphAdd(primary, "J");
MetaphAdd(secondary, "J");
}
else
{
MetaphAdd(primary, "J");
MetaphAdd(secondary, "K");
}
}
current += 2;
break;
}
if (GetAt(original, current + 1) == 'G')
current += 2;
else
current += 1;
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
break;
case 'H':
/* only keep if first & before vowel or btw. 2 vowels */
if (((current == 0) || IsVowel(original, current - 1))
&& IsVowel(original, current + 1))
{
MetaphAdd(primary, "H");
MetaphAdd(secondary, "H");
current += 2;
}
else
/* also takes care of 'HH' */
current += 1;
break;
case 'J':
/* obvious spanish, 'jose', 'san jacinto' */
if (StringAt(original, current, 4, "JOSE", "")
|| StringAt(original, 0, 4, "SAN ", ""))
{
if (((current == 0)
&& (GetAt(original, current + 4) == ' '))
|| StringAt(original, 0, 4, "SAN ", ""))
{
MetaphAdd(primary, "H");
MetaphAdd(secondary, "H");
}
else
{
MetaphAdd(primary, "J");
MetaphAdd(secondary, "H");
}
current += 1;
break;
}
if ((current == 0)
&& !StringAt(original, current, 4, "JOSE", ""))
{
MetaphAdd(primary, "J"); /* Yankelovich/Jankelowicz */
MetaphAdd(secondary, "A");
}
else
{
/* spanish pron. of e.g. 'bajador' */
if (IsVowel(original, current - 1)
&& !SlavoGermanic(original)
&& ((GetAt(original, current + 1) == 'A')
|| (GetAt(original, current + 1) == 'O')))
{
MetaphAdd(primary, "J");
MetaphAdd(secondary, "H");
}
else
{
if (current == last)
{
MetaphAdd(primary, "J");
MetaphAdd(secondary, "");
}
else
{
if (!StringAt(original, (current + 1), 1, "L", "T",
"K", "S", "N", "M", "B", "Z", "")
&& !StringAt(original, (current - 1), 1,
"S", "K", "L", ""))
{
MetaphAdd(primary, "J");
MetaphAdd(secondary, "J");