-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmetaphone3.go
4173 lines (3610 loc) · 113 KB
/
metaphone3.go
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
package metaphone3
import (
"fmt"
"unicode"
)
// debug flag to output additional data during encoding
var debug = false
// DefaultMaxLength is the max number of runes in a result when not specified in the encoder
var DefaultMaxLength = 8
// Encoder is a metaphone3 encoder that contains options and state for encoding. It is not
// safe to use across goroutines.
type Encoder struct {
// EncodeVowels determines if Metaphone3 will encode non-initial vowels. However, even
// if there are more than one vowel sound in a vowel sequence (i.e.
// vowel diphthong, etc.), only one 'A' will be encoded before the next consonant or the
// end of the word.
EncodeVowels bool
// EncodeExact controls if Metaphone3 will encode consonants as exactly as possible.
// This does not include 'S' vs. 'Z', since americans will pronounce 'S' at the
// at the end of many words as 'Z', nor does it include "CH" vs. "SH". It does cause
// a distinction to be made between 'B' and 'P', 'D' and 'T', 'G' and 'K', and 'V'
// and 'F'.
EncodeExact bool
// The max allowed length of the output metaphs, if <= 0 then the DefaultMaxLength is used
MaxLength int
in []rune
idx int
lastIdx int
primBuf, secondBuf []rune
flagAlInversion bool
}
// Encode takes in a string and returns primary and secondary metaphones.
// Both will be blank if given a blank input, and secondary can be blank
// if there's only one metaphone.
func (e *Encoder) Encode(in string) (primary, secondary string) {
if in == "" {
return "", ""
}
if e.MaxLength <= 0 {
e.MaxLength = DefaultMaxLength
}
e.flagAlInversion = false
// setup our input buffer and to-upper everything
e.in = make([]rune, 0, len(in))
for _, r := range in {
e.in = append(e.in, unicode.ToUpper(r))
}
e.lastIdx = len(e.in) - 1
e.primBuf = primeBuf(e.primBuf, e.MaxLength)
e.secondBuf = primeBuf(e.secondBuf, e.MaxLength)
// lets go rune-by-rune through the input string
for e.idx = 0; e.idx < len(e.in); e.idx++ {
// double check our output buffers, if they're full then we're done
// we're not checking exact "=" just be compat with the reference java implementation
// that means our buffers could be longer than MaxLength by a bit
if len(e.primBuf) >= e.MaxLength && len(e.secondBuf) >= e.MaxLength {
break
}
if debug {
fmt.Printf("Processing %v\n", string(e.in[e.idx]))
}
switch c := e.in[e.idx]; c {
case 'B':
e.encodeB()
case 'ß', 'Ç':
e.metaphAdd('S')
case 'C':
e.encodeC()
case 'D':
e.encodeD()
case 'F':
e.encodeF()
case 'G':
e.encodeG()
case 'H':
e.encodeH()
case 'J':
e.encodeJ()
case 'K':
e.encodeK()
case 'L':
e.encodeL()
case 'M':
e.encodeM()
case 'N':
e.encodeN()
case 'Ñ':
e.metaphAdd('N')
case 'P':
e.encodeP()
case 'Q':
e.encodeQ()
case 'R':
e.encodeR()
case 'S':
e.encodeS()
case 'T':
e.encodeT()
case 'Ð', 'Þ':
e.metaphAdd('0')
case 'V':
e.encodeV()
case 'W':
e.encodeW()
case 'X':
e.encodeX()
case '\uC28A':
//wat?
e.metaphAdd('X')
case '\uC28E':
//wat?
e.metaphAdd('S')
case 'Z':
e.encodeZ()
default:
if isVowel(c) {
e.encodeVowels()
}
}
}
// trim our buffers if needed
if len(e.primBuf) > e.MaxLength {
e.primBuf = e.primBuf[:e.MaxLength]
}
if len(e.secondBuf) > e.MaxLength {
e.secondBuf = e.secondBuf[:e.MaxLength]
}
if areEqual(e.primBuf, e.secondBuf) {
return string(e.primBuf), ""
}
return string(e.primBuf), string(e.secondBuf)
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Detailed encoder functions
//////////////////////////////////////////////////////////////////////////////////////////////////////
func (e *Encoder) encodeB() {
if e.encodeSilentB() {
return
}
// "-mb", e.g", "dumb", already skipped over under
// 'M', altho it should really be handled here...
e.metaphAddExactApprox("B", "P")
// skip double B, or BPx where X isn't H
if e.charNextIs('B') ||
(e.charNextIs('P') && e.idx+2 < len(e.in) && e.in[e.idx+2] != 'H') {
e.idx++
}
}
// Encodes silent 'B' for cases not covered under "-mb-"
func (e *Encoder) encodeSilentB() bool {
//'debt', 'doubt', 'subtle'
if e.stringAt(-2, "DEBT", "SUBTL", "SUBTIL") || e.stringAt(-3, "DOUBT") {
e.metaphAdd('T')
e.idx++
return true
}
return false
}
func (e *Encoder) encodeC() {
if e.encodeSilentCAtBeginning() ||
e.encodeCaToS() ||
e.encodeCoToS() ||
e.encodeCh() ||
e.encodeCcia() ||
e.encodeCc() ||
e.encodeCkCgCq() ||
e.encodeCFrontVowel() ||
e.encodeSilentC() ||
e.encodeCz() ||
e.encodeCs() {
return
}
if !e.stringAt(-1, "C", "K", "G", "Q") {
e.metaphAdd('K')
}
//name sent in 'mac caffrey', 'mac gregor
if e.stringAt(1, " C", " Q", " G") {
e.idx++
} else {
if e.stringAt(1, "C", "K", "Q") && !e.stringAt(1, "CE", "CI") {
e.idx++ // increment 1 here, so adjust offsets below
// account for combinations such as Ro-ckc-liffe
if e.stringAt(1, "C", "K", "Q") && !e.stringAt(2, "CE", "CI") {
e.idx++
}
}
}
}
func (e *Encoder) encodeSilentCAtBeginning() bool {
if e.idx == 0 && e.stringAt(0, "CT", "CN") {
return true
}
return false
}
//Encodes exceptions where "-CA-" should encode to S
//instead of K including cases where the cedilla has not been used
func (e *Encoder) encodeCaToS() bool {
// Special case: 'caesar'.
// Also, where cedilla not used, as in "linguica" => LNKS
if (e.idx == 0 && e.stringAt(0, "CAES", "CAEC", "CAEM")) ||
e.stringStart("FACADE", "FRANCAIS", "FRANCAIX", "LINGUICA", "GONCALVES", "PROVENCAL") {
e.metaphAdd('S')
e.advanceCounter(1, 0)
return true
}
return false
}
//Encodes exceptions where "-CO-" encodes to S instead of K
//including cases where the cedilla has not been used
func (e *Encoder) encodeCoToS() bool {
// e.g. 'coelecanth' => SLKN0
if e.stringAt(0, "COEL") && (e.isVowelAt(4) || e.idx+3 == e.lastIdx) ||
e.stringAt(0, "COENA", "COENO") || e.stringStart("GARCON", "FRANCOIS", "MELANCON") {
e.metaphAdd('S')
e.advanceCounter(2, 0)
return true
}
return false
}
func (e *Encoder) encodeCh() bool {
if !e.stringAt(0, "CH") {
return false
}
if e.encodeChae() ||
e.encodeChToH() ||
e.encodeSilentCh() ||
e.encodeArch() ||
e.encodeChToX() ||
e.encodeEnglishChToK() ||
e.encodeGermanicChToK() ||
e.encodeGreekChInitial() ||
e.encodeGreekChNonInitial() {
return true
}
if e.idx > 0 {
if e.stringStart("MC") && e.idx == 1 {
//e.g., "McHugh"
e.metaphAdd('K')
} else {
e.metaphAddAlt('X', 'K')
}
} else {
e.metaphAdd('X')
}
e.idx++
return true
}
func (e *Encoder) encodeChae() bool {
// e.g. 'michael'
if e.idx > 0 && e.stringAt(2, "AE") {
if e.stringStart("RACHAEL") {
e.metaphAdd('X')
} else if !e.stringAt(-1, "C", "K", "G", "Q") {
e.metaphAdd('K')
}
e.advanceCounter(3, 1)
return true
}
return false
}
// Encodes transliterations from the hebrew where the
// sound 'kh' is represented as "-CH-". The normal pronounciation
// of this in english is either 'h' or 'kh', and alternate
// spellings most often use "-H-"
func (e *Encoder) encodeChToH() bool {
// hebrew => 'H', e.g. 'channukah', 'chabad'
if (e.idx == 0 &&
(e.stringAt(2, "AIM", "ETH", "ELM", "ASID", "AZAN",
"UPPAH", "UTZPA", "ALLAH", "ALUTZ", "AMETZ",
"ESHVAN", "ADARIM", "ANUKAH", "ALLLOTH", "ANNUKAH", "AROSETH"))) ||
e.stringAt(-3, "CLACHAN") {
e.metaphAdd('H')
e.advanceCounter(2, 1)
return true
}
return false
}
func (e *Encoder) encodeSilentCh() bool {
if e.stringAt(-2, "YACHT", "FUCHSIA") ||
e.stringStart("STRACHAN", "CRICHTON") ||
(e.stringAt(-3, "DRACHM") && !e.stringAt(-3, "DRACHMA")) {
e.idx++
return true
}
return false
}
func (e *Encoder) encodeChToX() bool {
// e.g. 'approach', 'beach'
if (e.stringAt(-2, "OACH", "EACH", "EECH", "OUCH", "OOCH", "MUCH", "SUCH") && !e.stringAt(-3, "JOACH")) ||
e.stringAtEnd(-1, "ACHA", "ACHO") || // e.g. 'dacha', 'macho'
e.stringAtEnd(0, "CHOT", "CHOD", "CHAT") ||
(e.stringAtEnd(-1, "OCHE") && !e.stringAt(-2, "DOCHE")) ||
e.stringAt(-4, "ATTACH", "DETACH", "KOVACH", "PARACHUT") ||
e.stringAt(-5, "SPINACH", "MASSACHU") ||
e.stringStart("MACHAU") ||
(e.stringAt(-3, "THACH") && !e.stringAt(2, "E")) || // no ACHE
e.stringAt(-2, "VACHON") {
e.metaphAdd('X')
e.idx++
return true
}
return false
}
func (e *Encoder) encodeEnglishChToK() bool {
//'ache', 'echo', alternate spelling of 'michael'
if (e.idx == 1 && rootOrInflections(e.in, "ACHE")) ||
((e.idx > 3 && rootOrInflections(e.in[e.idx-1:], "ACHE")) &&
e.stringStart("EAR", "HEAD", "BACK", "HEART", "BELLY", "TOOTH")) ||
e.stringAt(-1, "ECHO") ||
e.stringAt(-2, "MICHEAL") ||
e.stringAt(-4, "JERICHO") ||
e.stringAt(-5, "LEPRECH") {
e.metaphAddAlt('K', 'X')
e.idx++
return true
}
return false
}
func (e *Encoder) encodeGermanicChToK() bool {
// various germanic
// "<consonant><vowel>CH-"implies a german word where 'ch' => K
if (e.idx > 1 &&
!e.isVowelAt(-2) &&
e.stringAt(-1, "ACH") &&
!e.stringAt(-2, "MACHADO", "MACHUCA", "LACHANC", "LACHAPE", "KACHATU") &&
!e.stringAt(-3, "KHACHAT") &&
(!e.charAt(2, 'I') && (!e.charAt(2, 'E') || e.stringAt(-2, "BACHER", "MACHER", "MACHEN", "LACHER"))) ||
// e.g. 'brecht', 'fuchs'
(e.stringAt(2, "T", "S") && !(e.stringStart("LUNCHTIME", "WHICHSOEVER"))) ||
// e.g. 'andromache'
e.stringStart("SCHR") ||
(e.idx > 2 && e.stringAt(-2, "MACHE")) ||
(e.idx == 2 && e.stringAt(-2, "ZACH")) ||
e.stringAt(-4, "SCHACH") ||
e.stringAt(-1, "ACHEN") ||
e.stringAt(-3, "SPICH", "ZURCH", "BUECH") ||
(e.stringAt(-3, "KIRCH", "JOACH", "BLECH", "MALCH") &&
!(e.stringAt(-3, "KIRCHNER") || e.idx+1 == e.lastIdx)) || // "kirch" and "blech" both get 'X'
e.stringAtEnd(-2, "NICH", "LICH", "BACH") ||
(e.stringAtEnd(-3, "URICH", "BRICH", "ERICH", "DRICH", "NRICH") &&
!e.stringAtEnd(-5, "ALDRICH") &&
!e.stringAtEnd(-6, "GOODRICH") &&
!e.stringAtEnd(-7, "GINGERICH"))) ||
e.stringAtEnd(-4, "ULRICH", "LFRICH", "LLRICH", "EMRICH", "ZURICH", "EYRICH") ||
// e.g., 'wachtler', 'wechsler', but not 'tichner'
((e.stringAt(-1, "A", "O", "U", "E") || e.idx == 0) &&
e.stringAt(2, "L", "R", "N", "M", "B", "H", "F", "V", "W", " ")) {
// "CHR/L-" e.g. 'chris' do not get
// alt pronunciation of 'X'
if e.stringAt(2, "R", "L") || e.isSlavoGermanic() {
e.metaphAdd('K')
} else {
e.metaphAddAlt('K', 'X')
}
e.idx++
return true
}
return false
}
// Encode "-ARCH-". Some occurances are from greek roots and therefore encode
// to 'K', others are from english words and therefore encode to 'X'
func (e *Encoder) encodeArch() bool {
if e.stringAt(-2, "ARCH") {
// "-ARCH-" has many combining forms where "-CH-" => K because of its
// derivation from the greek
if ((e.isVowelAt(2) && e.stringAt(-2, "ARCHA", "ARCHI", "ARCHO", "ARCHU", "ARCHY")) ||
e.stringAt(-2, "ARCHEA", "ARCHEG", "ARCHEO", "ARCHET", "ARCHEL", "ARCHES", "ARCHEP", "ARCHEM", "ARCHEN") ||
e.stringAtEnd(-2, "ARCH") ||
e.stringStart("MENARCH")) &&
(!rootOrInflections(e.in, "ARCH") &&
!e.stringAt(-4, "SEARCH", "POARCH") &&
!e.stringStart("ARCHER", "ARCHIE", "ARCHENEMY", "ARCHIBALD", "ARCHULETA", "ARCHAMBAU") &&
!((((e.stringAt(-3, "LARCH", "MARCH", "PARCH") ||
e.stringAt(-4, "STARCH")) &&
!e.stringStart("EPARCH", "NOMARCH", "EXILARCH", "HIPPARCH", "MARCHESE", "ARISTARCH", "MARCHETTI")) ||
rootOrInflections(e.in, "STARCH")) &&
(!e.stringAt(-2, "ARCHU", "ARCHY") || e.stringStart("STARCHY")))) {
e.metaphAddAlt('K', 'X')
} else {
e.metaphAdd('X')
}
e.idx++
return true
}
return false
}
func (e *Encoder) encodeGreekChInitial() bool {
// greek roots e.g. 'chemistry', 'chorus', ch at beginning of root
if (e.stringAt(0, "CHAMOM", "CHARAC", "CHARIS", "CHARTO", "CHARTU", "CHARYB", "CHRIST", "CHEMIC", "CHILIA") ||
(e.stringAt(0, "CHEMI", "CHEMO", "CHEMU", "CHEMY", "CHOND", "CHONA", "CHONI", "CHOIR", "CHASM",
"CHARO", "CHROM", "CHROI", "CHAMA", "CHALC", "CHALD", "CHAET", "CHIRO", "CHILO", "CHELA", "CHOUS",
"CHEIL", "CHEIR", "CHEIM", "CHITI", "CHEOP") && !(e.stringAt(0, "CHEMIN") || e.stringAt(-2, "ANCHONDO"))) ||
(e.stringAt(0, "CHISM", "CHELI") &&
// exclude spanish "machismo"
!(e.stringStart("MICHEL", "MACHISMO", "RICHELIEU", "REVANCHISM") ||
e.stringExact("CHISM"))) ||
// include e.g. "chorus", "chyme", "chaos"
(e.stringAt(0, "CHOR", "CHOL", "CHYM", "CHYL", "CHLO", "CHOS", "CHUS", "CHOE") && !e.stringStart("CHOLLO", "CHOLLA", "CHORIZ")) ||
// "chaos" => K but not "chao"
(e.stringAt(0, "CHAO") && e.idx+3 != e.lastIdx) ||
// e.g. "abranchiate"
(e.stringAt(0, "CHIA") && !(e.stringStart("CHIAPAS", "APPALACHIA"))) ||
// e.g. "chimera"
e.stringAt(0, "CHIMERA", "CHIMAER", "CHIMERI") ||
// e.g. "chameleon"
e.stringStart("CHAME", "CHELO", "CHITO") ||
// e.g. "spirochete"
((e.idx+4 == e.lastIdx || e.idx+5 == e.lastIdx) && e.stringAt(-1, "OCHETE"))) &&
// more exceptions where "-CH-" => X e.g. "chortle", "crocheter"
!(e.stringExact("CHORE", "CHOLO", "CHOLA") ||
e.stringAt(0, "CHORT", "CHOSE") ||
e.stringAt(-3, "CROCHET") ||
e.stringStart("CHEMISE", "CHARISE", "CHARISS", "CHAROLE")) {
if e.stringAt(2, "R", "L") {
e.metaphAdd('K')
} else {
e.metaphAddAlt('K', 'X')
}
e.idx++
return true
}
return false
}
func (e *Encoder) encodeGreekChNonInitial() bool {
//greek & other roots e.g. 'tachometer', 'orchid', ch in middle or end of root
if e.stringAt(-2, "LYCHN", "TACHO", "ORCHO", "ORCHI", "LICHO", "ORCHID", "NICHOL",
"MECHAN", "LICHEN", "MACHIC", "PACHEL", "RACHIF", "RACHID",
"RACHIS", "RACHIC", "MICHAL", "ORCHESTR") ||
e.stringAt(-3, "MELCH", "GLOCH", "TRACH", "TROCH", "BRACH", "SYNCH", "PSYCH",
"STICH", "PULCH", "EPOCH") ||
(e.stringAt(-3, "TRICH") && !e.stringAt(-5, "OSTRICH")) ||
(e.stringAt(-2, "TYCH", "TOCH", "BUCH", "MOCH", "CICH", "DICH", "NUCH", "EICH", "LOCH",
"DOCH", "ZECH", "WYCH") && !(e.stringAt(-4, "INDOCHINA") || e.stringAt(-2, "BUCHON"))) ||
((e.idx == 1 || e.idx == 2) && e.stringAt(-1, "OCHER", "ECHIN", "ECHID")) ||
e.stringAt(-4, "BRONCH", "STOICH", "STRYCH", "TELECH", "PLANCH", "CATECH", "MANICH", "MALACH",
"BIANCH", "DIDACH", "BRANCHIO", "BRANCHIF") ||
e.stringStart("ICHA", "ICHN") ||
(e.stringAt(-1, "ACHAB", "ACHAD", "ACHAN", "ACHAZ") && !e.stringAt(-2, "MACHADO", "LACHANC")) ||
e.stringAt(-1, "ACHISH", "ACHILL", "ACHAIA", "ACHENE", "ACHAIAN", "ACHATES", "ACHIRAL", "ACHERON",
"ACHILLEA", "ACHIMAAS", "ACHILARY", "ACHELOUS", "ACHENIAL", "ACHERNAR",
"ACHALASIA", "ACHILLEAN", "ACHIMENES", "ACHIMELECH", "ACHITOPHEL") ||
// e.g. 'inchoate'
(e.idx == 2 && (e.stringStart("INCHOA")) ||
// e.g. 'ischemia'
e.stringStart("ISCH")) ||
// e.g. 'ablimelech', 'antioch', 'pentateuch'
(e.idx+1 == e.lastIdx && e.stringAt(-1, "A", "O", "U", "E") &&
!(e.stringStart("DEBAUCH") || e.stringAt(-2, "MUCH", "SUCH", "KOCH") ||
e.stringAt(-5, "OODRICH", "ALDRICH"))) {
e.metaphAddAlt('K', 'X')
e.idx++
return true
}
return false
}
//Encodes reliably italian "-CCIA-"
func (e *Encoder) encodeCcia() bool {
//e.g., 'focaccia'
if e.stringAt(1, "CIA") {
e.metaphAddAlt('X', 'S')
e.idx++
return true
}
return false
}
func (e *Encoder) encodeCc() bool {
//double 'C', but not if e.g. 'McClellan'
if e.stringAt(0, "CC") && !(e.idx == 1 && e.in[0] == 'M') {
// exception
if e.stringAt(-3, "FLACCID") {
e.metaphAdd('S')
e.advanceCounter(2, 1)
return true
}
//'bacci', 'bertucci', other italian
if e.stringAtEnd(2, "I") ||
e.stringAt(2, "IO") || e.stringAtEnd(2, "INO", "INI") {
e.metaphAdd('X')
e.advanceCounter(2, 1)
return true
}
//'accident', 'accede' 'succeed'
if e.stringAt(2, "I", "E", "Y") && //except 'bellocchio','bacchus', 'soccer' get K
!(e.charAt(2, 'H') || e.stringAt(-2, "SOCCER")) {
e.metaphAddStr("KS", "KS")
e.advanceCounter(2, 1)
return true
}
// Pierce's rule
e.metaphAdd('K')
e.idx++
return true
}
return false
}
func (e *Encoder) encodeCkCgCq() bool {
if e.stringAt(0, "CK", "CG", "CQ") {
// eastern european spelling e.g. 'gorecki' == 'goresky'
if e.stringAtEnd(0, "CKI", "CKY") && len(e.in) > 6 {
e.metaphAddStr("K", "SK")
} else {
e.metaphAdd('K')
}
e.idx++ // skip the C
// if there's a C[KGQ][KGQ] then skip that second one too
if e.stringAt(1, "K", "G", "Q") {
e.idx++
}
return true
}
return false
}
//Encode cases where "C" preceeds a front vowel such as "E", "I", or "Y".
//These cases most likely => S or X
func (e *Encoder) encodeCFrontVowel() bool {
if e.stringAt(0, "CI", "CE", "CY") {
if e.encodeBritishSilentCE() ||
e.encodeCe() ||
e.encodeCi() ||
e.encodeLatinateSuffixes() {
e.advanceCounter(1, 0)
return true
}
e.metaphAdd('S')
e.advanceCounter(1, 0)
return true
}
return false
}
func (e *Encoder) encodeBritishSilentCE() bool {
// english place names like e.g.'gloucester' pronounced glo-ster
if e.stringAtEnd(1, "ESTER") || e.stringAt(1, "ESTERSHIRE") {
return true
}
return false
}
func (e *Encoder) encodeCe() bool {
// 'ocean', 'commercial', 'provincial', 'cello', 'fettucini', 'medici'
if (e.stringAt(1, "EAN") && e.isVowelAt(-1)) ||
(e.stringAtEnd(-1, "ACEA") && !e.stringStart("PANACEA")) || // e.g. 'rosacea'
e.stringAt(1, "ELLI", "ERTO", "EORL") || // e.g. 'botticelli', 'concerto'
e.stringAtEnd(-3, "CROCE") || // some italian names familiar to americans
e.stringAt(-3, "DOLCE") ||
e.stringAtEnd(1, "ELLO") { // e.g. cello
e.metaphAddAlt('X', 'S')
return true
}
return false
}
func (e *Encoder) encodeCi() bool {
// with consonant before C
// e.g. 'fettucini', but exception for the americanized pronunciation of 'mancini'
if (e.stringAtEnd(1, "INI") && !e.stringExact("MANCINI")) ||
e.stringAtEnd(-1, "ICI") || // e.g. 'medici'
e.stringAt(-1, "RCIAL", "NCIAL", "RCIAN", "UCIUS") || // e.g. "commercial', 'provincial', 'cistercian'
e.stringAt(-3, "MARCIA") || // special cases
e.stringAt(-2, "ANCIENT") {
e.metaphAddAlt('X', 'S')
return true
}
// exception
if e.stringAt(-4, "COERCION") {
e.metaphAdd('J')
return true
}
// with vowel before C (or at beginning?)
if (e.stringAt(0, "CIO", "CIE", "CIA") && e.isVowelAt(-1)) ||
e.stringAt(1, "IAO") {
if (e.stringAt(0, "CIAN", "CIAL", "CIAO", "CIES", "CIOL", "CION") ||
e.stringAt(-3, "GLACIER") || // exception - "glacier" => 'X' but "spacier" = > 'S'
e.stringAt(0, "CIENT", "CIENC", "CIOUS", "CIATE", "CIATI", "CIATO", "CIABL", "CIARY") ||
e.stringAtEnd(0, "CIA", "CIO", "CIAS", "CIOS")) &&
!(e.stringAt(-4, "ASSOCIATION") || e.stringStart("OCIE") ||
// exceptions mostly because these names are usually from
// the spanish rather than the italian in america
e.stringAt(-2, "LUCIO", "SOCIO", "SOCIE", "MACIAS", "LUCIANO", "HACIENDA") ||
e.stringAt(-3, "GRACIE", "GRACIA", "MARCIANO") ||
e.stringAt(-4, "PALACIO", "POLICIES", "FELICIANO") ||
e.stringAt(-5, "MAURICIO") ||
e.stringAt(-6, "ANDALUCIA") ||
e.stringAt(-7, "ENCARNACION")) {
e.metaphAddAlt('X', 'S')
} else {
e.metaphAddAlt('S', 'X')
}
return true
}
return false
}
func (e *Encoder) encodeLatinateSuffixes() bool {
if e.stringAt(1, "EOUS", "IOUS") {
e.metaphAddAlt('X', 'S')
return true
}
return false
}
func (e *Encoder) encodeSilentC() bool {
if e.stringAt(1, "T", "S") && e.stringStart("INDICT", "TUCSON", "CONNECTICUT") {
return true
}
return false
}
// Encodes slavic spellings or transliterations
// written as "-CZ-"
func (e *Encoder) encodeCz() bool {
if e.stringAt(1, "Z") && !e.stringAt(-1, "ECZEMA") {
if e.stringAt(0, "CZAR") {
e.metaphAdd('S')
} else {
// otherwise most likely a czech word...
e.metaphAdd('X')
}
e.idx++
return true
}
return false
}
func (e *Encoder) encodeCs() bool {
// give an 'etymological' 2nd
// encoding for "kovacs" so
// that it matches "kovach"
if e.stringStart("KOVACS") {
e.metaphAddStr("KS", "X")
e.idx++
return true
}
if e.stringAtEnd(-1, "ACS") && !e.stringAt(-4, "ISAACS") {
e.metaphAdd('X')
e.idx++
return true
}
return false
}
func (e *Encoder) encodeD() {
if e.encodeDg() || e.encodeDj() || e.encodeDtDd() ||
e.encodeDToJ() || e.encodeDous() || e.encodeSilentD() {
return
}
if e.EncodeExact {
// "final de-voicing" in this case
// e.g. 'missed' == 'mist'
if e.stringAtEnd(-3, "SSED") {
e.metaphAdd('T')
} else {
e.metaphAdd('D')
}
} else {
e.metaphAdd('T')
}
}
func (e *Encoder) encodeDg() bool {
if e.stringAt(0, "DG") {
// excludes exceptions e.g. 'edgar',
// or cases where 'g' is first letter of combining form
// e.g. 'handgun', 'waldglas'
if e.stringAt(2, "A", "O") ||
// e.g. "midgut"
// e.g. "handgrip"
// e.g. "mudgard"
// e.g. "woodgrouse"
e.stringAt(1, "GUN", "GUT", "GEAR", "GLAS", "GRIP", "GREN", "GILL", "GRAF",
"GUARD", "GUILT", "GRAVE", "GRASS", "GROUSE") {
e.metaphAddExactApprox("DG", "TK")
} else {
// e.g. "edge", "abridgment"
e.metaphAdd('J')
}
e.idx++
return true
}
return false
}
func (e *Encoder) encodeDj() bool {
// e.g. "adjacent"
if e.stringAt(0, "DJ") {
e.metaphAdd('J')
e.idx++
return true
}
return false
}
func (e *Encoder) encodeDtDd() bool {
// eat redundant 'T' or 'D'
if e.stringAt(0, "DT", "DD") {
if e.stringAt(0, "DTH") {
e.metaphAddExactApprox("D0", "T0")
e.idx += 2
} else {
if e.EncodeExact {
// devoice it
if e.stringAt(0, "DT") {
e.metaphAdd('T')
} else {
e.metaphAdd('D')
}
} else {
e.metaphAdd('T')
}
e.idx++
}
return true
}
return false
}
func (e *Encoder) encodeDToJ() bool {
// e.g. "module", "adulate"
if (e.stringAt(0, "DUL") && e.isVowelAt(-1) && e.isVowelAt(3)) ||
// e.g. "soldier", "grandeur", "procedure"
e.stringAtEnd(-1, "LDIER", "NDEUR", "EDURE", "RDURE") ||
e.stringAt(-3, "CORDIAL") ||
// e.g. "pendulum", "education"
// e.g. "individual", "individual", "residuum"
e.stringAt(-1, "ADUA", "IDUA", "IDUU", "NDULA", "NDULU", "EDUCA") {
e.metaphAddExactApproxAlt("J", "D", "J", "T")
e.advanceCounter(1, 0)
return true
}
return false
}
func (e *Encoder) encodeDous() bool {
// e.g. "assiduous", "arduous"
if e.stringAt(1, "UOUS") {
e.metaphAddExactApproxAlt("J", "D", "J", "T")
e.advanceCounter(3, 0)
return true
}
return false
}
func (e *Encoder) encodeSilentD() bool {
// silent 'D' e.g. 'wednesday', 'handsome'
return e.stringAt(-2, "WEDNESDAY") ||
e.stringAt(-3, "HANDKER", "HANDSOM", "WINDSOR") ||
// french silent D at end in words or names familiar to americans
e.stringEnd("PERNOD", "ARTAUD", "RENAUD", "RIMBAUD", "MICHAUD", "BICHAUD")
}
func (e *Encoder) encodeF() {
// Encode cases where "-FT-" => "T" is usually silent
// e.g. 'often', 'soften'
// This should really be covered under "T"!
if e.stringAt(-1, "OFTEN") {
e.metaphAddStr("F", "FT")
e.idx++
return
}
// eat redundant 'F'
if e.charNextIs('F') {
e.idx++
}
e.metaphAdd('F')
}
func (e *Encoder) encodeG() {
if e.encodeSilentGAtBeginning() || e.encodeGg() || e.encodeGk() || e.encodeGh() || e.encodeSilentG() ||
e.encodeGn() || e.encodeGl() || e.encodeInitialGFrontVowel() || e.encodeNger() || e.encodeGer() ||
e.encodeGel() || e.encodeNonInitialGFrontVowel() || e.encodeGaToJ() {
return
}
if !e.stringAt(-1, "C", "K", "G", "Q") {
e.metaphAddExactApprox("G", "K")
}
}
func (e *Encoder) encodeSilentGAtBeginning() bool {
return e.stringAtStart(0, "GN")
}
func (e *Encoder) encodeGg() bool {
if e.charNextIs('G') {
// italian e.g, 'loggia', 'caraveggio', also 'suggest' and 'exaggerate'
if e.stringAt(-1, "AGGIA", "OGGIA", "AGGIO", "EGGIO", "EGGIA", "IGGIO") ||
// 'ruggiero' but not 'snuggies'
(e.stringAt(-1, "UGGIE") && !(e.idx+3 == e.lastIdx || e.idx+4 == e.lastIdx)) ||
e.stringAtEnd(-1, "AGGI", "OGGI") ||
e.stringAt(-2, "SUGGES", "XAGGER", "REGGIE") {
// expection where "-GG-" => KJ
if e.stringAt(-2, "SUGGEST") {
e.metaphAddExactApprox("G", "K")
}
e.metaphAdd('J')
e.advanceCounter(2, 1)
} else {
e.metaphAddExactApprox("G", "K")
e.idx++
}
return true
}
return false
}
func (e *Encoder) encodeGk() bool {
// 'gingko'
if e.charNextIs('K') {
e.metaphAdd('K')
e.idx++
return true
}
return false
}
func (e *Encoder) encodeGh() bool {
if e.charNextIs('H') {
if e.encodeGhAfterConsonant() || e.encodeInitialGh() || e.encodeGhToJ() || e.encodeGhToH() ||
e.encodeUght() || e.encodeGhHPartOfOtherWord() || e.encodeSilentGh() || e.encodeGhToF() {
return true
}
e.metaphAddExactApprox("G", "K")
e.idx++
return true
}
return false
}
func (e *Encoder) encodeGhAfterConsonant() bool {
// e.g. 'burgher', 'bingham'
if e.idx > 0 && !e.isVowelAt(-1) &&
// not e.g. 'greenhalgh'
!e.stringAtEnd(-3, "HALGH") {
e.metaphAddExactApprox("G", "K")
e.idx++
return true
}
return false
}
func (e *Encoder) encodeInitialGh() bool {
if e.idx == 0 {
// e.g. "ghislane", "ghiradelli"
if e.charAt(2, 'I') {
e.metaphAdd('J')
} else {
e.metaphAddExactApprox("G", "K")
}
e.idx++
return true
}
return false
}
func (e *Encoder) encodeGhToJ() bool {
// e.g., 'greenhalgh', 'dunkenhalgh', english names
if e.stringAtEnd(-2, "ALGH") {
e.metaphAddAlt('J', unicode.ReplacementChar)
e.idx++
return true
}
return false
}
func (e *Encoder) encodeGhToH() bool {
// special cases
// e.g., 'donoghue', 'donaghy'
if (e.stringAt(-4, "DONO", "DONA") && e.isVowelAt(2)) ||
e.stringAt(-5, "CALLAGHAN") {
e.metaphAdd('H')
e.idx++
return true
}
return false
}
func (e *Encoder) encodeUght() bool {
// e.g. "ought", "aught", "daughter", "slaughter"
if e.stringAt(-1, "UGHT") {
if (e.stringAt(-3, "LAUGH") && !(e.stringAt(-4, "SLAUGHT") || e.stringAt(-3, "LAUGHTO"))) ||
e.stringAt(-4, "DRAUGH") {
e.metaphAddStr("FT", "FT")
} else {
e.metaphAdd('T')
}
e.idx += 2
return true
}
return false
}
func (e *Encoder) encodeGhHPartOfOtherWord() bool {
// if the 'H' is the beginning of another word or syllable