@echogarden/text-segmentation

中文文档
0.8.1发布于 2026/09/02MITTypeScriptES Module近 30 天下载 1.6万

A library for multilingual word, phrase and sentence segmentation.

原始使用文档
文档目录11 节
  1. Installation
  2. Usage
  3. Splitting to words
  4. Language mixtures
  5. Getting detailed metadata
  6. Segmenting to words, phrases and sentences
  7. Segmenting a predefined word sequence
  8. Algorithm outline
  9. Word segmentation
  10. Sentence and phrase segmentation
  11. License

Echogarden text segmentation library

A library providing word, phrase and sentence segmentation for natural language text.

  • Fully multilingual. Covers all languages and writing systems representable as Unicode characters. When needed, applies second stage processing using a WebAssembly port of the ICU C++ Library (if available) for handling difficult to segment east-Asian languages, like Chinese, Japanese, Thai and Khmer
  • Segments mixtures of different languages, including mixtures of different scripts, like Chinese, Cyrillic and Latin, all within a single sentence or phrase, without needing to explicitly specify a particular language
  • Includes built-in suppression lists for language-specific abbreviations and special word patterns. Currently included languages are English, German, Spanish, French, Italian, Portuguese and Russian, mostly extracted from the CLDR JSON datasets
  • Accepts user-provided suppression lists
  • Very fast. For most languages, word segmentation is done mostly via a single regular expression, dynamically built for the given options, and suppression set. Majority of processing is done within the JavaScript regular expression engine, or within optimized WebAssembly binaries
  • Written in TypeScript

Installation

npm install @echogarden/text-segmentation

To enable fine-grained segmentation of Chinese, Japanese, Thai and Khmer words, install this additional package:

npm install @echogarden/icu-segmentation-wasm

