File: soundex.go

package info (click to toggle)
golang-github-xrash-smetrics 0.0~git20170218.a3153f7-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,076 kB
  • sloc: makefile: 9
file content (40 lines) | stat: -rw-r--r-- 572 bytes parent folder | download
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
package smetrics

import (
	"strings"
)

func Soundex(s string) string {
	m := map[byte]string{
		'B': "1", 'P': "1", 'F': "1", 'V': "1",
		'C': "2", 'S': "2", 'K': "2", 'G': "2", 'J': "2", 'Q': "2", 'X': "2", 'Z': "2",
		'D': "3", 'T': "3",
		'L': "4",
		'M': "5", 'N': "5",
		'R': "6",
	}

	s = strings.ToUpper(s)

	r := string(s[0])
	p := s[0]
	for i := 1; i < len(s) && len(r) < 4; i++ {
		c := s[i]

		if (c < 'A' || c > 'Z') || (c == p) {
			continue
		}

		p = c

		if n, ok := m[c]; ok {
			r += n
		}
	}

	for i := len(r); i < 4; i++ {
		r += "0"
	}

	return r
}