Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@napi-rs/pinyin

Brooooooklyn46.7kMIT1.7.5TypeScript support: included

Fastest Chinese words to pinyin library

napi-rs, NAPI, N-API, Rust, node-addon, node-addon-api, pinyin, jieba, 拼音, 汉语, 汉字, 中文

readme

@napi-rs/pinyin

Build Status Downloads License

🚀 Help me to become a full-time open-source developer by sponsoring me on Github

rust-pinyin Node.js 版本,不支持 web.

功能

  • 超高性能
  • postinstallnode-gyp,纯净安装无烦恼
  • Node.js 版本支持,升级 Node 版本无需 rebuild/reinstall
  • linux alpine 支持
  • 分词再转拼音
  • 原生异步支持,可运行在 libuv 线程池中,不阻塞主线程

安装

yarn add @napi-rs/pinyin

系统/Node.js 版本

| | node12 | node14 | node16 | node17 | | ---------------- | ------ | ------ | ------ | ------ | | Windows x64 | ✓ | ✓ | ✓ | ✓ | | Windows x32 | ✓ | ✓ | ✓ | ✓ | | Windows arm64 | ✓ | ✓ | ✓ | ✓ | | macOS x64 | ✓ | ✓ | ✓ | ✓ | | macOS arm64 | ✓ | ✓ | ✓ | ✓ | | Linux x64 gnu | ✓ | ✓ | ✓ | ✓ | | Linux x64 musl | ✓ | ✓ | ✓ | ✓ | | Linux arm gnu | ✓ | ✓ | ✓ | ✓ | | Linux arm64 gnu | ✓ | ✓ | ✓ | ✓ | | Linux arm64 musl | ✓ | ✓ | ✓ | ✓ | | Android arm64 | ✓ | ✓ | ✓ | ✓ | | Android armv7 | ✓ | ✓ | ✓ | ✓ | | FreeBSD x64 | ✓ | ✓ | ✓ | ✓ |

pinyin 性能对比

Benchmark over pinyin and pinyin-pro package:

Note

pinyin-pro doesn't support segment feature.

System info

OS: macOS 12.3.1 21E258 arm64
Host: MacBookPro18,2
Kernel: 21.4.0
Shell: zsh 5.8
CPU: Apple M1 Max
GPU: Apple M1 Max
Memory: 9539MiB / 65536MiB
Running "Short input without segment" suite...
Progress: 100%

  @napi-rs/pinyin:
    2 183 922 ops/s, ±0.32%   | fastest

  pinyin-pro:
    1 603 486 ops/s, ±0.10%   | slowest, 26.58% slower

  node-pinyin:
    2 150 629 ops/s, ±0.21%   | 1.52% slower

Finished 3 cases!
  Fastest: @napi-rs/pinyin
  Slowest: pinyin-pro
Running "Long input without segment" suite...
Progress: 100%

  @napi-rs/pinyin:
    551 ops/s, ±0.55%   | fastest

  pinyin-pro:
    182 ops/s, ±11.67%   | slowest, 66.97% slower

  node-pinyin:
    226 ops/s, ±14.00%   | 58.98% slower

Finished 3 cases!
  Fastest: @napi-rs/pinyin
  Slowest: pinyin-pro
Running "Short input with segment" suite...
Progress: 100%

  @napi-rs/pinyin:
    863 839 ops/s, ±0.61%   | fastest

  node-pinyin:
    710 893 ops/s, ±0.58%   | slowest, 17.71% slower

Finished 2 cases!
  Fastest: @napi-rs/pinyin
  Slowest: node-pinyin
Running "Long input with segment" suite...
Progress: 100%

  @napi-rs/pinyin:
    304 ops/s, ±1.99%   | fastest

  node-pinyin:
    8 ops/s, ±2.85%     | slowest, 97.37% slower

Finished 2 cases!
  Fastest: @napi-rs/pinyin
  Slowest: node-pinyin

用法

同步

import { pinyin } from '@napi-rs/pinyin'

console.log(pinyin('中心')) // [ [ 'zhōng' ], [ 'xīn' ] ]

异步

import { asyncPinyin } from '@napi-rs/pinyin'

asyncPinyin('中心').then(console.log.bind(console)) // [ [ 'zhōng' ], [ 'xīn' ] ]

参数

  • input <string>

    需要转拼音的中文字符串

  • options? <Options>

    转拼音参数

    • Options.heteronym? <boolean>

      是否处理多音字, 默认 false。如果为 true,返回类型为 string[][]/Promise<string[][]>, 如果为 false 返回类型为 string[]/Promise<string[]>

    • Options.style? <PINYIN_STYLE>

      拼音风格,默认为 PINYIN_STYLE.WithTone 可选值为:

      • Plain 普通风格,不带声调

      • WithTone 带声调的风格

      • WithToneNum 声调在各个拼音之后,使用数字 1-4 表示的风格

      • WithToneNumEnd 声调在拼音最后,使用数字 1-4 表示的风格

      • FirstLetter 首字母风格

    • Options.segment? <boolean>

      是否开启分词。输入有多音字时,开启分词可以获得更准确结果。

changelog

1.7.5 (2024-03-24)

Bug Fixes

1.7.4 (2024-03-21)

Bug Fixes

1.7.3 (2023-05-24)

1.7.2 (2023-05-24)

Bug Fixes

  • deps: update rust crate pinyin to 0.10 (#225) (444690e)

Performance Improvements

  • improve performance by reduce memory allocation (23f7307)

1.7.1 (2022-10-07)

1.7.0 (2021-12-22)

Features

1.6.2 (2021-07-22)

Bug Fixes

  • fix linux aarch64 musl build (a2792f1)

1.6.1 (2021-06-18)

Bug Fixes

  • switch to mimalloc on all platforms (0166e2c)

1.6.0 (2021-06-04)

Bug Fixes

  • deps: update rust crate once_cell to 1.7 (960bf4a)

1.5.1 (2021-01-28)

Features

1.5.0 (2021-01-06)

1.4.0 (2020-12-30)

1.3.0 (2020-12-01)

Features

  • support aarch64-darwin/linux (b3ee719)

1.2.0 (2020-11-03)

Bug Fixes

  • mixed Chinese and non-Chinese words (40be9e7)

1.1.0 (2020-11-01)

Features

  • support segment and sort (5433c8f)

1.0.0 (2020-10-31)