snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
This commit is contained in:
104
server/pkg/str/soundex.go
Normal file
104
server/pkg/str/soundex.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package str
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ToSoundex takes a word and returns the soundex code for it.
|
||||
// https://en.wikipedia.org/wiki/Soundex
|
||||
//
|
||||
// 1. Retain the first letter of the name and drop all other occurrences of a, e, i, o, u, y, h, w.
|
||||
// 2. Replace consonants with digits as follows (after the first letter):
|
||||
// b, f, p, v → 1
|
||||
// c, g, j, k, q, s, x, z → 2
|
||||
// d, t → 3
|
||||
// l → 4
|
||||
// m, n → 5
|
||||
// r → 6
|
||||
// 3. If two or more letters with the same number are adjacent in the original name (before step 1),
|
||||
// only retain the first letter; also two letters with the same number separated
|
||||
// by 'h' or 'w' are coded as a single number, whereas such letters separated by a vowel are coded twice.
|
||||
// This rule also applies to the first letter.
|
||||
// 4. Iterate the previous step until you have one letter and three numbers.
|
||||
// If you have too few letters in your word that you can't assign three numbers, append with zeros
|
||||
// until there are three numbers. If you have more than 3 letters, just retain the first 3 numbers.
|
||||
func ToSoundex(s string) string {
|
||||
var (
|
||||
// soundex code
|
||||
code string
|
||||
// last code
|
||||
lastCode string
|
||||
// last rune
|
||||
lastRune rune
|
||||
// last rune is vowel
|
||||
lastRuneIsVowel bool
|
||||
)
|
||||
|
||||
// retain the first letter of the name and drop all other occurrences of a, e, i, o, u, y, h, w
|
||||
for _, r := range s {
|
||||
if r == 'a' || r == 'e' || r == 'i' || r == 'o' || r == 'u' || r == 'y' || r == 'h' || r == 'w' {
|
||||
continue
|
||||
}
|
||||
|
||||
code = string(r)
|
||||
break
|
||||
}
|
||||
|
||||
// replace consonants with digits as follows (after the first letter)
|
||||
for _, r := range s {
|
||||
if r == 'a' || r == 'e' || r == 'i' || r == 'o' || r == 'u' || r == 'y' || r == 'h' || r == 'w' {
|
||||
lastRuneIsVowel = true
|
||||
continue
|
||||
}
|
||||
|
||||
if lastRuneIsVowel {
|
||||
lastRuneIsVowel = false
|
||||
lastCode = ""
|
||||
}
|
||||
|
||||
switch r {
|
||||
case 'b', 'f', 'p', 'v':
|
||||
lastCode = "1"
|
||||
case 'c', 'g', 'j', 'k', 'q', 's', 'x', 'z':
|
||||
lastCode = "2"
|
||||
case 'd', 't':
|
||||
lastCode = "3"
|
||||
case 'l':
|
||||
lastCode = "4"
|
||||
case 'm', 'n':
|
||||
lastCode = "5"
|
||||
case 'r':
|
||||
lastCode = "6"
|
||||
}
|
||||
|
||||
if lastCode != "" && lastCode != string(lastRune) {
|
||||
code += lastCode
|
||||
}
|
||||
|
||||
lastRune = r
|
||||
}
|
||||
|
||||
// if two or more letters with the same number are adjacent in the original name (before step 1),
|
||||
// only retain the first letter
|
||||
// also two letters with the same number separated by 'h' or 'w' are coded as a single number,
|
||||
// whereas such letters separated by a vowel are coded twice
|
||||
// this rule also applies to the first letter
|
||||
code = strings.ReplaceAll(code, "11", "1")
|
||||
code = strings.ReplaceAll(code, "22", "2")
|
||||
code = strings.ReplaceAll(code, "33", "3")
|
||||
code = strings.ReplaceAll(code, "44", "4")
|
||||
code = strings.ReplaceAll(code, "55", "5")
|
||||
code = strings.ReplaceAll(code, "66", "6")
|
||||
|
||||
// iterate the previous step until you have one letter and three numbers
|
||||
// if you have too few letters in your word that you can't assign three numbers,
|
||||
// append with zeros until there are three numbers
|
||||
// if you have more than 3 letters, just retain the first 3 numbers
|
||||
if len(code) < 4 {
|
||||
code += strings.Repeat("0", 4-len(code))
|
||||
} else {
|
||||
code = code[:4]
|
||||
}
|
||||
|
||||
return code
|
||||
}
|
||||
Reference in New Issue
Block a user