-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
198 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,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, ..xs] if acc == [] -> tr(xs, [a, ..acc]) | ||
[a, ..xs] -> | ||
case a { | ||
"B" | "F" | "P" | "V" -> tr(xs, ["1", ..acc]) | ||
"C" | "G" | "J" | "K" | "Q" | "S" | "X" | "Z" -> tr(xs, ["2", ..acc]) | ||
"D" | "T" -> tr(xs, ["3", ..acc]) | ||
"L" -> tr(xs, ["4", ..acc]) | ||
"M" | "N" -> tr(xs, ["5", ..acc]) | ||
"R" -> tr(xs, ["6", ..acc]) | ||
_ -> tr(xs, 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([]) | ||
|> string.join("") | ||
|> cleanup | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import gleam/list | ||
import gleam/string | ||
import gleam/set | ||
|
||
pub fn remove_value(xs: List(a), value: a) { | ||
list.filter(xs, fn(x) { x != value }) | ||
} | ||
|
||
pub fn remove_not_allowed_chars(word: String, allowed_chars: String) { | ||
let allowed_chars_set = | ||
allowed_chars | ||
|> string.to_graphemes | ||
|> set.from_list | ||
word | ||
|> string.to_graphemes | ||
|> list.filter(fn(c) { set.contains(allowed_chars_set, c) }) | ||
|> string.join("") | ||
} | ||
|
||
pub fn remove_adjacent_dups(xs: List(a)) -> List(a) { | ||
list.fold_right(xs, [], fn(acc, x) { | ||
case list.first(acc) { | ||
Ok(r) if r == x -> acc | ||
_ -> [x, ..acc] | ||
} | ||
}) | ||
} | ||
|
||
pub fn then_or_else(is: Bool, then: a, or_else: a) { | ||
case is { | ||
True -> then | ||
False -> or_else | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import gleeunit/should | ||
import phonetic_gleam/utils | ||
|
||
pub fn remove_adjacents_dup() { | ||
utils.remove_adjacent_dups([]) | ||
|> should.equal([]) | ||
utils.remove_adjacent_dups([1]) | ||
|> should.equal([1]) | ||
utils.remove_adjacent_dups([1, 1]) | ||
|> should.equal([1]) | ||
utils.remove_adjacent_dups([1, 2, 3]) | ||
|> should.equal([1, 2, 3]) | ||
utils.remove_adjacent_dups([1, 2, 2, 3, 3, 4]) | ||
|> should.equal([1, 2, 3, 4]) | ||
utils.remove_adjacent_dups([1, 1, 2, 1, 1]) | ||
|> should.equal([1, 2, 1]) | ||
} |