Current section
Files
Jump to
Current section
Files
src/hanguleam@extractor.erl
-module(hanguleam@extractor).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/hanguleam/extractor.gleam").
-export([get_choseong/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/hanguleam/extractor.gleam", 68).
-spec get_choseong_index(integer()) -> integer().
get_choseong_index(Codepoint) ->
Base = Codepoint - 16#AC00,
case (21 * 28) of
0 -> 0;
Gleam@denominator -> Base div Gleam@denominator
end.
-file("src/hanguleam/extractor.gleam", 73).
-spec get_choseong_by_index(integer()) -> gleam@option:option(binary()).
get_choseong_by_index(Index) ->
_pipe = hanguleam@internal@utils:get_value_by_index(
Index,
[<<"ㄱ"/utf8>>,
<<"ㄲ"/utf8>>,
<<"ㄴ"/utf8>>,
<<"ㄷ"/utf8>>,
<<"ㄸ"/utf8>>,
<<"ㄹ"/utf8>>,
<<"ㅁ"/utf8>>,
<<"ㅂ"/utf8>>,
<<"ㅃ"/utf8>>,
<<"ㅅ"/utf8>>,
<<"ㅆ"/utf8>>,
<<"ㅇ"/utf8>>,
<<"ㅈ"/utf8>>,
<<"ㅉ"/utf8>>,
<<"ㅊ"/utf8>>,
<<"ㅋ"/utf8>>,
<<"ㅌ"/utf8>>,
<<"ㅍ"/utf8>>,
<<"ㅎ"/utf8>>]
),
gleam@option:from_result(_pipe).
-file("src/hanguleam/extractor.gleam", 56).
-spec extract_from_complete_hangul(binary()) -> gleam@option:option(binary()).
extract_from_complete_hangul(Char) ->
case hanguleam@internal@unicode:get_codepoint_result_from_char(Char) of
{ok, Codepoint} ->
case hanguleam@internal@unicode:is_complete_hangul(Codepoint) of
true ->
_pipe = get_choseong_index(Codepoint),
get_choseong_by_index(_pipe);
false ->
none
end;
{error, _} ->
none
end.
-file("src/hanguleam/extractor.gleam", 49).
-spec extract_choseong_from_char(binary()) -> gleam@option:option(binary()).
extract_choseong_from_char(Char) ->
case gleam@list:contains(
[<<"ㄱ"/utf8>>,
<<"ㄲ"/utf8>>,
<<"ㄴ"/utf8>>,
<<"ㄷ"/utf8>>,
<<"ㄸ"/utf8>>,
<<"ㄹ"/utf8>>,
<<"ㅁ"/utf8>>,
<<"ㅂ"/utf8>>,
<<"ㅃ"/utf8>>,
<<"ㅅ"/utf8>>,
<<"ㅆ"/utf8>>,
<<"ㅇ"/utf8>>,
<<"ㅈ"/utf8>>,
<<"ㅉ"/utf8>>,
<<"ㅊ"/utf8>>,
<<"ㅋ"/utf8>>,
<<"ㅌ"/utf8>>,
<<"ㅍ"/utf8>>,
<<"ㅎ"/utf8>>],
Char
) of
true ->
{some, Char};
false ->
extract_from_complete_hangul(Char)
end.
-file("src/hanguleam/extractor.gleam", 30).
-spec do_get_choseong(binary(), binary()) -> binary().
do_get_choseong(Word, Accumulator) ->
case gleam_stdlib:string_pop_grapheme(Word) of
{ok, {Head, Tail}} ->
Extracted = case Head of
<<" "/utf8>> ->
<<" "/utf8>>;
<<"\t"/utf8>> ->
<<"\t"/utf8>>;
<<"\n"/utf8>> ->
<<"\n"/utf8>>;
_ ->
case extract_choseong_from_char(Head) of
{some, Choseong} ->
Choseong;
none ->
<<""/utf8>>
end
end,
do_get_choseong(Tail, <<Accumulator/binary, Extracted/binary>>);
{error, _} ->
Accumulator
end.
-file("src/hanguleam/extractor.gleam", 26).
?DOC(
" Extracts the initial consonants (choseong) from Korean Hangul characters in a string.\n"
" Non-Korean characters are filtered out, while whitespace characters (spaces, tabs, newlines)\n"
" are preserved in their original positions.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" get_choseong(\"사과\")\n"
" // -> \"ㅅㄱ\"\n"
"\n"
" get_choseong(\"띄어 쓰기\")\n"
" // -> \"ㄸㅇ ㅆㄱ\"\n"
"\n"
" get_choseong(\"안녕hello\")\n"
" // -> \"ㅇㄴ\"\n"
" ```\n"
).
-spec get_choseong(binary()) -> binary().
get_choseong(Word) ->
do_get_choseong(Word, <<""/utf8>>).