@echogarden/icu-segmentation-wasm

中文文档
0.4.0发布于 2026/07/24MITTypeScriptES Module近 30 天下载 1.3万

WebAssembly port of the ICU library's character, word, line-break, and sentence segmentation methods.

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. Split operations
  4. Iterator operations
  5. Building the WebAssembly module
  6. License

ICU Segmentation Library (WebAssembly port)

Provides natural language text segmentation based on the ICU (International Components for Unicode) C++ library, ported to WebAssembly.

  • Character segmentation finds the boundaries between grapheme clusters, which may be longer than a single Unicode codepoint, taking into account various linguistic properties
  • Word segmentation finds boundaries between words, based on rulesets. It also supports more challenging languages like Chinese, Japanese, Thai and Khmer, which require specialized lexicons to determine the boundaries
  • Line break boundary detection finds potential locations where a line break can be added, for the purpose of word-wrapping
  • Sentence segmentation finds sentence boundaries based on rulesets and language-specific lexicons
  • Supports all recent JavaScript runtimes (Node.js and browsers)

Installation

npm install @echogarden/icu-segmentation-wasm

Note: package size is about 32 MB uncompressed (13.5 MB gzipped), due to the size of the ICU data bundled into the WebAssembly binary.

Usage

Split operations

Return an array of strings representing the parts.

import * as ICUSegmantation from '@echogarden/icu-segmentation-wasm'

await ICUSegmantation.initialize()

const str = 'Hello World! How are you doing today?'

console.log(ICUSegmantation.splitToCharacters(str))
// Outputs: [
//   'H','e','l','l','o',' ','W','o','r','l','d','!',' ','H','o','w',' ','a','r','e',' ',
//   'y','o', 'u',' ','d','o','i','n','g',' ','t','o','d','a','y','?'
// ]

console.log(ICUSegmantation.splitToWords(str))
// Outputs: [
//	'Hello', ' ', 'World', '!', ' ', 'How', ' ', 'are', ' ',
//  'you', ' ', 'doing', ' ', 'today', '?'
// ]

console.log(ICUSegmantation.splitToSentences(str, 'en'))
// Outputs: [
//   'Hello World! ',
//   'How are you doing today?'
// ]

Iterator operations

Provides an iterator for sequence of boundary indexes.

They return a JavaScript iterator that can be consumed by a for..of loop:

createCharacterBreakIterator(text, lanaguge?)
createWordBreakIterator(text, lanaguge?)
createLineBreakIterator(text, lanaguge?)
createSentenceBreakIterator(text, lanaguge?)

Example usage of an iterator:

const text = 'Привет, мир! Как у тебя дела сегодня?'

for (const boundaryIndex of createSentenceBreakIterator(text, 'ru')) {
    console.log(boundaryIndex)
}

// Outputs:
// 0
// 13
// 37

Building the WebAssembly module

See this guide, which goes through the process of how ICU is built and linked to the WebAssembly wrapper used in this package.

License

MIT

版本与发布记录

@echogarden/icu-segmentation-wasm 共发布 6 个版本,首次发布于 2024/11/27;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.4.0latest2026/07/243 个月前
0.3.02026/07/233 个月前
0.2.22025/05/171.5 年前
0.2.12025/05/121.5 年前
0.2.02025/05/121.5 年前
0.1.02024/11/272 年前

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

@echogarden/icu-segmentation-wasm 是什么?

WebAssembly port of the ICU library's character, word, line-break, and sentence segmentation methods.

如何安装 @echogarden/icu-segmentation-wasm?

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

@echogarden/icu-segmentation-wasm 支持 TypeScript 吗?

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

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

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

@echogarden/icu-segmentation-wasm 可以商用吗?

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

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

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

@echogarden/icu-segmentation-wasm 还在维护吗?

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