### Install dependencies
Source: https://github.com/hotoo/pinyin/blob/master/DEVELOP.md
Run this command to install all project dependencies using pnpm.
```bash
pnpm i
```
--------------------------------
### Install pinyin CLI
Source: https://github.com/hotoo/pinyin/blob/master/apps/website/docs/en-US/cli/index.md
Install the package globally using npm.
```shell
npm i pinyin-cli -g
```
--------------------------------
### Install pinyin packages
Source: https://context7.com/hotoo/pinyin/llms.txt
Commands to install the core library or the CLI tool.
```bash
npm install pinyin --save
```
```bash
npm install pinyin-cli -g
```
--------------------------------
### Import Example Component
Source: https://github.com/hotoo/pinyin/blob/master/apps/website/docs/en-US/EXAMPLE.mdx
Import the Example component from its local path. This component is used for demonstrating pinyin conversion functionalities.
```tsx
import Example from '../../components/example/index.tsx';
```
--------------------------------
### Install pinyin via npm
Source: https://github.com/hotoo/pinyin/blob/master/apps/website/docs/en-US/api/v4/index.md
Use npm to install the pinyin package into your project.
```bash
npm install pinyin --save
```
--------------------------------
### Pinyin CLI Usage Examples
Source: https://context7.com/hotoo/pinyin/llms.txt
The `pinyin-cli` provides command-line access to pinyin conversion features, supporting various styles, modes, segmentation, heteronyms, and custom separators. Examples cover basic conversion to advanced options like grouping and help.
```bash
# Basic conversion
pinyin 中文
# Output: zhōng wén
# Different styles
pinyin -s NORMAL 中文
# Output: zhong wen
pinyin -s TONE2 中文
# Output: zhong1 wen2
pinyin -s FIRST_LETTER 中文
# Output: z w
# Surname mode
pinyin -m SURNAME 华罗庚
# Output: huà luó gēng
# With segmentation
pinyin -S 银行行长
# Output: yín háng háng zhǎng
# Heteronym output
pinyin -h 好
# Output: hǎo,hào
# Compact output with heteronyms
pinyin -h -c 好人
# Output: hǎo,rén hào,rén
# Custom separator
pinyin -p "-" 中文
# Output: zhōng-wén
# Group by phrases
pinyin -S -g 我喜欢你
# Output: wǒ xǐhuān nǐ
# Show help
pinyin --help
```
--------------------------------
### Example Component Usage for Pinyin Conversion
Source: https://github.com/hotoo/pinyin/blob/master/apps/website/docs/en-US/EXAMPLE.mdx
Render the Example component to provide an interface for Chinese to Pinyin conversion. Configure various output styles and options via the i18n prop.
```tsx
```
--------------------------------
### Configure Pinyin Output Styles
Source: https://context7.com/hotoo/pinyin/llms.txt
Examples of different output formats using the style option.
```typescript
import { pinyin } from "pinyin";
const text = "拼音";
// STYLE_TONE (default) - Tone marks on vowels
console.log(pinyin(text, { style: "tone" }));
// Output: [ [ 'pīn' ], [ 'yīn' ] ]
// STYLE_NORMAL - No tone marks
console.log(pinyin(text, { style: "normal" }));
// Output: [ [ 'pin' ], [ 'yin' ] ]
// STYLE_TONE2 - Tone number at end
console.log(pinyin(text, { style: "tone2" }));
// Output: [ [ 'pin1' ], [ 'yin1' ] ]
// STYLE_TO3NE - Tone number after vowel
console.log(pinyin(text, { style: "to3ne" }));
// Output: [ [ 'pi1n' ], [ 'yi1n' ] ]
// STYLE_INITIALS - Initial consonants only
console.log(pinyin(text, { style: "initials" }));
// Output: [ [ 'p' ], [ 'y' ] ]
// STYLE_FIRST_LETTER - First letter only
console.log(pinyin(text, { style: "first_letter" }));
// Output: [ [ 'p' ], [ 'y' ] ]
// STYLE_PASSPORT - Uppercase, ü becomes YU
console.log(pinyin("绿", { style: "passport" }));
// Output: [ [ 'LYU' ] ]
```
--------------------------------
### Sorting by Pinyin
Source: https://github.com/hotoo/pinyin/blob/master/apps/website/docs/en-US/api/v4/index.md
Provides examples of how to sort data using pinyin comparison.
```APIDOC
## Q&A
### How to sort by pinyin?
This module provide default compare implementation:
```javascript
const pinyin = require('pinyin');
const data = '我要排序'.split('');
const sortedData = data.sort(pinyin.compare);
```
But if you need different implementation, do it like:
```javascript
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);
```
```
--------------------------------
### options.group explanation
Source: https://github.com/hotoo/pinyin/blob/master/apps/website/docs/en-US/index.md
Enables grouping of Pinyin results by phrases, which requires segmentation to be active. An example shows grouped Pinyin for '我喜欢你'.
```markdown
### `` options.group
Group pinyin by phrases. for example:
```
我喜欢你
wǒ xǐhuān nǐ
```
```
--------------------------------
### Convert Chinese to Pinyin in TypeScript
Source: https://github.com/hotoo/pinyin/blob/master/apps/website/docs/en-US/api/v4/index.md
Examples of using the pinyin function with various options like heteronym, segmentation, grouping, and styles.
```typescript
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'] ]
```
--------------------------------
### Convert Chinese to Pinyin in TypeScript
Source: https://github.com/hotoo/pinyin/blob/master/apps/website/docs/zh-CN/index.md
Examples of using the pinyin function with various options like heteronym support, segmentation, and custom styles.
```typescript
import { pinyin } from "pinyin";
console.log(pinyin("中心")); // [ [ 'zhōng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
heteronym: true, // 启用多音字模式
})); // [ [ 'zhōng', 'zhòng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
heteronym: true, // 启用多音字模式
segment: true, // 启用分词,以解决多音字问题。默认不开启,使用 true 开启使用 Intl.Segmenter 分词库。
})); // [ [ 'zhōng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
segment: "@node-rs/jieba", // 指定分词库,可以是 "Intl.Segmenter", "nodejieba"、"segmentit"、"@node-rs/jieba"。
})); // [ [ 'zhōng' ], [ 'xīn' ] ]
console.log(pinyin("我喜欢你", {
segment: "segmentit", // 启用分词
group: true, // 启用词组
})); // [ [ 'wǒ' ], [ 'xǐhuān' ], [ 'nǐ' ] ]
console.log(pinyin("中心", {
style: "initials", // 设置拼音风格。
heteronym: true, // 即使有多音字,因为拼音风格选择,重复的也会合并。
})); // [ [ 'zh' ], [ 'x' ] ]
console.log(pinyin("华夫人", {
mode: "surname", // 姓名模式。
})); // [ ['huà'], ['fū'], ['rén'] ]
```
--------------------------------
### Compact Output Mode in Pinyin
Source: https://context7.com/hotoo/pinyin/llms.txt
Use the `compact` option to get all pronunciation combinations as flat arrays, useful for generating all variants of a phrase with heteronyms. This contrasts with the default nested array output.
```typescript
import { pinyin } from "pinyin";
// Without compact - nested arrays
const normalOutput = pinyin("你好吗", { heteronym: true });
console.log(normalOutput);
// Output: [ [ 'nǐ' ], [ 'hǎo', 'hào' ], [ 'ma', 'má', 'mǎ' ] ]
// With compact - all combinations as separate arrays
const compactOutput = pinyin("你好吗", { heteronym: true, compact: true });
console.log(compactOutput);
// Output: [
// [ 'nǐ', 'hǎo', 'ma' ], [ 'nǐ', 'hǎo', 'má' ], [ 'nǐ', 'hǎo', 'mǎ' ],
// [ 'nǐ', 'hào', 'ma' ], [ 'nǐ', 'hào', 'má' ], [ 'nǐ', 'hào', 'mǎ' ]
// ]
```
--------------------------------
### Sort Data by Pinyin
Source: https://github.com/hotoo/pinyin/blob/master/apps/website/docs/zh-CN/index.md
Examples for sorting Chinese characters using the default comparator or a custom implementation.
```javascript
const pinyin = require('pinyin');
const data = '我要排序'.split('');
const sortedData = data.sort(pinyin.compare);
```
```javascript
const pinyin = require('pinyin');
const data = '我要排序'.split('');
// 建议将汉字的拼音持久化存储起来。
const pinyinData = data.map(han => ({
han: han,
pinyin: pinyin(han)[0][0], // 可以自行选择不同的生成拼音方案和风格。
}));
const sortedData = pinyinData.sort((a, b) => {
return a.pinyin.localeCompare(b.pinyin);
}).map(d => d.han);
```
--------------------------------
### Sorting Chinese Text with pinyin.compare
Source: https://context7.com/hotoo/pinyin/llms.txt
The `compare` function is designed for sorting Chinese text alphabetically by pinyin and can be directly used with `Array.sort()`. A custom sorting example demonstrates more control using `pinyin` with `localeCompare`.
```typescript
import { pinyin, compare } from "pinyin";
// Sort Chinese characters alphabetically by pinyin
const characters = '我要排序'.split('');
const sorted = characters.sort(compare);
console.log(sorted.join(''));
// Output: 排我序要
// Custom sorting with more control
const words = ['北京', '上海', '广州', '深圳'];
const sortedWords = words.sort((a, b) => {
const pyA = pinyin(a, { style: 'normal' }).flat().join('');
const pyB = pinyin(b, { style: 'normal' }).flat().join('');
return pyA.localeCompare(pyB);
});
console.log(sortedWords);
// Output: [ '北京', '广州', '上海', '深圳' ]
```
--------------------------------
### Run Tests with npm
Source: https://github.com/hotoo/pinyin/blob/master/apps/website/docs/en-US/api/v4/index.md
Execute the test suite for the pinyin module using npm.
```bash
npm test
```
--------------------------------
### CLI Options
Source: https://github.com/hotoo/pinyin/blob/master/apps/website/docs/en-US/cli/index.md
List of available command-line flags and their descriptions.
```text
-h, --help output usage information
-V, --version output the version number
-v, --version output the version number
-s, --style