forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleMetaphone.cs
More file actions
1277 lines (1210 loc) · 44.1 KB
/
Copy pathDoubleMetaphone.cs
File metadata and controls
1277 lines (1210 loc) · 44.1 KB
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
// commons-codec version compatibility level: 1.9
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
namespace Lucene.Net.Analysis.Phonetic.Language
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <summary>
/// Encodes a string into a double metaphone value. This Implementation is based on the algorithm by <c>Lawrence
/// Philips</c>.
/// <para/>
/// This class is conditionally thread-safe. The instance field <see cref="maxCodeLen"/> is mutable
/// <see cref="MaxCodeLen"/> but is not volatile, and accesses are not synchronized. If an instance of the class is
/// shared between threads, the caller needs to ensure that suitable synchronization is used to ensure safe publication
/// of the value between threads, and must not set <see cref="MaxCodeLen"/> after initial setup.
/// <para/>
/// See <a href="http://drdobbs.com/184401251?pgno=2">Original Article</a>
/// <para/>
/// See <a href="http://en.wikipedia.org/wiki/Metaphone">http://en.wikipedia.org/wiki/Metaphone</a>
/// </summary>
public class DoubleMetaphone : IStringEncoder
{
/// <summary>
/// "Vowels" to test for
/// </summary>
private const string VOWELS = "AEIOUY";
/// <summary>
/// Prefixes when present which are not pronounced
/// </summary>
private static readonly string[] SILENT_START =
{ "GN", "KN", "PN", "WR", "PS" };
private static readonly string[] L_R_N_M_B_H_F_V_W_SPACE =
{ "L", "R", "N", "M", "B", "H", "F", "V", "W", " " };
private static readonly string[] ES_EP_EB_EL_EY_IB_IL_IN_IE_EI_ER =
{ "ES", "EP", "EB", "EL", "EY", "IB", "IL", "IN", "IE", "EI", "ER" };
private static readonly string[] L_T_K_S_N_M_B_Z =
{ "L", "T", "K", "S", "N", "M", "B", "Z" };
/// <summary>
/// Maximum length of an encoding, default is 4
/// </summary>
private int maxCodeLen = 4;
/// <summary>
/// Creates an instance of this <see cref="DoubleMetaphone"/> encoder
/// </summary>
public DoubleMetaphone()
: base()
{
}
/// <summary>
/// Encode a value with Double Metaphone.
/// </summary>
/// <param name="value">String to encode.</param>
/// <returns>An encoded string.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual string GetDoubleMetaphone(string value)
{
return GetDoubleMetaphone(value, false);
}
/// <summary>
/// Encode a value with Double Metaphone, optionally using the alternate encoding.
/// </summary>
/// <param name="value">String to encode.</param>
/// <param name="alternate">Use alternate encode.</param>
/// <returns>An encoded string.</returns>
public virtual string GetDoubleMetaphone(string value, bool alternate)
{
value = CleanInput(value);
if (value is null)
{
return null;
}
bool slavoGermanic = IsSlavoGermanic(value);
int index = IsSilentStart(value) ? 1 : 0;
DoubleMetaphoneResult result = new DoubleMetaphoneResult(this.MaxCodeLen);
while (!result.IsComplete && index <= value.Length - 1)
{
switch (value[index])
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
index = HandleAEIOUY(result, index);
break;
case 'B':
result.Append('P');
index = CharAt(value, index + 1) == 'B' ? index + 2 : index + 1;
break;
case '\u00C7':
// A C with a Cedilla
result.Append('S');
index++;
break;
case 'C':
index = HandleC(value, result, index);
break;
case 'D':
index = HandleD(value, result, index);
break;
case 'F':
result.Append('F');
index = CharAt(value, index + 1) == 'F' ? index + 2 : index + 1;
break;
case 'G':
index = HandleG(value, result, index, slavoGermanic);
break;
case 'H':
index = HandleH(value, result, index);
break;
case 'J':
index = HandleJ(value, result, index, slavoGermanic);
break;
case 'K':
result.Append('K');
index = CharAt(value, index + 1) == 'K' ? index + 2 : index + 1;
break;
case 'L':
index = HandleL(value, result, index);
break;
case 'M':
result.Append('M');
index = ConditionM0(value, index) ? index + 2 : index + 1;
break;
case 'N':
result.Append('N');
index = CharAt(value, index + 1) == 'N' ? index + 2 : index + 1;
break;
case '\u00D1':
// N with a tilde (spanish ene)
result.Append('N');
index++;
break;
case 'P':
index = HandleP(value, result, index);
break;
case 'Q':
result.Append('K');
index = CharAt(value, index + 1) == 'Q' ? index + 2 : index + 1;
break;
case 'R':
index = HandleR(value, result, index, slavoGermanic);
break;
case 'S':
index = HandleS(value, result, index, slavoGermanic);
break;
case 'T':
index = HandleT(value, result, index);
break;
case 'V':
result.Append('F');
index = CharAt(value, index + 1) == 'V' ? index + 2 : index + 1;
break;
case 'W':
index = HandleW(value, result, index);
break;
case 'X':
index = HandleX(value, result, index);
break;
case 'Z':
index = HandleZ(value, result, index, slavoGermanic);
break;
default:
index++;
break;
}
}
return alternate ? result.Alternate : result.Primary;
}
// LUCENENET specific - in .NET we don't need an object overload of Encode(), since strings are sealed anyway.
/// <summary>
/// Encode the value using DoubleMetaphone.
/// </summary>
/// <param name="value">String to encode.</param>
/// <returns>An encoded string.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual string Encode(string value)
{
return GetDoubleMetaphone(value);
}
/// <summary>
/// Check if the Double Metaphone values of two <see cref="string"/> values
/// are equal.
/// </summary>
/// <param name="value1">The left-hand side of the encoded <see cref="string.Equals(object)"/>.</param>
/// <param name="value2">The right-hand side of the encoded <see cref="string.Equals(object)"/>.</param>
/// <returns><c>true</c> if the encoded <see cref="string"/>s are equal; <c>false</c> otherwise.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual bool IsDoubleMetaphoneEqual(string value1, string value2)
{
return IsDoubleMetaphoneEqual(value1, value2, false);
}
/// <summary>
/// Check if the Double Metaphone values of two <see cref="string"/> values
/// are equal, optionally using the alternate value.
/// </summary>
/// <param name="value1">The left-hand side of the encoded <see cref="string.Equals(object)"/>.</param>
/// <param name="value2">The right-hand side of the encoded <see cref="string.Equals(object)"/>.</param>
/// <param name="alternate">Use the alternate value if <c>true</c>.</param>
/// <returns><c>true</c> if the encoded <see cref="string"/>s are equal; <c>false</c> otherwise.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual bool IsDoubleMetaphoneEqual(string value1, string value2, bool alternate)
{
return GetDoubleMetaphone(value1, alternate).Equals(GetDoubleMetaphone(value2, alternate), StringComparison.Ordinal);
}
/// <summary>
/// Gets or Sets the maxCodeLen.
/// </summary>
public virtual int MaxCodeLen
{
get => this.maxCodeLen;
set => this.maxCodeLen = value;
}
//-- BEGIN HANDLERS --//
/// <summary>
/// Handles 'A', 'E', 'I', 'O', 'U', and 'Y' cases.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int HandleAEIOUY(DoubleMetaphoneResult result, int index) // LUCENENET: CA1822: Mark members as static
{
if (index == 0)
{
result.Append('A');
}
return index + 1;
}
/// <summary>
/// Handles 'C' cases.
/// </summary>
private int HandleC(string value, DoubleMetaphoneResult result, int index)
{
if (ConditionC0(value, index))
{ // very confusing, moved out
result.Append('K');
index += 2;
}
else if (index == 0 && Contains(value, index, 6, "CAESAR"))
{
result.Append('S');
index += 2;
}
else if (Contains(value, index, 2, "CH"))
{
index = HandleCH(value, result, index);
}
else if (Contains(value, index, 2, "CZ") &&
!Contains(value, index - 2, 4, "WICZ"))
{
//-- "Czerny" --//
result.Append('S', 'X');
index += 2;
}
else if (Contains(value, index + 1, 3, "CIA"))
{
//-- "focaccia" --//
result.Append('X');
index += 3;
}
else if (Contains(value, index, 2, "CC") &&
!(index == 1 && CharAt(value, 0) == 'M'))
{
//-- double "cc" but not "McClelland" --//
return HandleCC(value, result, index);
}
else if (Contains(value, index, 2, "CK", "CG", "CQ"))
{
result.Append('K');
index += 2;
}
else if (Contains(value, index, 2, "CI", "CE", "CY"))
{
//-- Italian vs. English --//
if (Contains(value, index, 3, "CIO", "CIE", "CIA"))
{
result.Append('S', 'X');
}
else
{
result.Append('S');
}
index += 2;
}
else
{
result.Append('K');
if (Contains(value, index + 1, 2, " C", " Q", " G"))
{
//-- Mac Caffrey, Mac Gregor --//
index += 3;
}
else if (Contains(value, index + 1, 1, "C", "K", "Q") &&
!Contains(value, index + 1, 2, "CE", "CI"))
{
index += 2;
}
else
{
index++;
}
}
return index;
}
/// <summary>
/// Handles 'CC' cases.
/// </summary>
private int HandleCC(string value, DoubleMetaphoneResult result, int index)
{
if (Contains(value, index + 2, 1, "I", "E", "H") &&
!Contains(value, index + 2, 2, "HU"))
{
//-- "bellocchio" but not "bacchus" --//
if ((index == 1 && CharAt(value, index - 1) == 'A') ||
Contains(value, index - 1, 5, "UCCEE", "UCCES"))
{
//-- "accident", "accede", "succeed" --//
result.Append("KS");
}
else
{
//-- "bacci", "bertucci", other Italian --//
result.Append('X');
}
index += 3;
}
else
{ // Pierce's rule
result.Append('K');
index += 2;
}
return index;
}
/// <summary>
/// Handles 'CH' cases.
/// </summary>
private static int HandleCH(string value, DoubleMetaphoneResult result, int index) // LUCENENET: CA1822: Mark members as static
{
if (index > 0 && Contains(value, index, 4, "CHAE"))
{ // Michael
result.Append('K', 'X');
return index + 2;
}
else if (ConditionCH0(value, index))
{
//-- Greek roots ("chemistry", "chorus", etc.) --//
result.Append('K');
return index + 2;
}
else if (ConditionCH1(value, index))
{
//-- Germanic, Greek, or otherwise 'ch' for 'kh' sound --//
result.Append('K');
return index + 2;
}
else
{
if (index > 0)
{
if (Contains(value, 0, 2, "MC"))
{
result.Append('K');
}
else
{
result.Append('X', 'K');
}
}
else
{
result.Append('X');
}
return index + 2;
}
}
/// <summary>
/// Handles 'D' cases.
/// </summary>
private static int HandleD(string value, DoubleMetaphoneResult result, int index) // LUCENENET: CA1822: Mark members as static
{
if (Contains(value, index, 2, "DG"))
{
//-- "Edge" --//
if (Contains(value, index + 2, 1, "I", "E", "Y"))
{
result.Append('J');
index += 3;
//-- "Edgar" --//
}
else
{
result.Append("TK");
index += 2;
}
}
else if (Contains(value, index, 2, "DT", "DD"))
{
result.Append('T');
index += 2;
}
else
{
result.Append('T');
index++;
}
return index;
}
/// <summary>
/// Handles 'G' cases.
/// </summary>
private int HandleG(string value, DoubleMetaphoneResult result, int index,
bool slavoGermanic)
{
if (CharAt(value, index + 1) == 'H')
{
index = HandleGH(value, result, index);
}
else if (CharAt(value, index + 1) == 'N')
{
if (index == 1 && IsVowel(CharAt(value, 0)) && !slavoGermanic)
{
result.Append("KN", "N");
}
else if (!Contains(value, index + 2, 2, "EY") &&
CharAt(value, index + 1) != 'Y' && !slavoGermanic)
{
result.Append("N", "KN");
}
else
{
result.Append("KN");
}
index = index + 2;
}
else if (Contains(value, index + 1, 2, "LI") && !slavoGermanic)
{
result.Append("KL", "L");
index += 2;
}
else if (index == 0 &&
(CharAt(value, index + 1) == 'Y' ||
Contains(value, index + 1, 2, ES_EP_EB_EL_EY_IB_IL_IN_IE_EI_ER)))
{
//-- -ges-, -gep-, -gel-, -gie- at beginning --//
result.Append('K', 'J');
index += 2;
}
else if ((Contains(value, index + 1, 2, "ER") ||
CharAt(value, index + 1) == 'Y') &&
!Contains(value, 0, 6, "DANGER", "RANGER", "MANGER") &&
!Contains(value, index - 1, 1, "E", "I") &&
!Contains(value, index - 1, 3, "RGY", "OGY"))
{
//-- -ger-, -gy- --//
result.Append('K', 'J');
index += 2;
}
else if (Contains(value, index + 1, 1, "E", "I", "Y") ||
Contains(value, index - 1, 4, "AGGI", "OGGI"))
{
//-- Italian "biaggi" --//
if (Contains(value, 0, 4, "VAN ", "VON ") ||
Contains(value, 0, 3, "SCH") ||
Contains(value, index + 1, 2, "ET"))
{
//-- obvious germanic --//
result.Append('K');
}
else if (Contains(value, index + 1, 3, "IER"))
{
result.Append('J');
}
else
{
result.Append('J', 'K');
}
index += 2;
}
else if (CharAt(value, index + 1) == 'G')
{
index += 2;
result.Append('K');
}
else
{
index++;
result.Append('K');
}
return index;
}
/// <summary>
/// Handles 'GH' cases.
/// </summary>
private int HandleGH(string value, DoubleMetaphoneResult result, int index)
{
if (index > 0 && !IsVowel(CharAt(value, index - 1)))
{
result.Append('K');
index += 2;
}
else if (index == 0)
{
if (CharAt(value, index + 2) == 'I')
{
result.Append('J');
}
else
{
result.Append('K');
}
index += 2;
}
else if ((index > 1 && Contains(value, index - 2, 1, "B", "H", "D")) ||
(index > 2 && Contains(value, index - 3, 1, "B", "H", "D")) ||
(index > 3 && Contains(value, index - 4, 1, "B", "H")))
{
//-- Parker's rule (with some further refinements) - "hugh"
index += 2;
}
else
{
if (index > 2 && CharAt(value, index - 1) == 'U' &&
Contains(value, index - 3, 1, "C", "G", "L", "R", "T"))
{
//-- "laugh", "McLaughlin", "cough", "gough", "rough", "tough"
result.Append('F');
}
else if (index > 0 && CharAt(value, index - 1) != 'I')
{
result.Append('K');
}
index += 2;
}
return index;
}
/// <summary>
/// Handles 'H' cases.
/// </summary>
private int HandleH(string value, DoubleMetaphoneResult result, int index)
{
//-- only keep if first & before vowel or between 2 vowels --//
if ((index == 0 || IsVowel(CharAt(value, index - 1))) &&
IsVowel(CharAt(value, index + 1)))
{
result.Append('H');
index += 2;
//-- also takes car of "HH" --//
}
else
{
index++;
}
return index;
}
/// <summary>
/// Handles 'J' cases.
/// </summary>
private int HandleJ(string value, DoubleMetaphoneResult result, int index,
bool slavoGermanic)
{
if (Contains(value, index, 4, "JOSE") || Contains(value, 0, 4, "SAN "))
{
//-- obvious Spanish, "Jose", "San Jacinto" --//
if ((index == 0 && (CharAt(value, index + 4) == ' ') ||
value.Length == 4) || Contains(value, 0, 4, "SAN "))
{
result.Append('H');
}
else
{
result.Append('J', 'H');
}
index++;
}
else
{
if (index == 0 && !Contains(value, index, 4, "JOSE"))
{
result.Append('J', 'A');
}
else if (IsVowel(CharAt(value, index - 1)) && !slavoGermanic &&
(CharAt(value, index + 1) == 'A' || CharAt(value, index + 1) == 'O'))
{
result.Append('J', 'H');
}
else if (index == value.Length - 1)
{
result.Append('J', ' ');
}
else if (!Contains(value, index + 1, 1, L_T_K_S_N_M_B_Z) &&
!Contains(value, index - 1, 1, "S", "K", "L"))
{
result.Append('J');
}
if (CharAt(value, index + 1) == 'J')
{
index += 2;
}
else
{
index++;
}
}
return index;
}
/// <summary>
/// Handles 'L' cases.
/// </summary>
private int HandleL(string value, DoubleMetaphoneResult result, int index)
{
if (CharAt(value, index + 1) == 'L')
{
if (ConditionL0(value, index))
{
result.AppendPrimary('L');
}
else
{
result.Append('L');
}
index += 2;
}
else
{
index++;
result.Append('L');
}
return index;
}
/// <summary>
/// Handles 'P' cases.
/// </summary>
private int HandleP(string value, DoubleMetaphoneResult result, int index)
{
if (CharAt(value, index + 1) == 'H')
{
result.Append('F');
index += 2;
}
else
{
result.Append('P');
index = Contains(value, index + 1, 1, "P", "B") ? index + 2 : index + 1;
}
return index;
}
/// <summary>
/// Handles 'R' cases.
/// </summary>
private int HandleR(string value, DoubleMetaphoneResult result, int index,
bool slavoGermanic)
{
if (index == value.Length - 1 && !slavoGermanic &&
Contains(value, index - 2, 2, "IE") &&
!Contains(value, index - 4, 2, "ME", "MA"))
{
result.AppendAlternate('R');
}
else
{
result.Append('R');
}
return CharAt(value, index + 1) == 'R' ? index + 2 : index + 1;
}
/// <summary>
/// Handles 'S' cases.
/// </summary>
private int HandleS(string value, DoubleMetaphoneResult result, int index,
bool slavoGermanic)
{
if (Contains(value, index - 1, 3, "ISL", "YSL"))
{
//-- special cases "island", "isle", "carlisle", "carlysle" --//
index++;
}
else if (index == 0 && Contains(value, index, 5, "SUGAR"))
{
//-- special case "sugar-" --//
result.Append('X', 'S');
index++;
}
else if (Contains(value, index, 2, "SH"))
{
if (Contains(value, index + 1, 4, "HEIM", "HOEK", "HOLM", "HOLZ"))
{
//-- germanic --//
result.Append('S');
}
else
{
result.Append('X');
}
index += 2;
}
else if (Contains(value, index, 3, "SIO", "SIA") || Contains(value, index, 4, "SIAN"))
{
//-- Italian and Armenian --//
if (slavoGermanic)
{
result.Append('S');
}
else
{
result.Append('S', 'X');
}
index += 3;
}
else if ((index == 0 && Contains(value, index + 1, 1, "M", "N", "L", "W")) ||
Contains(value, index + 1, 1, "Z"))
{
//-- german & anglicisations, e.g. "smith" match "schmidt" //
// "snider" match "schneider" --//
//-- also, -sz- in slavic language although in hungarian it //
// is pronounced "s" --//
result.Append('S', 'X');
index = Contains(value, index + 1, 1, "Z") ? index + 2 : index + 1;
}
else if (Contains(value, index, 2, "SC"))
{
index = HandleSC(value, result, index);
}
else
{
if (index == value.Length - 1 && Contains(value, index - 2, 2, "AI", "OI"))
{
//-- french e.g. "resnais", "artois" --//
result.AppendAlternate('S');
}
else
{
result.Append('S');
}
index = Contains(value, index + 1, 1, "S", "Z") ? index + 2 : index + 1;
}
return index;
}
/// <summary>
/// Handles 'SC' cases.
/// </summary>
private int HandleSC(string value, DoubleMetaphoneResult result, int index)
{
if (CharAt(value, index + 2) == 'H')
{
//-- Schlesinger's rule --//
if (Contains(value, index + 3, 2, "OO", "ER", "EN", "UY", "ED", "EM"))
{
//-- Dutch origin, e.g. "school", "schooner" --//
if (Contains(value, index + 3, 2, "ER", "EN"))
{
//-- "schermerhorn", "schenker" --//
result.Append("X", "SK");
}
else
{
result.Append("SK");
}
}
else
{
if (index == 0 && !IsVowel(CharAt(value, 3)) && CharAt(value, 3) != 'W')
{
result.Append('X', 'S');
}
else
{
result.Append('X');
}
}
}
else if (Contains(value, index + 2, 1, "I", "E", "Y"))
{
result.Append('S');
}
else
{
result.Append("SK");
}
return index + 3;
}
/// <summary>
/// Handles 'T' cases.
/// </summary>
private static int HandleT(string value, DoubleMetaphoneResult result, int index) // LUCENENET: CA1822: Mark members as static
{
if (Contains(value, index, 4, "TION"))
{
result.Append('X');
index += 3;
}
else if (Contains(value, index, 3, "TIA", "TCH"))
{
result.Append('X');
index += 3;
}
else if (Contains(value, index, 2, "TH") || Contains(value, index, 3, "TTH"))
{
if (Contains(value, index + 2, 2, "OM", "AM") ||
//-- special case "thomas", "thames" or germanic --//
Contains(value, 0, 4, "VAN ", "VON ") ||
Contains(value, 0, 3, "SCH"))
{
result.Append('T');
}
else
{
result.Append('0', 'T');
}
index += 2;
}
else
{
result.Append('T');
index = Contains(value, index + 1, 1, "T", "D") ? index + 2 : index + 1;
}
return index;
}
/// <summary>
/// Handles 'W' cases.
/// </summary>
private int HandleW(string value, DoubleMetaphoneResult result, int index)
{
if (Contains(value, index, 2, "WR"))
{
//-- can also be in middle of word --//
result.Append('R');
index += 2;
}
else
{
if (index == 0 && (IsVowel(CharAt(value, index + 1)) ||
Contains(value, index, 2, "WH")))
{
if (IsVowel(CharAt(value, index + 1)))
{
//-- Wasserman should match Vasserman --//
result.Append('A', 'F');
}
else
{
//-- need Uomo to match Womo --//
result.Append('A');
}
index++;
}
else if ((index == value.Length - 1 && IsVowel(CharAt(value, index - 1))) ||
Contains(value, index - 1, 5, "EWSKI", "EWSKY", "OWSKI", "OWSKY") ||
Contains(value, 0, 3, "SCH"))
{
//-- Arnow should match Arnoff --//
result.AppendAlternate('F');
index++;
}
else if (Contains(value, index, 4, "WICZ", "WITZ"))
{
//-- Polish e.g. "filipowicz" --//
result.Append("TS", "FX");
index += 4;
}
else
{
index++;
}
}
return index;
}
/// <summary>
/// Handles 'X' cases.
/// </summary>
private static int HandleX(string value, DoubleMetaphoneResult result, int index) // LUCENENET: CA1822: Mark members as static
{
if (index == 0)
{
result.Append('S');
index++;
}
else
{
if (!((index == value.Length - 1) &&
(Contains(value, index - 3, 3, "IAU", "EAU") ||
Contains(value, index - 2, 2, "AU", "OU"))))
{
//-- French e.g. breaux --//
result.Append("KS");
}
index = Contains(value, index + 1, 1, "C", "X") ? index + 2 : index + 1;
}
return index;
}
/// <summary>
/// Handles 'Z' cases.
/// </summary>
private int HandleZ(string value, DoubleMetaphoneResult result, int index,
bool slavoGermanic)
{
if (CharAt(value, index + 1) == 'H')
{
//-- Chinese pinyin e.g. "zhao" or Angelina "Zhang" --//
result.Append('J');
index += 2;
}
else
{
if (Contains(value, index + 1, 2, "ZO", "ZI", "ZA") ||
(slavoGermanic && (index > 0 && CharAt(value, index - 1) != 'T')))
{
result.Append("S", "TS");
}
else
{
result.Append('S');
}
index = CharAt(value, index + 1) == 'Z' ? index + 2 : index + 1;
}
return index;
}
//-- BEGIN CONDITIONS --//
/// <summary>
/// Complex condition 0 for 'C'.
/// </summary>
private bool ConditionC0(string value, int index)
{
if (Contains(value, index, 4, "CHIA"))
{
return true;
}
else if (index <= 1)
{
return false;
}
else if (IsVowel(CharAt(value, index - 2)))
{
return false;
}
else if (!Contains(value, index - 1, 3, "ACH"))
{
return false;
}
else
{
char c = CharAt(value, index + 2);
return (c != 'I' && c != 'E') ||
Contains(value, index - 2, 6, "BACHER", "MACHER");
}
}