Packages

A Gleam library for Korean text processing, providing comprehensive tools for handling Hangul characters

Current section

Files

Jump to
hanguleam src hanguleam@parser.erl
Raw

src/hanguleam@parser.erl

-module(hanguleam@parser).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/hanguleam/parser.gleam").
-export([disassemble_complete_character/1, disassemble/1, disassemble_to_groups/1]).
-export_type([disassemble_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type disassemble_error() :: incomplete_hangul |
non_hangul_character |
empty_string.
-file("src/hanguleam/parser.gleam", 96).
-spec syllable_to_components(hanguleam@types:hangul_syllable()) -> list(binary()).
syllable_to_components(Syllable) ->
{hangul_syllable, {choseong, Cho}, {jungseong, Jung}, {jongseong, Jong}} = Syllable,
_pipe = [Cho],
_pipe@1 = lists:append(_pipe, gleam@string:to_graphemes(Jung)),
(fun(Base) -> case Jong of
<<""/utf8>> ->
Base;
_ ->
lists:append(Base, gleam@string:to_graphemes(Jong))
end end)(_pipe@1).
-file("src/hanguleam/parser.gleam", 108).
-spec disassemble_jamo(binary()) -> list(binary()).
disassemble_jamo(Char) ->
case hanguleam@internal@character:parse_hangul_jamo(Char) of
{ok, {consonant, Components}} ->
gleam@string:split(Components, <<""/utf8>>);
{ok, {vowel, Components}} ->
gleam@string:split(Components, <<""/utf8>>);
{error, _} ->
[]
end.
-file("src/hanguleam/parser.gleam", 148).
?DOC(
" Disassembles a single complete Korean Hangul character into its constituent parts.\n"
" This function only works with complete Hangul syllables (가-힣 range) and returns\n"
" detailed information about the choseong (initial), jungseong (medial), and jongseong (final) components.\n"
"\n"
" ## Return Value\n"
"\n"
" - `Ok(HangulSyllable)`: Contains the disassembled components\n"
" - `choseong`: Initial consonant (e.g., \"ㄱ\")\n"
" - `jungseong`: Medial vowel (e.g., \"ㅏ\") \n"
" - `jongseong`: Final consonant(s) (e.g., \"ㅂㅅ\" or \"\" if none)\n"
" - `Error(IncompleteHangul)`: Input is a Hangul jamo but not a complete syllable\n"
" - `Error(NonHangulCharacter)`: Input is not a Korean character\n"
" - `Error(EmptyString)`: Input string is empty\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" disassemble_complete_character(\"값\")\n"
" // -> Ok(HangulSyllable(Choseong(\"ㄱ\"), Jungseong(\"ㅏ\"), Jongseong(\"ㅂㅅ\")))\n"
"\n"
" disassemble_complete_character(\"리\")\n"
" // -> Ok(HangulSyllable(Choseong(\"ㄹ\"), Jungseong(\"ㅣ\"), Jongseong(\"\")))\n"
"\n"
" disassemble_complete_character(\"ㅏ\")\n"
" // -> Error(IncompleteHangul)\n"
"\n"
" disassemble_complete_character(\"a\")\n"
" // -> Error(NonHangulCharacter)\n"
"\n"
" disassemble_complete_character(\"\")\n"
" // -> Error(EmptyString)\n"
" ```\n"
).
-spec disassemble_complete_character(binary()) -> {ok,
hanguleam@types:hangul_syllable()} |
{error, disassemble_error()}.
disassemble_complete_character(Char) ->
case hanguleam@internal@character:get_character_type(Char) of
empty ->
{error, empty_string};
non_hangul ->
{error, non_hangul_character};
{incomplete_hangul, _} ->
{error, incomplete_hangul};
{complete_hangul, Char@1} ->
{ok, Char@1}
end.
-file("src/hanguleam/parser.gleam", 87).
-spec disassemble_char_to_groups(binary()) -> list(binary()).
disassemble_char_to_groups(Char) ->
case disassemble_complete_character(Char) of
{ok, Syllable} ->
syllable_to_components(Syllable);
{error, incomplete_hangul} ->
disassemble_jamo(Char);
{error, non_hangul_character} ->
[Char];
{error, empty_string} ->
[Char]
end.
-file("src/hanguleam/parser.gleam", 41).
-spec do_disassemble(binary(), binary()) -> binary().
do_disassemble(Text, Accumulator) ->
case gleam_stdlib:string_pop_grapheme(Text) of
{ok, {Head, Tail}} ->
Components = begin
_pipe = disassemble_char_to_groups(Head),
gleam@string:join(_pipe, <<""/utf8>>)
end,
do_disassemble(Tail, <<Accumulator/binary, Components/binary>>);
{error, _} ->
Accumulator
end.
-file("src/hanguleam/parser.gleam", 37).
?DOC(
" Disassembles Korean Hangul characters into their constituent jamo (consonants and vowels).\n"
" This function breaks down complete Hangul syllables and individual jamo into their basic components.\n"
" Non-Korean characters and whitespace are preserved as-is.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" disassemble(\"값\")\n"
" // -> \"ㄱㅏㅂㅅ\"\n"
"\n"
" disassemble(\"값이 비싸다\")\n"
" // -> \"ㄱㅏㅂㅅㅇㅣ ㅂㅣㅆㅏㄷㅏ\"\n"
"\n"
" disassemble(\"ㅘ\")\n"
" // -> \"ㅗㅏ\"\n"
"\n"
" disassemble(\"ㄵ\")\n"
" // -> \"ㄴㅈ\"\n"
"\n"
" disassemble(\"hello 안녕\")\n"
" // -> \"hello ㅇㅏㄴㄴㅕㅇ\"\n"
" ```\n"
).
-spec disassemble(binary()) -> binary().
disassemble(Text) ->
do_disassemble(Text, <<""/utf8>>).
-file("src/hanguleam/parser.gleam", 74).
-spec do_disassemble_to_groups(binary(), list(list(binary()))) -> list(list(binary())).
do_disassemble_to_groups(Text, Accumulator) ->
case gleam_stdlib:string_pop_grapheme(Text) of
{ok, {Head, Tail}} ->
Group = disassemble_char_to_groups(Head),
do_disassemble_to_groups(Tail, [Group | Accumulator]);
{error, _} ->
Accumulator
end.
-file("src/hanguleam/parser.gleam", 70).
?DOC(
" Disassembles Korean Hangul characters into groups of jamo components.\n"
" Unlike `disassemble`, this function returns a 2D array where each character\n"
" is represented as a separate array of its constituent jamo.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" disassemble_to_groups(\"사과\")\n"
" // -> [[\"ㅅ\", \"ㅏ\"], [\"ㄱ\", \"ㅗ\", \"ㅏ\"]]\n"
"\n"
" disassemble_to_groups(\"ㅘ\")\n"
" // -> [[\"ㅗ\", \"ㅏ\"]]\n"
"\n"
" disassemble_to_groups(\"ㄵ\")\n"
" // -> [[\"ㄴ\", \"ㅈ\"]]\n"
"\n"
" disassemble_to_groups(\"hello\")\n"
" // -> [[\"h\"], [\"e\"], [\"l\"], [\"l\"], [\"o\"]]\n"
" ```\n"
).
-spec disassemble_to_groups(binary()) -> list(list(binary())).
disassemble_to_groups(Text) ->
_pipe = do_disassemble_to_groups(Text, []),
lists:reverse(_pipe).