npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dwscdv3/pinyin

v3.0.0-alpha.6

Published

汉语拼音转换工具。

Downloads

1

Readme

pīnyīn (v3)

pinyin, The convert tool of chinese pinyin.


NPM version Build Status Coverage Status Language Grade: JavaScript NPM downloads

Web Site: 简体中文 | English

README: 简体中文 | English

Convert Han to pinyin. useful for phonetic notation, sorting, and searching.

Note: This module both support Node and Web browser.

Python version see mozillazg/python-pinyin


Feature

  • Segmentation for heteronym words.
  • Support Traditional and Simplified Chinese.
  • Support multiple pinyin style.

Install

via npm:

npm install pinyin@alpha --save

Usage

for developer:

import pinyin from "pinyin";

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

console.log(pinyin("中心", {
  heteronym: true                // Enable heteronym mode.
}));                            // [ [ 'zhōng', 'zhòng' ], [ 'xīn' ] ]

console.log(pinyin("中心", {
  heteronym: true,              // Enable heteronym mode.
  segment: true                 // Enable Chinese words segmentation, fix most heteronym problem.
}));                            // [ [ 'zhōng' ], [ 'xīn' ] ]

console.log(pinyin("我喜欢你", {
  segment: true,                // Enable segmentation. Needed for grouping.
  group: true                   // Group pinyin segments
}));                            // [ [ 'wǒ' ], [ 'xǐhuān' ], [ 'nǐ' ] ]

console.log(pinyin("中心", {
  style: pinyin.STYLE_INITIALS, // Setting pinyin style.
  heteronym: true
}));                            // [ [ 'zh' ], [ 'x' ] ]

console.log(pinyin("华夫人", {
  mode: "surname",              // 姓名模式。
}));                            // [ ['huà'], ['fū'], ['rén'] ]

for cli:

$ pinyin 中心
zhōng xīn
$ pinyin -h

Types

IPinyinOptions

The types for the second argument of pinyin method.

export interface IPinyinOptions {
  style?: IPinyinStyle; // output style of pinyin.
  mode?: IPinyinMode, // mode of pinyin.
  segment?: IPinyinSegment | boolean;
  heteronym?: boolean;
  group?: boolean;
  compact?: boolean;
}

IPinyinStyle

The output style of pinyin.

export type IPinyinStyle =
  "normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | // Suggest.
  "NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" |
  0        | 1      | 2       | 5       | 3          | 4;               // compatibility.

IPinyinMode

The mode of pinyin.

// - NORMAL: Default mode is normal mode.
// - SURNAME: surname mode, for chinese surname.
export type IPinyinMode =
  "normal" | "surname" |
  "NORMAL" | "SURNAME";

IPinyinSegment

The segment method.

  • Default is disable segment: false
  • If set true, use "segmentit" module for segment in Web, use "@node-rs/jieba" for segment in Node.
  • Also specify follow string for segment (bug just "segmentit" in web):
export type IPinyinSegment = "segmentit" | "@node-rs/jieba";

API

<Array> pinyin(words[, options])

Convert Han (汉字) to pinyin.

options argument is optional, for sepcify heteronym mode and pinyin styles.

Return a Array<Array<String>>. If one of Han is heteronym word, it would be have multiple pinyin.

Number pinyin.compare(a, b)

Default compare implementation for pinyin.

Options

<Boolean> options.segment

Enable Chinese word segmentation. Segmentation is helpful for fix heteronym problem, but performance will be more slow, and need more CPU and memory.

Default is false.

<Boolean> options.heteronym

Enable or disable heteronym mode. default is disabled, false.

<Boolean> options.group

Group pinyin by phrases. for example:

我喜欢你
wǒ xǐhuān nǐ

<Object> options.style

Specify pinyin style. please use static properties like STYLE_*. default is .STYLE_TONE. see Static Property for more.

options.mode

pinyin mode, default is pinyin.MODE_NORMAL. If you cleared in surname scene, use pinyin.MODE_SURNAME maybe better.

Static Property

.STYLE_NORMAL

Normal mode.

Example: pin yin

.STYLE_TONE

Tone style, this is default.

Example: pīn yīn

.STYLE_TONE2

tone style by postfix number [0-4].

Example: pin1 yin1

.STYLE_TO3NE

tone style by number [0-4] after phonetic notation character.

Example: pin1 yin1

.STYLE_INITIALS

Initial consonant (of a Chinese syllable).

Example: pinyin of 中国 is zh g

Note: when a Han (汉字) without initial consonant, will convert to empty string.

.STYLE_FIRST_LETTER

First letter style.

Example: p y

pinyin.MODE_NORMAL

Normal mode. This is the default mode.

pinyin.MODE_SURNAME

Surname mode. If chinese word is surname, The pinyin of surname is prioritized.

Test

npm test

Q&A

What's the different Node version and Web version?

pinyin support Node and Web browser now, the API and usage is complete same.

But the Web version is simple than Node version. Just frequently-used dict, without segmentation, and the dict is compress for web.

Because of Traditional and Segmentation, the convert result will be not complete same. and the test case have some different too.

| Feature | Web version | Node version | |--------------|---------------------------------|----------------------------------| | Dict | Frequently-used Dict, Compress. | Complete Dict, without Compress. | | Segmentation | NO | Segmentation options. | | Traditional | NO | Full Traditional support. |

How to sort by pinyin?

This module provide default compare implementation:

const pinyin = require('pinyin');

const data = '我要排序'.split('');
const sortedData = data.sort(pinyin.compare);

But if you need different implementation, do it like:

const pinyin = require('pinyin');

const data = '我要排序'.split('');

// Suggest you to store pinyin result by data persistence.
const pinyinData = data.map(han => ({
  han: han,
  pinyin: pinyin(han)[0][0], // Choose you options and styles.
}));
const sortedData = pinyinData.sort((a, b) => {
  return a.pinyin.localeCompare(b.pinyin);
}).map(d => d.han);

Donate

If this module is helpful for you, please Star this repository.

And you have chioce donate to me via Aliapy or WeChat:

License

MIT