ECMAScript Intl.Segmenter Proposal

repository·master·Indexed 41 days ago

https://github.com/tc39/proposal-intl-segmenter

A standard JavaScript API for Unicode segmentation that allows developers to find boundaries between graphemes, words, and sentences based on locale-specific rules. It provides a `segment()` method to produce an iterable %Segments% object and a `containing()` method for random access to segments.

Tokens
899
Snippets
3
Records
4
Agent score
50%

What's inside Intl.Segmenter

  1. Initialize an Intl.Segmenter

    master

    Create a new locale-dependent segmenter using new Intl.Segmenter(locale, options). The options object allows you to specify the granularity of the segmentation.

    Supported granularity values:

    • "grapheme" (default): Finds boundaries between user-perceived characters.
    • "word": Finds boundaries between words and non-word characters (like spaces or punctuation).
    • "sentence": Finds boundaries between sentences.
    // Create a locale-specific word segmenter
    let segmenter = new Intl.Segmenter("fr", {granularity: "word"});
  2. Segment a string and iterate over results

    master

    Use the segment(string) method on an Intl.Segmenter instance to produce a %Segments% object. This object is an iterable that can be used in a for...of loop to access individual segments.

    Each segment is a plain object containing:

    • segment: The actual string segment.
    • index: The starting code unit index in the original string.
    • input: The original input string.
    • isWordLike: (Only when granularity is "word") true if the segment consists of letters/numbers/ideographs, false if it is whitespace/punctuation, and undefined otherwise.
    let segmenter = new Intl.Segmenter("fr", {granularity: "word"});
    let input = "Moi?  N'est-ce pas.";
    let segments = segmenter.segment(input);
    
    for (let {segment, index, isWordLike} of segments) {
      console.log(segment, index, isWordLike);
    }
  3. Perform random access with containing()

    master

    The %Segments% object provides a containing(index) method for direct random access. This allows you to find the specific segment that encompasses a given code unit index without iterating from the beginning.

    • If the index is within a segment, it returns that segment's data object.
    • If the index is out of bounds, it returns undefined.
    • The index argument is coerced into an integer (e.g., null or NaN becomes 0).
    let input = "Allons-y!";
    let segmenter = new Intl.Segmenter("fr", {granularity: "word"});
    let segments = segmenter.segment(input);
    
    // Returns the segment containing the character at index 0
    let current = segments.containing(0);
    // → { index: 0, segment: "Allons", isWordLike: true }
    
    // Returns the segment containing the character at index 6
    current = segments.containing(6);
    // → { index: 6, segment: "-", isWordLike: false }
  4. Understand the Segment data object structure

    master

    When iterating or using containing(), the returned segment data follows this schema:

    PropertyTypeDescription
    segmentstringThe string segment itself.
    indexnumberThe code unit index where the segment begins.
    inputstringThe original string being segmented.
    isWordLikeboolean or undefinedtrue if granularity is "word" and segment is word-like; false if granularity is "word" and segment is not word-like; undefined if granularity is not "word".