Text segmentation, modelled on
Intl.Segmenter.
Splits text into segments by grapheme cluster, word, or sentence boundaries.
:graphemesegmentation uses Elixir's built-inString.graphemes/1and is always available.:wordand:sentencesegmentation requires the optionalunicode_stringdependency. When that library is not installed, these granularities return an error.
segment/2 returns a flat list of segment strings for
simplicity. segment_with_metadata/2 mirrors the JS segment
objects, returning maps with :segment, :index, and
:word_like? keys.
Summary
Functions
Segments a string into a list of substrings.
Segments a string, raising on error.
Segments a string into a list of segment maps with metadata.
Segments a string into segment maps, raising on error.
Functions
Segments a string into a list of substrings.
Arguments
stringis the text to segment.optionsis a keyword list of options.
Options
:granularityis:grapheme,:word, or:sentence. The default is:grapheme.:localeis a locale identifier string. Only used for:wordand:sentencegranularity. The default is"root".:trimis a boolean. Whentrue, whitespace-only segments are removed. Only applies to:wordand:sentencegranularity. The default isfalse.
Returns
{:ok, segments}wheresegmentsis a list of strings.{:error, reason}if the granularity is not supported or theunicode_stringdependency is missing.
Examples
iex> Intl.Segmenter.segment("héllo", granularity: :grapheme)
{:ok, ["h", "é", "l", "l", "o"]}
Segments a string, raising on error.
Same as segment/2 but returns the list directly or raises.
Arguments
stringis the text to segment.optionsis a keyword list of options.
Returns
- A list of segment strings.
Examples
iex> Intl.Segmenter.segment!("héllo", granularity: :grapheme)
["h", "é", "l", "l", "o"]
@spec segment_with_metadata(String.t(), Keyword.t()) :: {:ok, [ %{ segment: String.t(), index: non_neg_integer(), word_like?: boolean() | nil } ]} | {:error, term()}
Segments a string into a list of segment maps with metadata.
Modelled on the JS Intl.Segmenter segment objects. Each map has
a :segment (the text), an :index (the byte offset of the
segment in the input, where JS uses UTF-16 code-unit indexes),
and a :word_like? key. :word_like? is a boolean for
:word granularity — true when the segment contains alphabetic
or numeric content — and nil for other granularities, matching
the JS isWordLike being undefined outside word segmentation.
Arguments
stringis the text to segment.optionsis a keyword list of options. Accepts the same options assegment/2. Withtrim: true, whitespace-only segments are removed after indexing, so the remaining:indexvalues still refer to the original string.
Returns
{:ok, segments}wheresegmentsis a list of%{segment: String.t(), index: non_neg_integer, word_like?: boolean | nil}maps.{:error, reason}if the granularity is not supported or theunicode_stringdependency is missing.
Examples
iex> Intl.Segmenter.segment_with_metadata("Hi there!", granularity: :word)
{:ok, [
%{segment: "Hi", index: 0, word_like?: true},
%{segment: " ", index: 2, word_like?: false},
%{segment: "there", index: 3, word_like?: true},
%{segment: "!", index: 8, word_like?: false}
]}
@spec segment_with_metadata!(String.t(), Keyword.t()) :: [ %{ segment: String.t(), index: non_neg_integer(), word_like?: boolean() | nil } ] | no_return()
Segments a string into segment maps, raising on error.
Same as segment_with_metadata/2 but returns the list directly
or raises.
Arguments
stringis the text to segment.optionsis a keyword list of options.
Returns
A list of
%{segment: String.t(), index: non_neg_integer, word_like?: boolean | nil}maps.
Examples
iex> Intl.Segmenter.segment_with_metadata!("Hi", granularity: :word)
[%{segment: "Hi", index: 0, word_like?: true}]