Debian Codegraph @

soundex.go

raw symbols SCIP
1 package smetrics
2
3 import (
4 "strings"
5 )
6
7 // The Soundex encoding. It is a phonetic algorithm that considers how the words sound in English. Soundex maps a string to a 4-byte code consisting of the first letter of the original string and three numbers. Strings that sound similar should map to the same code.
8 func Soundex(s string) string {
9 m := map[byte]string{
10 'B': "1", 'P': "1", 'F': "1", 'V': "1",
11 'C': "2", 'S': "2", 'K': "2", 'G': "2", 'J': "2", 'Q': "2", 'X': "2", 'Z': "2",
12 'D': "3", 'T': "3",
13 'L': "4",
14 'M': "5", 'N': "5",
15 'R': "6",
16 }
17
18 s = strings.ToUpper(s)
19
20 r := string(s[0])
21 p := s[0]
22 for i := 1; i < len(s) && len(r) < 4; i++ {
23 c := s[i]
24
25 if (c < 'A' || c > 'Z') || (c == p) {
26 continue
27 }
28
29 p = c
30
31 if n, ok := m[c]; ok {
32 r += n
33 }
34 }
35
36 for i := len(r); i < 4; i++ {
37 r += "0"
38 }
39
40 return r
41 }