-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundex.gleam
53 lines (48 loc) · 1.02 KB
/
soundex.gleam
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
import gleam/list
import gleam/string
import phonetic_gleam/utils
fn tr_char(b) {
case b {
"B" | "F" | "P" | "V" -> "1"
"C" | "G" | "J" | "K" | "Q" | "S" | "X" | "Z" -> "2"
"D" | "T" -> "3"
"L" -> "4"
"M" | "N" -> "5"
"R" -> "6"
_ -> ""
}
}
fn tr(chars, acc) {
case chars {
[] -> list.reverse(acc)
[a, b, ..xs] if acc == [] -> {
// first character code equal with second?
case tr_char(a) == tr_char(b) {
True -> tr(xs, [a, ..acc])
False -> tr([b, ..xs], [a, ..acc])
}
}
[a, ..xs] if acc == [] -> tr(xs, [a, ..acc])
[a, ..xs] -> tr(xs, [tr_char(a), ..acc])
}
}
fn cleanup(codes) {
codes
|> utils.remove_adjacent_dups
|> utils.remove_value("")
|> list.take(4)
|> string.join("")
|> string.pad_right(to: 4, with: "0")
}
fn prepare_word(word) {
word
|> string.uppercase
|> utils.remove_not_allowed_chars("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|> string.to_graphemes
}
pub fn encode(word) {
word
|> prepare_word
|> tr([])
|> cleanup
}