-
Notifications
You must be signed in to change notification settings - Fork 0
/
metaphone3.go
5451 lines (4841 loc) · 172 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
/*
Copyright 2010, Lawrence Philips
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Translated by Andy Jackson.
* A request from the author: Please comment and sign any changes you make to
* the Metaphone 3 reference implementation.
* <br>
* Please do NOT reformat this module to Refine's coding standard,
* but instead keep the original format so that it can be more easily compared
* to any modified fork of the original.
*/
/**
* Metaphone 3<br>
* VERSION 2.1.3
*
* by Lawrence Philips<br>
*
* Metaphone 3 is designed to return an *approximate* phonetic key (and an alternate
* approximate phonetic key when appropriate) that should be the same for English
* words, and most names familiar in the United States, that are pronounced *similarly*.
* The key value is *not* intended to be an *exact* phonetic, or even phonemic,
* representation of the word. This is because a certain degree of 'fuzziness' has
* proven to be useful in compensating for variations in pronunciation, as well as
* misheard pronunciations. For example, although americans are not usually aware of it,
* the letter 's' is normally pronounced 'z' at the end of words such as "sounds".<br><br>
*
* The 'approximate' aspect of the encoding is implemented according to the following rules:<br><br>
*
* (1) All vowels are encoded to the same value - 'A'. If the parameter encodeVowels
* is set to false, only *initial* vowels will be encoded at all. If encodeVowels is set
* to true, 'A' will be encoded at all places in the word that any vowels are normally
* pronounced. 'W' as well as 'Y' are treated as vowels. Although there are differences in
* the pronunciation of 'W' and 'Y' in different circumstances that lead to their being
* classified as vowels under some circumstances and as consonants in others, for the purposes
* of the 'fuzziness' component of the Soundex and Metaphone family of algorithms they will
* be always be treated here as vowels.<br><br>
*
* (2) Voiced and un-voiced consonant pairs are mapped to the same encoded value. This
* means that:<br>
* 'D' and 'T' -> 'T'<br>
* 'B' and 'P' -> 'P'<br>
* 'G' and 'K' -> 'K'<br>
* 'Z' and 'S' -> 'S'<br>
* 'V' and 'F' -> 'F'<br><br>
*
* - In addition to the above voiced/unvoiced rules, 'CH' and 'SH' -> 'X', where 'X'
* represents the "-SH-" and "-CH-" sounds in Metaphone 3 encoding.<br><br>
*
* - Also, the sound that is spelled as "TH" in English is encoded to '0' (zero symbol). (Although
* Americans are not usually aware of it, "TH" is pronounced in a voiced (e.g. "that") as
* well as an unvoiced (e.g. "theater") form, which are naturally mapped to the same encoding.)<br><br>
*
* The encodings in this version of Metaphone 3 are according to pronunciations common in the
* United States. This means that they will be inaccurate for consonant pronunciations that
* are different in the United Kingdom, for example "tube" -> "CHOOBE" -> XAP rather than american TAP.<br><br>
*
* Metaphone 3 was preceded by by Soundex, patented in 1919, and Metaphone and Double Metaphone,
* developed by Lawrence Philips. All of these algorithms resulted in a significant number of
* incorrect encodings. Metaphone3 was tested against a database of about 100 thousand English words,
* names common in the United States, and non-English words found in publications in the United States,
* with an emphasis on words that are commonly mispronounced, prepared by the Moby Words website,
* but with the Moby Words 'phonetic' encodings algorithmically mapped to Double Metaphone encodings.
* Metaphone3 increases the accuracy of encoding of english words, common names, and non-English
* words found in american publications from the 89% for Double Metaphone, to over 98%.<br><br>
*
* DISCLAIMER:
* Anthropomorphic Software LLC claims only that Metaphone 3 will return correct encodings,
* within the 'fuzzy' definition of correct as above, for a very high percentage of correctly
* spelled English and commonly recognized non-English words. Anthropomorphic Software LLC
* warns the user that a number of words remain incorrectly encoded, that misspellings may not
* be encoded 'properly', and that people often have differing ideas about the pronunciation
* of a word. Therefore, Metaphone 3 is not guaranteed to return correct results every time, and
* so a desired target word may very well be missed. Creators of commercial products should
* keep in mind that systems like Metaphone 3 produce a 'best guess' result, and should
* condition the expectations of end users accordingly.<br><br>
*
* METAPHONE3 IS PROVIDED "AS IS" WITHOUT
* WARRANTY OF ANY KIND. LAWRENCE PHILIPS AND ANTHROPOMORPHIC SOFTWARE LLC
* MAKE NO WARRANTIES, EXPRESS OR IMPLIED, THAT IT IS FREE OF ERROR,
* OR ARE CONSISTENT WITH ANY PARTICULAR STANDARD OF MERCHANTABILITY,
* OR THAT IT WILL MEET YOUR REQUIREMENTS FOR ANY PARTICULAR APPLICATION.
* LAWRENCE PHILIPS AND ANTHROPOMORPHIC SOFTWARE LLC DISCLAIM ALL LIABILITY
* FOR DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES RESULTING FROM USE
* OF THIS SOFTWARE.
*
* @author Lawrence Philips
*
* Metaphone 3 is designed to return an <i>approximate</i> phonetic key (and an alternate
* approximate phonetic key when appropriate) that should be the same for English
* words, and most names familiar in the United States, that are pronounced "similarly".
* The key value is <i>not</i> intended to be an exact phonetic, or even phonemic,
* representation of the word. This is because a certain degree of 'fuzziness' has
* proven to be useful in compensating for variations in pronunciation, as well as
* misheard pronunciations. For example, although americans are not usually aware of it,
* the letter 's' is normally pronounced 'z' at the end of words such as "sounds".<br><br>
*
* The 'approximate' aspect of the encoding is implemented according to the following rules:<br><br>
*
* (1) All vowels are encoded to the same value - 'A'. If the parameter encodeVowels
* is set to false, only *initial* vowels will be encoded at all. If encodeVowels is set
* to true, 'A' will be encoded at all places in the word that any vowels are normally
* pronounced. 'W' as well as 'Y' are treated as vowels. Although there are differences in
* the pronunciation of 'W' and 'Y' in different circumstances that lead to their being
* classified as vowels under some circumstances and as consonants in others, for the purposes
* of the 'fuzziness' component of the Soundex and Metaphone family of algorithms they will
* be always be treated here as vowels.<br><br>
*
* (2) Voiced and un-voiced consonant pairs are mapped to the same encoded value. This
* means that:<br>
* 'D' and 'T' -> 'T'<br>
* 'B' and 'P' -> 'P'<br>
* 'G' and 'K' -> 'K'<br>
* 'Z' and 'S' -> 'S'<br>
* 'V' and 'F' -> 'F'<br><br>
*
* - In addition to the above voiced/unvoiced rules, 'CH' and 'SH' -> 'X', where 'X'
* represents the "-SH-" and "-CH-" sounds in Metaphone 3 encoding.<br><br>
*
* - Also, the sound that is spelled as "TH" in English is encoded to '0' (zero symbol). (Although
* americans are not usually aware of it, "TH" is pronounced in a voiced (e.g. "that") as
* well as an unvoiced (e.g. "theater") form, which are naturally mapped to the same encoding.)<br><br>
*
* In the "Exact" encoding, voiced/unvoiced pairs are <i>not</i> mapped to the same encoding, except
* for the voiced and unvoiced versions of 'TH', sounds such as 'CH' and 'SH', and for 'S' and 'Z',
* so that the words whose metaph keys match will in fact be closer in pronunciation that with the
* more approximate setting. Keep in mind that encoding settings for search strings should always
* be exactly the same as the encoding settings of the stored metaph keys in your database!
* Because of the considerably increased accuracy of Metaphone3, it is now possible to use this
* setting and have a very good chance of getting a correct encoding.
* <br><br>
* In the Encode Vowels encoding, all non-initial vowels and diphthongs will be encoded to
* 'A', and there will only be one such vowel encoding character between any two consonants.
* It turns out that there are some surprising wrinkles to encoding non-initial vowels in
* practice, pre-eminently in inversions between spelling and pronunciation such as e.g.
* "wrinkle" => 'RANKAL', where the last two sounds are inverted when spelled.
* <br><br>
* The encodings in this version of Metaphone 3 are according to pronunciations common in the
* United States. This means that they will be inaccurate for consonant pronunciations that
* are different in the United Kingdom, for example "tube" -> "CHOOBE" -> XAP rather than american TAP.
* <br><br>
*
*/
package metaphone3
import (
"strings"
)
/** Default size of key storage allocation */
var MAX_KEY_ALLOCATION = 32
/** Default maximum length of encoded key. */
var DEFAULT_MAX_KEY_LENGTH = 8
type M3 struct {
/** Flag whether or not to encode non-initial vowels. */
encodeVowels bool
/** Flag whether or not to encode consonants as exactly
* as possible. */
encodeExact bool
/** Length of word sent in to be encoded, as
* measured at beginning of encoding. */
length int
/** Running copy of primary key. */
primary strings.Builder
/** Running copy of secondary key. */
secondary strings.Builder
/** Index of character in m.inWord currently being
* encoded. */
current int
/** Index of last character in m.inWord. */
last int
/** Flag that an AL inversion has already been done. */
flag_AL_inversion bool
/** Length of encoded key string. */
metaphLength int
/** Internal copy of word to be encoded, allocated separately
* from pointed to in incoming parameter string. */
inWord string
}
////////////////////////////////////////////////////////////////////////////////
// Metaphone3 class definition
////////////////////////////////////////////////////////////////////////////////
/**
* Constructor, default. This constructor is most convenient when
* encoding more than one word at a time. New words to encode can
* be set using SetWord(char *).
*
*/
func New() *M3 {
return &M3{
metaphLength: DEFAULT_MAX_KEY_LENGTH,
}
}
/**
* Sets length allocated for output keys.
* If incoming number is greater than maximum allowable
* length returned by GetMaximumKeyLength(), set key length
* to maximum key length and return false; otherwise, set key
* length to parameter value and return true.
*
* @param inKeyLength new length of key.
* @return true if able to set key length to requested value.
*
*/
func (m *M3) SetKeyLength(inKeyLength int) bool {
if inKeyLength < 1 {
// can't have that -
// no room for terminating null
inKeyLength = 1
}
if inKeyLength > MAX_KEY_ALLOCATION {
m.metaphLength = MAX_KEY_ALLOCATION
return false
}
m.metaphLength = inKeyLength
return true
}
/**
* Adds an encoding character to the encoded key value string - two parameter version
*
* @param main primary encoding character to be added to encoded key string
* @param alt alternative encoding character to be added to encoded alternative key string
*
*/
func (m *M3) metaphAdd(main string, alt string) {
if !(main == "A" && (m.primary.Len() > 0) && (m.primary.String()[m.primary.Len()-1] == 'A')) {
m.primary.WriteString(main)
}
if !(alt == "A" && (m.secondary.Len() > 0) && (m.secondary.String()[m.secondary.Len()-1] == 'A')) {
if alt != "" {
m.secondary.WriteString(alt)
}
}
}
/**
* Adds an encoding character to the encoded key value string - Exact/Approx version
*
* @param mainExact primary encoding character to be added to encoded key if string
* m.encodeExact is set
*
* @param altExact alternative encoding character to be added to encoded alternative
* key if m.encodeExact is set string
*
* @param main primary encoding character to be added to encoded key string
*
* @param alt alternative encoding character to be added to encoded alternative key string
*
*/
func (m *M3) metaphAddExactApprox4(mainExact string, altExact string, main string, alt string) {
if m.encodeExact {
m.metaphAdd(mainExact, altExact)
} else {
m.metaphAdd(main, alt)
}
}
/**
* Adds an encoding character to the encoded key value string - Exact/Approx version
*
* @param mainExact primary encoding character to be added to encoded key if string
* m.encodeExact is set
*
* @param main primary encoding character to be added to encoded key string
*
*/
func (m *M3) metaphAddExactApprox(mainExact string, main string) {
if m.encodeExact {
m.metaphAdd(mainExact, mainExact)
} else {
m.metaphAdd(main, main)
}
}
/** Sets flag that causes Metaphone3 to 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.
*
* @param inEncodeVowels Non-initial vowels encoded if true, not if false.
*/
func (m *M3) SetEncodeVowels(inEncodeVowels bool) { m.encodeVowels = inEncodeVowels }
/** Sets flag that causes Metaphone3 to 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'.
*
* @param inEncodeExact consonants to be encoded "exactly" if true, not if false.
*/
func (m *M3) SetEncodeExact(inEncodeExact bool) { m.encodeExact = inEncodeExact }
/**
* Test for close front vowels
*
* @return true if close front vowel
*/
func (m *M3) front_Vowel(at int) bool {
return ((m.charAt(at) == 'E') || (m.charAt(at) == 'I') || (m.charAt(at) == 'Y'))
}
/**
* Detect names or words that begin with spellings
* typical of german or slavic words, for the purpose
* of choosing alternate pronunciations correctly
*
*/
func (m *M3) slavoGermanic() bool {
return m.stringAt(0, 3, "SCH", "") || m.stringAt(0, 2, "SW", "") || (m.charAt(0) == 'J') || (m.charAt(0) == 'W')
}
/**
* Tests if character is a vowel
*
* @param inChar character to be tested in to be encoded string
* @return true if character is a vowel, false if not
*
*/
func isVowel(inChar rune) bool {
return (inChar == 'A') || (inChar == 'E') || (inChar == 'I') || (inChar == 'O') || (inChar == 'U') || (inChar == 'Y') || (inChar == 'À') || (inChar == 'Á') || (inChar == 'Â') || (inChar == 'Ã') || (inChar == 'Ä') || (inChar == 'Å') || (inChar == 'Æ') || (inChar == 'È') || (inChar == 'É') || (inChar == 'Ê') || (inChar == 'Ë') || (inChar == 'Ì') || (inChar == 'Í') || (inChar == 'Î') || (inChar == 'Ï') || (inChar == 'Ò') || (inChar == 'Ó') || (inChar == 'Ô') || (inChar == 'Õ') || (inChar == 'Ö') || (inChar == '') || (inChar == 'Ø') || (inChar == 'Ù') || (inChar == 'Ú') || (inChar == 'Û') || (inChar == 'Ü') || (inChar == 'Ý') || (inChar == '')
}
/**
* skips over vowels in a string. Has exceptions for skipping consonants that
* will not be encoded.
*
* @param at position, in to be encoded string, of character to start skipping from
*
* @return position of next consonant in to be encoded string
*/
func (m *M3) skipVowels(at int) int {
if at < 0 {
return 0
}
if at >= m.length {
return m.length
}
it := m.charAt(at)
for isVowel(it) || (it == 'W') {
if m.stringAt(at, 4, "WICZ", "WITZ", "WIAK", "") || m.stringAt((at-1), 5, "EWSKI", "EWSKY", "OWSKI", "OWSKY", "") || (m.stringAt(at, 5, "WICKI", "WACKI", "") && ((at + 4) == m.last)) {
break
}
at++
if ((m.charAt(at-1) == 'W') && (m.charAt(at) == 'H')) && !(m.stringAt(at, 3, "HOP", "") || m.stringAt(at, 4, "HIDE", "HARD", "HEAD", "HAWK", "HERD", "HOOK", "HAND", "HOLE", "") || m.stringAt(at, 5, "HEART", "HOUSE", "HOUND", "") || m.stringAt(at, 6, "HAMMER", "")) {
at++
}
if at > (m.length - 1) {
break
}
it = m.charAt(at)
}
return at
}
/**
* Advanced counter m.current so that it indexes the next character to be encoded
*
* @param ifNotEncodeVowels number of characters to advance if not encoding internal vowels
* @param ifEncodeVowels number of characters to advance if encoding internal vowels
*
*/
func (m *M3) advanceCounter(ifNotEncodeVowels, ifEncodeVowels int) {
if !m.encodeVowels {
m.current += ifNotEncodeVowels
} else {
m.current += ifEncodeVowels
}
}
/**
* Subscript safe charAt()
*
* @param at index of character to access
* @return null if index out of bounds, .charAt() otherwise
*/
func (m *M3) charAt(at int) rune {
// check subbounds string
if (at < 0) || (at > (m.length - 1)) {
return rune(0)
}
for i, r := range m.inWord {
if i == at {
return r
}
}
return rune(0)
}
/**
* Tests whether the word is the root or a regular english inflection
* of it, e.g. "ache", "achy", "aches", "ached", "aching", "achingly"
* This is for cases where we want to match only the root and corresponding
* inflected forms, and not completely different words which may have the
* same subin them string.
*/
func rootOrInflections(inWord string, root string) bool {
var test string
test = root + "S"
if (inWord == root) || (inWord == test) {
return true
}
if root[len(root)-1] != 'E' {
test = root + "ES"
}
if inWord == test {
return true
}
if root[len(root)-1] != 'E' {
test = root + "ED"
} else {
test = root + "D"
}
if inWord == test {
return true
}
if root[len(root)-1] == 'E' {
root = root[:len(root)-1]
}
test = root + "ING"
if inWord == test {
return true
}
test = root + "INGLY"
if inWord == test {
return true
}
test = root + "Y"
if inWord == test {
return true
}
return false
}
/**
* Determines if one of the substrings sent in is the same as
* what is at the specified position in the being encoded string.
*
* @param start
* @param length
* @param compareStrings
* @return
*/
func (m *M3) stringAt(start, length int, compareStrings ...string) bool {
// check subbounds string
if (start < 0) || (start > (m.length - 1)) || ((start + length - 1) > (m.length - 1)) {
return false
}
b := strings.Builder{}
for i, r := range m.inWord {
if i >= start {
b.WriteRune(r)
}
if i == start+length {
break
}
}
target := b.String()
for _, strFragment := range compareStrings {
if target == strFragment {
return true
}
}
return false
}
/**
* Encodes input to one or two key values string according to Metaphone 3 rules.
*
*/
func (m *M3) Encode(in string) (primary, secondary string) {
m.flag_AL_inversion = false
m.current = 0
m.inWord = strings.ToUpper(in)
m.primary.Reset()
m.secondary.Reset()
m.length = 0
for _ = range in {
m.length++
}
if m.length < 1 {
return
}
//zero based index
m.last = m.length - 1
///////////main loop//////////////////////////
for !(m.primary.Len() > m.metaphLength) && !(m.secondary.Len() > m.metaphLength) {
if m.current >= m.length {
break
}
switch m.charAt(m.current) {
case 'B':
m.encode_B()
break
case 'ß':
case 'Ç':
m.metaphAdd("S", "S")
m.current++
break
case 'C':
m.encode_C()
break
case 'D':
m.encode_D()
break
case 'F':
m.encode_F()
break
case 'G':
m.encode_G()
break
case 'H':
m.encode_H()
break
case 'J':
m.encode_J()
break
case 'K':
m.encode_K()
break
case 'L':
m.encode_L()
break
case 'M':
m.encode_M()
break
case 'N':
m.encode_N()
break
case 'Ñ':
m.metaphAdd("N", "N")
m.current++
break
case 'P':
m.encode_P()
break
case 'Q':
m.encode_Q()
break
case 'R':
m.encode_R()
break
case 'S':
m.encode_S()
break
case 'T':
m.encode_T()
break
case 'Ð': // eth
case 'Þ': // thorn
m.metaphAdd("0", "0")
m.current++
break
case 'V':
m.encode_V()
break
case 'W':
m.encode_W()
break
case 'X':
m.encode_X()
break
case '':
m.metaphAdd("X", "X")
m.current++
break
case '':
m.metaphAdd("S", "S")
m.current++
break
case 'Z':
m.encode_Z()
break
default:
if isVowel(m.charAt(m.current)) {
m.encode_Vowels()
break
}
m.current++
}
}
primary, secondary = m.primary.String(), m.secondary.String()
//only give back m.metaphLength number of chars in m.metaph
if len(primary) > m.metaphLength {
primary = primary[:m.metaphLength]
}
if len(secondary) > m.metaphLength {
secondary = secondary[:m.metaphLength]
}
// it is possible for the two metaphs to be the same
// after truncation. lose the second one if so
if primary == secondary {
secondary = ""
}
return primary, secondary
}
/**
* Encodes all initial vowels to A.
*
* Encodes non-initial vowels to A if m.encodeVowels is true
*
*
*/
func (m *M3) encode_Vowels() {
if m.current == 0 {
// all init vowels map to 'A'
// as of Double Metaphone
m.metaphAdd("A", "A")
} else if m.encodeVowels {
if m.charAt(m.current) != 'E' {
if m.skip_Silent_UE() {
return
}
if m.o_Silent() {
m.current++
return
}
// encode all vowels and
// diphthongs to the same value
m.metaphAdd("A", "A")
} else {
m.encode_E_Pronounced()
}
}
if !(!isVowel(m.charAt(m.current-2)) && m.stringAt((m.current-1), 4, "LEWA", "LEWO", "LEWI", "")) {
m.current = m.skipVowels(m.current)
} else {
m.current++
}
}
/**
* Encodes cases where non-initial 'e' is pronounced, taking
* care to detect unusual cases from the greek.
*
* Only executed if non initial vowel encoding is turned on
*
*
*/
func (m *M3) encode_E_Pronounced() {
// special cases with two pronunciations
// 'agape' 'lame' 'resume'
if (m.stringAt(0, 4, "LAME", "SAKE", "PATE", "") && (m.length == 4)) || (m.stringAt(0, 5, "AGAPE", "") && (m.length == 5)) || ((m.current == 5) && m.stringAt(0, 6, "RESUME", "")) {
m.metaphAdd("", "A")
return
}
// special case "inge" => 'INGA', 'INJ'
if m.stringAt(0, 4, "INGE", "") && (m.length == 4) {
m.metaphAdd("A", "")
return
}
// special cases with two pronunciations
// special handling due to the difference in
// the pronunciation of the '-D'
if (m.current == 5) && m.stringAt(0, 7, "BLESSED", "LEARNED", "") {
m.metaphAddExactApprox4("D", "AD", "T", "AT")
m.current += 2
return
}
// encode all vowels and diphthongs to the same value
if (!m.e_Silent() && !m.flag_AL_inversion && !m.silent_Internal_E()) || m.e_Pronounced_Exceptions() {
m.metaphAdd("A", "A")
}
// now that we've visited the vowel in question
m.flag_AL_inversion = false
}
/**
* Tests for cases where non-initial 'o' is not pronounced
* Only executed if non initial vowel encoding is turned on
*
* @return true if encoded as silent - no addition to m.metaph key
*
*/
func (m *M3) o_Silent() bool {
// if "iron" at beginning or end of word and not "irony"
if (m.charAt(m.current) == 'O') && m.stringAt((m.current-2), 4, "IRON", "") {
if (m.stringAt(0, 4, "IRON", "") || (m.stringAt((m.current-2), 4, "IRON", "") && (m.last == (m.current + 1)))) && !m.stringAt((m.current-2), 6, "IRONIC", "") {
return true
}
}
return false
}
/**
* Tests and encodes cases where non-initial 'e' is never pronounced
* Only executed if non initial vowel encoding is turned on
*
* @return true if encoded as silent - no addition to m.metaph key
*
*/
func (m *M3) e_Silent() bool {
if m.e_Pronounced_At_End() {
return false
}
// 'e' silent when last letter, altho
// also silent if before plural 's'
// or past tense or participle 'd', e.g.
// 'grapes' and 'banished' => PNXT
// and not e.g. "nested", "rises", or "pieces" => RASAS
// e.g. 'wholeness', 'boneless', 'barely'
if (m.current == m.last) || (m.stringAt(m.last, 1, "S", "D", "") && (m.current > 1) && ((m.current + 1) == m.last) && !(m.stringAt((m.current-1), 3, "TED", "SES", "CES", "") || m.stringAt(0, 9, "ANTIPODES", "ANOPHELES", "") || m.stringAt(0, 8, "MOHAMMED", "MUHAMMED", "MOUHAMED", "") || m.stringAt(0, 7, "MOHAMED", "") || m.stringAt(0, 6, "NORRED", "MEDVED", "MERCED", "ALLRED", "KHALED", "RASHED", "MASJED", "") || m.stringAt(0, 5, "JARED", "AHMED", "HAMED", "JAVED", "") || m.stringAt(0, 4, "ABED", "IMED", ""))) || (m.stringAt((m.current+1), 4, "NESS", "LESS", "") && ((m.current + 4) == m.last)) || (m.stringAt((m.current+1), 2, "LY", "") && ((m.current + 2) == m.last) && !m.stringAt(0, 6, "CICELY", "")) {
return true
}
return false
}
/**
* Tests for words where an 'E' at the end of the word
* is pronounced
*
* special cases, mostly from the greek, spanish, japanese,
* italian, and french words normally having an acute accent.
* also, pronouns and articles
*
* Many Thanks to ali, QuentinCompson, JeffCO, ToonScribe, Xan,
* Trafalz, and VictorLaszlo, all of them atriots from the Eschaton,
* for all their fine contributions!
*
* @return true if 'E' at end is pronounced
*
*/
func (m *M3) e_Pronounced_At_End() bool {
if (m.current == m.last) && (m.stringAt((m.current-6), 7, "STROPHE", "") ||
// if a vowel is before the 'E', vowel eater will have eaten it.
//otherwise, consonant + 'E' will need 'E' pronounced
(m.length == 2) || ((m.length == 3) && !isVowel(m.charAt(0))) ||
// these german name endings can be relied on to have the 'e' pronounced
(m.stringAt((m.last-2), 3, "BKE", "DKE", "FKE", "KKE", "LKE",
"NKE", "MKE", "PKE", "TKE", "VKE", "ZKE", "") && !m.stringAt(0, 5, "FINKE", "FUNKE", "") && !m.stringAt(0, 6, "FRANKE", "")) || m.stringAt((m.last-4), 5, "SCHKE", "") || (m.stringAt(0, 4, "ACME", "NIKE", "CAFE", "RENE", "LUPE", "JOSE", "ESME", "") && (m.length == 4)) || (m.stringAt(0, 5, "LETHE", "CADRE", "TILDE", "SIGNE", "POSSE", "LATTE", "ANIME", "DOLCE", "CROCE",
"ADOBE", "OUTRE", "JESSE", "JAIME", "JAFFE", "BENGE", "RUNGE",
"CHILE", "DESME", "CONDE", "URIBE", "LIBRE", "ANDRE", "") && (m.length == 5)) || (m.stringAt(0, 6, "HECATE", "PSYCHE", "DAPHNE", "PENSKE", "CLICHE", "RECIPE",
"TAMALE", "SESAME", "SIMILE", "FINALE", "KARATE", "RENATE", "SHANTE",
"OBERLE", "COYOTE", "KRESGE", "STONGE", "STANGE", "SWAYZE", "FUENTE",
"SALOME", "URRIBE", "") && (m.length == 6)) || (m.stringAt(0, 7, "ECHIDNE", "ARIADNE", "MEINEKE", "PORSCHE", "ANEMONE", "EPITOME",
"SYNCOPE", "SOUFFLE", "ATTACHE", "MACHETE", "KARAOKE", "BUKKAKE",
"VICENTE", "ELLERBE", "VERSACE", "") && (m.length == 7)) || (m.stringAt(0, 8, "PENELOPE", "CALLIOPE", "CHIPOTLE", "ANTIGONE", "KAMIKAZE", "EURIDICE",
"YOSEMITE", "FERRANTE", "") && (m.length == 8)) || (m.stringAt(0, 9, "HYPERBOLE", "GUACAMOLE", "XANTHIPPE", "") && (m.length == 9)) || (m.stringAt(0, 10, "SYNECDOCHE", "") && (m.length == 10))) {
return true
}
return false
}
/**
* Detect internal silent 'E's e.g. "roseman",
* "firestone"
*
*/
func (m *M3) silent_Internal_E() bool {
// 'olesen' but not 'olen' RAKE BLAKE
if (m.stringAt(0, 3, "OLE", "") && m.e_Silent_Suffix(3) && !m.e_Pronouncing_Suffix(3)) || (m.stringAt(0, 4, "BARE", "FIRE", "FORE", "GATE", "HAGE", "HAVE",
"HAZE", "HOLE", "CAPE", "HUSE", "LACE", "LINE",
"LIVE", "LOVE", "MORE", "MOSE", "MORE", "NICE",
"RAKE", "ROBE", "ROSE", "SISE", "SIZE", "WARE",
"WAKE", "WISE", "WINE", "") && m.e_Silent_Suffix(4) && !m.e_Pronouncing_Suffix(4)) || (m.stringAt(0, 5, "BLAKE", "BRAKE", "BRINE", "CARLE", "CLEVE", "DUNNE",
"HEDGE", "HOUSE", "JEFFE", "LUNCE", "STOKE", "STONE",
"THORE", "WEDGE", "WHITE", "") && m.e_Silent_Suffix(5) && !m.e_Pronouncing_Suffix(5)) || (m.stringAt(0, 6, "BRIDGE", "CHEESE", "") && m.e_Silent_Suffix(6) && !m.e_Pronouncing_Suffix(6)) || m.stringAt((m.current-5), 7, "CHARLES", "") {
return true
}
return false
}
/**
* Detect conditions required
* for the 'E' not to be pronounced
*
*/
func (m *M3) e_Silent_Suffix(at int) bool {
if (m.current == (at - 1)) && (m.length > (at + 1)) && (isVowel(m.charAt(at+1)) || (m.stringAt(at, 2, "ST", "SL", "") && (m.length > (at + 2)))) {
return true
}
return false
}
/**
* Detect endings that will
* cause the 'e' to be pronounced
*
*/
func (m *M3) e_Pronouncing_Suffix(at int) bool {
// e.g. 'bridgewood' - the other vowels will get eaten
// up so we need to put one in here
if (m.length == (at + 4)) && m.stringAt(at, 4, "WOOD", "") {
return true
}
// same as above
if (m.length == (at + 5)) && m.stringAt(at, 5, "WATER", "WORTH", "") {
return true
}
// e.g. 'bridgette'
if (m.length == (at + 3)) && m.stringAt(at, 3, "TTE", "LIA", "NOW", "ROS", "RAS", "") {
return true
}
// e.g. 'olena'
if (m.length == (at + 2)) && m.stringAt(at, 2, "TA", "TT", "NA", "NO", "NE",
"RS", "RE", "LA", "AU", "RO", "RA", "") {
return true
}
// e.g. 'bridget'
if (m.length == (at + 1)) && m.stringAt(at, 1, "T", "R", "") {
return true
}
return false
}
/**
* Exceptions where 'E' is pronounced where it
* usually wouldn't be, and also some cases
* where 'LE' transposition rules don't apply
* and the vowel needs to be encoded here
*
* @return true if 'E' pronounced
*
*/
func (m *M3) e_Pronounced_Exceptions() bool {
// greek names e.g. "herakles" or hispanic names e.g. "robles", where 'e' is pronounced, other exceptions
if (((m.current + 1) == m.last) && (m.stringAt((m.current-3), 5, "OCLES", "ACLES", "AKLES", "") || m.stringAt(0, 4, "INES", "") || m.stringAt(0, 5, "LOPES", "ESTES", "GOMES", "NUNES", "ALVES", "ICKES",
"INNES", "PERES", "WAGES", "NEVES", "BENES", "DONES", "") || m.stringAt(0, 6, "CORTES", "CHAVES", "VALDES", "ROBLES", "TORRES", "FLORES", "BORGES",
"NIEVES", "MONTES", "SOARES", "VALLES", "GEDDES", "ANDRES", "VIAJES",
"CALLES", "FONTES", "HERMES", "ACEVES", "BATRES", "MATHES", "") || m.stringAt(0, 7, "DELORES", "MORALES", "DOLORES", "ANGELES", "ROSALES", "MIRELES", "LINARES",
"PERALES", "PAREDES", "BRIONES", "SANCHES", "CAZARES", "REVELES", "ESTEVES",
"ALVARES", "MATTHES", "SOLARES", "CASARES", "CACERES", "STURGES", "RAMIRES",
"FUNCHES", "BENITES", "FUENTES", "PUENTES", "TABARES", "HENTGES", "VALORES", "") || m.stringAt(0, 8, "GONZALES", "MERCEDES", "FAGUNDES", "JOHANNES", "GONSALES", "BERMUDES",
"CESPEDES", "BETANCES", "TERRONES", "DIOGENES", "CORRALES", "CABRALES",
"MARTINES", "GRAJALES", "") || m.stringAt(0, 9, "CERVANTES", "FERNANDES", "GONCALVES", "BENEVIDES", "CIFUENTES", "SIFUENTES",
"SERVANTES", "HERNANDES", "BENAVIDES", "") || m.stringAt(0, 10, "ARCHIMEDES", "CARRIZALES", "MAGALLANES", ""))) || m.stringAt(m.current-2, 4, "FRED", "DGES", "DRED", "GNES", "") || m.stringAt((m.current-5), 7, "PROBLEM", "RESPLEN", "") || m.stringAt((m.current-4), 6, "REPLEN", "") || m.stringAt((m.current-3), 4, "SPLE", "") {
return true
}
return false
}
/**
* Encodes "-UE".
*
* @return true if encoding handled in this routine, false if not
*/
func (m *M3) skip_Silent_UE() bool {
// always silent except for cases listed below
if (m.stringAt((m.current-1), 3, "QUE", "GUE", "") && !m.stringAt(0, 8, "BARBEQUE", "PALENQUE", "APPLIQUE", "") &&
// '-que' cases usually french but missing the acute accent
!m.stringAt(0, 6, "RISQUE", "") && !m.stringAt((m.current-3), 5, "ARGUE", "SEGUE", "") && !m.stringAt(0, 7, "PIROGUE", "ENRIQUE", "") && !m.stringAt(0, 10, "COMMUNIQUE", "")) && (m.current > 1) && (((m.current + 1) == m.last) || m.stringAt(0, 7, "JACQUES", "")) {
m.current = m.skipVowels(m.current)
return true
}
return false
}