(@echogarden/icu-segmentation-wasm is a 27 MB package, and is set as a peer dependency. For size reasons, it's not installed by default, to ease on deployments that may not require it - especially browsers)

Usage

Splitting to words

import { splitToWords } from '@echogarden/text-segmentation'

const wordSequence: WordSequence =
    await splitToWords('Hello, world! How are you doing today?', { language: 'en' })

console.log(wordSequence.words)

Prints a list of words (including spaces and punctuation):

['Hello', ',', ' ', 'world', '!', ' ', 'How',' ', 'are', ' ', 'you', ' ', 'doing', ' ', 'today', '?']

Language mixtures

A language option provided like language: 'en' is only used for loading suppression dictionaries. You can still include mixtures of different languages and scripts within the same input text:

await splitToWords('Hello world! Привет мир! 你好世界!')

Producing:

['Hello', ' ', 'world', '!', ' ', 'Привет', ' ', 'мир', '!', ' ', '你好', '世界', '!']

Getting detailed metadata

To get more detailed information,

console.log(wordSequence.entries)

prints a list of objects, including metadata on each word:

[
    { text: 'Hello', startOffset: 0, endOffset: 5, isPunctuation: false },
    { text: ',', startOffset: 5, endOffset: 6, isPunctuation: true },
    { text: ' ', startOffset: 6, endOffset: 7, isPunctuation: true },
    { text: 'world', startOffset: 7, endOffset: 12, isPunctuation: false },
    { text: '!', startOffset: 12, endOffset: 13, isPunctuation: true },
    { text: ' ', startOffset: 13, endOffset: 14, isPunctuation: true },
    { text: 'How', startOffset: 14, endOffset: 17, isPunctuation: false },
    { text: ' ', startOffset: 17, endOffset: 18, isPunctuation: true },
    { text: 'are', startOffset: 18, endOffset: 21, isPunctuation: false },
    { text: ' ', startOffset: 21, endOffset: 22, isPunctuation: true },
    { text: 'you', startOffset: 22, endOffset: 25, isPunctuation: false },
    { text: ' ', startOffset: 25, endOffset: 26, isPunctuation: true },
    { text: 'doing', startOffset: 26, endOffset: 31, isPunctuation: false },
    { text: ' ', startOffset: 31, endOffset: 32, isPunctuation: true },
    { text: 'today', startOffset: 32, endOffset: 37, isPunctuation: false },
    { text: '?', startOffset: 37, endOffset: 38, isPunctuation: true }
]

Segmenting to words, phrases and sentences

const result: SegmentationResult =
    await segmentText(`Hello, world! How are you doing today?`)

result is a nested object containing a breakdown of sentences, phrases and words in the given text. It is described by these TypeScript types:

interface SegmentationResult {
    words: WordSequence
    sentences: Sentence[]
}

interface Sentence {
    text: string
    charRange: Range
    wordRange: Range
    words: WordSequence

    phrases: Phrase[]
}

interface Phrase {
    text: string
    charRange: Range
    wordRange: Range
    words: WordSequence
}

interface Range {
    start: number
    end: number
}

Segmenting a predefined word sequence

If you have a pre-existing WordSequence object (or possibly a modified form of an existing one), you can segment it to sentences and phrases without needing to recompute the word boundaries:

const result: SegmentationResult = await segmentWordSequence(wordSequence: WordSequence)

In this way, you can also specify custom word boundaries, and the phrase and sentence segmentation operations would ensure that break characters are never identified within the spans of non-punctuation words.

Algorithm outline

Word segmentation

Looks for one out of several accepted character sequence patterns, and identifies that pattern as an individual word.

There are several types of accepted patterns, evaluated in this order:

  • User-provided suppressions
  • Language-specific suppressions. For example, in English it would include abbreviations like Mr., Mrs., e.g., i.e, etc., or contractions like and 'cause, 'bout in English, 'n in Afrikaans
  • Noun suppressions, shared in all languages, like names of brands, misc. abbreviations and programming languages. Examples: C#, F#, C++, Yahoo!, Toys"R"Us, Dunkin', Ke$ha, Sky+, I/O, A/C, A/V
  • Top-level domains, like .com, .org, .net (also doubling as the noun .NET as in the ".NET framework")
  • Number patterns, consisting of a sequences of digits separated by various separator characters, like decimal separators 3.14, 3,14, and thousands separators like 233,421 (,), 233.421 (.) or 233 421 ( ). optional - or + signs like -34,534.123, +43 345,344, or scientific notation like 1.554e-34
  • Time patterns like 15:12 and 06:34:23
  • Date patterns like 10/15/2021 (currently disabled)
  • Dimension patterns like 53x545x76
  • Percentage patterns, which are number patterns preceded or followed by %, like 53.243% or %34.12
  • Currency patterns like $101.25, €50, 20£, -53.23¥, which, like percentage patterns, are number patterns preceded or followed by a currency symbol
  • Emojis like 😄, 🎉, 👨‍👩‍👧‍👦
  • Abbreviation patterns like Y.M.C.A: these patterns will be automatically matched (no special suppressions needed) if there is a sequence of single . alternating between single letters (like x.y.z), optionally, there may be a space between the characters, like x. y. z.
  • Dot connected word sequences like abc.def.g12
  • Underscore connected word sequence abc_def_g12
  • Interpunct connected word sequence abc·def·g12
  • And finally: word character sequences consisting of letter characters (Unicode category Letter), mark characters (Unicode category Mark) or digit characters (Unicode category Decimal_Number), which may include inner apostrophes like ' and ’ and inner separators like -, _ ·

Current limitations:

  • No current general identification of preceding or trailing apostrophes, like in English possessive plurals "The brothers' friend" or "The diplomats' contracts". Reason: this requires a large lexicon or more sophisticated language understanding, since the apostrophe is generally ambiguous with a single quote.

Further segmentation for words containing Chinese, Japanese, Thai, or Khmer characters

This requires the the @echogarden/icu-segmentation-wasm package to be manually installed.

  • Iterate each previously identified word span
  • Check if the word span contains at at least one codepoint belonging to the supported east Asian languages character ranges
  • Load the ICU module on the first match that is found (due to a startup delay of about 40ms - 80ms, the module is only loaded if absolutely needed)
  • Split the word span further using the ICU library

Sentence and phrase segmentation

Sentence segmentation:

  • Split to words, or use a pre-supplied word sequence
  • Identify sentence enders, like ., 。, ?, ?, !, ! or a line break, outside word spans, and identify sentence boundaries based on them
  • Extend sentence spans with any number of possible trailing punctuation characters, after the ender character, like ”, ’, ), ], }, », line breaks or other whitespace

Phrase segmentation:

  • Iterate over the sentences detected
  • Within each sentence, identify phrase separators like ,, 、, ,, ،, ;, ;, :, :, — outside word spans, and split phrases based on them
  • Extend phrase spans with any number of trailing punctuation characters, up to the end of the containing sentence

License

MIT

版本与发布记录

@echogarden/text-segmentation 共发布 15 个版本,首次发布于 2024/11/29;过去 12 个月发布了 5 个版本。

版本发布日期距今
0.8.1latest2026/09/022 个月前
0.8.02026/08/302 个月前
0.7.02026/07/243 个月前
0.6.02026/07/233 个月前
0.5.02026/07/203 个月前
0.4.12025/08/311.2 年前

关于 @echogarden/text-segmentation 的常见问题

@echogarden/text-segmentation 是什么?

A library for multilingual word, phrase and sentence segmentation.

如何安装 @echogarden/text-segmentation?

在项目目录运行 npm install @echogarden/text-segmentation,或使用 pnpm add / yarn add / bun add。

@echogarden/text-segmentation 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@echogarden/text-segmentation 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@echogarden/text-segmentation 可以商用吗?

@echogarden/text-segmentation 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@echogarden/text-segmentation 的下载量有多少?

根据 npm 官方下载统计,@echogarden/text-segmentation 在 2026/10/13 – 2026/11/11 这 30 天内被下载 15,954 次,日均约 532 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@echogarden/text-segmentation 还在维护吗?

最新版本 0.8.1 发布于 2026/09/02(2 个月前);至今共发布 15 个版本,其中过去 12 个月发布了 5 个;首次发布于 2024/11/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。