pinyin

repository·master·Indexed 27 days ago

https://github.com/hotoo/pinyin

A tool for converting Chinese characters to pīnyīn, featuring a core library for Node.js and Web projects and a command-line interface (pinyin-cli). It supports various output styles (tone, initials, passport), heteronym mode for polyphonic characters, surname-specific prioritization, and word segmentation via Intl.Segmenter, nodejieba, segmentit, or @node-rs/jieba to improve accuracy.

Tokens
7.7K
Snippets
24
Records
55
Agent score
92%

What's inside pinyin

  1. Overview of the pinyin monorepo structure

    master

    The hotoo/pinyin repository is a monorepo powered by turbo and pnpm. It contains several distinct packages for different usage scenarios:

    • packages/pinyin: The core pinyin conversion engine.
    • packages/pinyin-cli: A command-line tool that can be installed globally for use in any terminal environment.
    • packages/pinyin-react: A React-specific version (marked as coming soon).
    • apps/website: The documentation website.
    • packages/tools: Miscellaneous tools used within the repository.
  2. Compare Node.js vs Web environments

    master

    The pinyin library works in both Node.js and Web browsers, but there are key differences in capabilities:

    FeatureWeb VersionNode.js Version
    DictionaryCompressed common characters.Full character dictionary.
    SegmentationNo segmentation support.Supports segmentation algorithms (improves accuracy).
    Traditional ChineseNot supported.Basic support for conversion.
    PerformanceOptimized for network/client.Optimized for server-side processing.

    Recommendation for Web users: To avoid performance hits on the client, it is recommended to convert Chinese to pinyin on the server and persist the results.

  3. Use the PinyinTable component

    master

    The PinyinTable component provides an audio-enabled interface for pronunciation practice and listening training. Users can click on any Pinyin to hear its pronunciation, which plays through the tones in order: first, second, third, and fourth tone.

    The component accepts an i18n prop to localize the labels for initials, finals, and medial vowels.

    <PinyinTable i18n={{
      initials: "Initials",
      finals: "Finals",
      medial_vowels_i: "Medial Vowels (i)",
      medial_vowels_u: "Medial Vowels (u)",
      medial_vowels_v: "Medial Vowels (ü)",
    }} />
  4. Install and use the pinyin-cli command line tool

    master

    The pinyin-cli package allows you to convert Chinese characters to pīnyīn directly from your terminal. It can be installed globally for use anywhere in your system.

    npm install pinyin-cli -g
    
    # Usage examples
    pinyin 中文
    pinyin --help
  5. Install and use the pinyin core library

    master

    The pinyin package is the core library for converting Chinese characters to pīnyīn. You can install it via npm and use the pinyin function to perform conversions.

    npm install pinyin
    import { pinyin } from 'pinyin';
    
    const py = pinyin('中文');