...

Source file src/github.com/xrash/smetrics/soundex.go

Documentation: github.com/xrash/smetrics

     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  	b := strings.Builder{}
    10  	b.Grow(4)
    11  
    12  	p := s[0]
    13  	if p <= 'z' && p >= 'a' {
    14  		p -= 32 // convert to uppercase
    15  	}
    16  	b.WriteByte(p)
    17  
    18  	n := 0
    19  	for i := 1; i < len(s); i++ {
    20  		c := s[i]
    21  
    22  		if c <= 'z' && c >= 'a' {
    23  			c -= 32 // convert to uppercase
    24  		} else if c < 'A' || c > 'Z' {
    25  			continue
    26  		}
    27  
    28  		if c == p {
    29  			continue
    30  		}
    31  
    32  		p = c
    33  
    34  		switch c {
    35  		case 'B', 'P', 'F', 'V':
    36  			c = '1'
    37  		case 'C', 'S', 'K', 'G', 'J', 'Q', 'X', 'Z':
    38  			c = '2'
    39  		case 'D', 'T':
    40  			c = '3'
    41  		case 'L':
    42  			c = '4'
    43  		case 'M', 'N':
    44  			c = '5'
    45  		case 'R':
    46  			c = '6'
    47  		default:
    48  			continue
    49  		}
    50  
    51  		b.WriteByte(c)
    52  		n++
    53  		if n == 3 {
    54  			break
    55  		}
    56  	}
    57  
    58  	for i := n; i < 3; i++ {
    59  		b.WriteByte('0')
    60  	}
    61  
    62  	return b.String()
    63  }
    64  

View as plain text