Top Related Projects
汉字转拼音(pypinyin)
A copy of http://sourceforge.net/projects/pinyin4j, then deploy it to maven central repository.
Quick Overview
overtrue/pinyin is a PHP library for converting Chinese characters to Pinyin (romanization of Chinese). It provides a simple and efficient way to transliterate Chinese text into its phonetic representation using the Latin alphabet, which is useful for various applications such as sorting, searching, and pronunciation assistance.
Pros
- Easy to use with a simple API
- Supports multiple output formats (with or without tones, with or without spaces)
- Handles polyphones (characters with multiple pronunciations) based on context
- Actively maintained and well-documented
Cons
- Limited to Mandarin Chinese (doesn't support other Chinese dialects)
- May not always provide perfect accuracy for uncommon or ambiguous characters
- Requires PHP, which might not be suitable for all project environments
Code Examples
- Basic usage:
use Overtrue\Pinyin\Pinyin;
$pinyin = new Pinyin();
echo $pinyin->sentence('带着希望去旅行,比到达终点更美好');
// Output: dai zhe xi wang qu lv xing, bi dao da zhong dian geng mei hao
- Converting to Pinyin with tones:
use Overtrue\Pinyin\Pinyin;
$pinyin = new Pinyin();
echo $pinyin->sentence('带着希望去旅行,比到达终点更美好', PINYIN_TONE);
// Output: dài zhe xī wàng qù lǚ xíng, bǐ dào dá zhōng diǎn gēng měi hǎo
- Converting to Pinyin initials:
use Overtrue\Pinyin\Pinyin;
$pinyin = new Pinyin();
echo $pinyin->abbr('带着希望去旅行');
// Output: dzxwqlx
Getting Started
To use overtrue/pinyin in your PHP project, follow these steps:
-
Install the library using Composer:
composer require overtrue/pinyin
-
Include the Composer autoloader in your PHP file:
require 'vendor/autoload.php';
-
Use the library in your code:
use Overtrue\Pinyin\Pinyin; $pinyin = new Pinyin(); echo $pinyin->sentence('你好世界');
This will output: ni hao shi jie
Competitor Comparisons
汉字转拼音(pypinyin)
Pros of python-pinyin
- Written in Python, making it more accessible for Python developers
- Supports multiple output formats (e.g., numerical tone marks, diacritical marks)
- Includes a command-line interface for easy use outside of Python scripts
Cons of python-pinyin
- Generally slower performance compared to pinyin
- Less extensive documentation and examples
- Smaller community and fewer contributors
Code Comparison
python-pinyin:
from pypinyin import pinyin, Style
print(pinyin('中心', style=Style.TONE))
# Output: [['zhōng'], ['xīn']]
pinyin:
use Overtrue\Pinyin\Pinyin;
echo Pinyin::sentence('中心');
// Output: zhong xin
Key Differences
- Language: python-pinyin is written in Python, while pinyin is in PHP
- Output flexibility: python-pinyin offers more output style options
- Performance: pinyin generally has better performance for large-scale text processing
- Community: pinyin has a larger user base and more active development
- Integration: Each library is better suited for projects in its respective language ecosystem
Both libraries serve the purpose of converting Chinese characters to Pinyin, but they cater to different programming environments and use cases. The choice between them largely depends on the project's language, performance requirements, and specific Pinyin formatting needs.
A copy of http://sourceforge.net/projects/pinyin4j, then deploy it to maven central repository.
Pros of pinyin4j
- Written in Java, making it suitable for Java-based projects and Android development
- Supports multiple pinyin output formats (with or without tone marks, numbers, etc.)
- Includes a comprehensive dictionary for accurate character-to-pinyin conversion
Cons of pinyin4j
- Less actively maintained compared to pinyin (last update in 2016)
- Limited to Java ecosystem, while pinyin supports multiple programming languages
- Lacks some advanced features like customizable dictionaries found in pinyin
Code Comparison
pinyin4j:
PinyinHelper.toHanyuPinyinStringArray('中')[0]; // "zhong1"
PinyinFormat format = new PinyinFormat();
format.setToneType(ToneType.WITHOUT_TONE);
PinyinHelper.toHanYuPinyinString("中国", format); // "zhong guo"
pinyin:
Pinyin::convert('中国'); // ["zhong", "guo"]
Pinyin::sentence('中国'); // "zhong guo"
Pinyin::abbr('中国'); // "zg"
Both libraries provide methods for converting Chinese characters to pinyin, but pinyin offers more concise syntax and additional features like abbreviation generation. pinyin4j provides more control over output formats within its Java environment, while pinyin supports a wider range of programming languages and use cases.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Pinyin
:cn: åºäº mozillazg/pinyin-data è¯å ¸çä¸æ转æ¼é³å·¥å ·ï¼æ´åç¡®çæ¯æå¤é³åçæ±å转æ¼é³è§£å³æ¹æ¡ã
å欢æç项ç®ï¼ç¹å»è¿éæ¯ææ
å®è£
ä½¿ç¨ Composer å®è£ :
composer require overtrue/pinyin:^5.0
使ç¨
æ¼é³é£æ ¼
é¤äºè·åé¦åæ¯çæ¹æ³å¤ï¼æææ¹æ³é½æ¯æ第äºä¸ªåæ°ï¼ç¨äºæå®æ¼é³çæ ¼å¼ï¼å¯éå¼ä¸ºï¼
symbol
ï¼é»è®¤ï¼å£°è°ç¬¦å·ï¼ä¾å¦pÄ«n yÄ«n
none
ä¸è¾åºæ¼é³ï¼ä¾å¦pin yin
number
æ«å°¾æ°å模å¼çæ¼é³ï¼ä¾å¦pin1 yin1
è¿åå¼
é¤äº permalink
è¿åå符串å¤ï¼å
¶å®æ¹æ³é½è¿åéåç±»å Overtrue\Pinyin\Collection
ï¼
use Overtrue\Pinyin\Pinyin;
$pinyin = Pinyin::sentence('ä½ å¥½ï¼ä¸ç');
ä½ å¯ä»¥éè¿ä»¥ä¸æ¹å¼è®¿é®éåå 容:
echo $pinyin; // nÇ hÇo shì jiè
// ç´æ¥å°å¯¹è±¡è½¬æå符串
$string = (string) $pinyin; // nÇ hÇo shì jiè
$pinyin->toArray(); // ['nÇ', 'hÇo', 'shì', 'jiè']
// ç´æ¥ä½¿ç¨ç´¢å¼è®¿é®
$pinyin[0]; // 'nÇ'
// 使ç¨å½æ°éå
$pinyin->map('ucfirst'); // ['NÇ', 'HÇo', 'Shì', 'Jiè']
// æ¼æ¥ä¸ºå符串
$pinyin->join(' '); // 'nÇ hÇo shì jiè'
$pinyin->join('-'); // 'nÇ-hÇo-shì-jiè'
// 转æ json
$pinyin->toJson(); // '["nÇ","hÇo","shì","jiè"]'
json_encode($pinyin); // '["nÇ","hÇo","shì","jiè"]'
æå段è½è½¬æ¼é³
use Overtrue\Pinyin\Pinyin;
echo Pinyin::sentence('带çå¸æå»æ
è¡ï¼æ¯å°è¾¾ç»ç¹æ´ç¾å¥½');
// dà i zhe xÄ« wà ng qù lÇ xÃng ï¼ bÇ dà o dá zhÅng diÇn gèng mÄi hÇo
// å»é¤å£°è°
echo Pinyin::sentence('带çå¸æå»æ
è¡ï¼æ¯å°è¾¾ç»ç¹æ´ç¾å¥½', 'none');
// dai zhe xi wang qu lv xing ï¼ bi dao da zhong dian geng mei hao
// ä¿çææéæ±åå符
echo Pinyin::fullSentence('ã«æ¯çååï¼Ïæ¯å¸è
åæ¯', 'none');
// ã« shi pian jia ming ï¼Ï shi xi la zi mu
çæç¨äºé¾æ¥çæ¼é³å符串
é常ç¨äºæç« é¾æ¥çï¼å¯ä»¥ä½¿ç¨ permalink
æ¹æ³è·åæ¼é³å符串ï¼
echo Pinyin::permalink('带çå¸æå»æ
è¡'); // dai-zhe-xi-wang-qu-lyu-xing
echo Pinyin::permalink('带çå¸æå»æ
è¡', '.'); // dai.zhe.xi.wang.qu.lyu.xing
è·åé¦å符å符串
é常ç¨äºå建æç´¢ç¨çç´¢å¼ï¼å¯ä»¥ä½¿ç¨ abbr
æ¹æ³è½¬æ¢ï¼
Pinyin::abbr('带çå¸æå»æ
è¡'); // ['d', 'z', 'x', 'w', 'q', 'l', 'x']
echo Pinyin::abbr('带çå¸æå»æ
è¡')->join('-'); // d-z-x-w-q-l-x
echo Pinyin::abbr('ä½ å¥½2018ï¼')->join(''); // nh2018
echo Pinyin::abbr('Happy New Year! 2018ï¼')->join(''); // HNY2018
// ä¿çåå符串çè±æåè¯
echo Pinyin::abbr('CGVçµå½±é¢', false, true)->join(''); // CGVdyy
å§åé¦åæ¯
å°é¦åä½ä¸ºå§æ°è½¬æ¢ï¼å ¶ä½ä½ä¸ºæ®éè¯è¯è½¬æ¢ï¼
Pinyin::nameAbbr('欧é³'); // ['o', 'y']
echo Pinyin::nameAbbr('ååå')->join('-'); // s-d-d
å§å转æ¢
å§åçå§ç读é³æäºä¸æ®éåä¸ä¸æ ·ï¼æ¯å¦ âåâ 常è§çé³ä¸º dan
ï¼èä½ä¸ºå§çæ¶å读 shan
ã
Pinyin::name('åææ'); // ['shà n', 'mÇu', 'mÇu']
Pinyin::name('åææ', 'none'); // ['shan', 'mou', 'mou']
Pinyin::name('åææ', 'none')->join('-'); // shan-mou-mou
æ¤ç §å§å转æ¢
æ ¹æ®å½å®¶è§å® å
³äºä¸å½æ¤ç
§æ
è¡è¯ä¸å§åæ¼é³ üï¼åãå¾ãé¾ã绿ã女çï¼ç»ä¸æ¼å为 YU çæé çè§åï¼å° ü
转æ¢ä¸º yu
ï¼
Pinyin::passportName('åå°å¸'); // ['lyu', 'xiao', 'bu']
Pinyin::passportName('女å°è±'); // ['nyu', 'xiao', 'hua']
Pinyin::passportName('å¾å¸'); // ['lyu', 'shi']
å¤é³å
å¤é³åçè¿åå¼ä¸ºå ³èæ°ç»çéåï¼é»è®¤è¿åå»éåçææ读é³ï¼
$pinyin = Pinyin::polyphones('éåº');
$pinyin['é']; // ["zhòng", "chóng", "tóng"]
$pinyin['åº']; // ["qìng"]
$pinyin->toArray();
// [
// "é": ["zhòng", "chóng", "tóng"],
// "åº": ["qìng"]
// ]
å¦æä¸æ³è¦å»éï¼å¯ä»¥æ°ç»å½¢å¼è¿åï¼
$pinyin = Pinyin::polyphones('éåºéåº', Converter::TONE_STYLE_SYMBOL, true);
// or
$pinyin = Pinyin::polyphonesAsArray('éåºéåº', Converter::TONE_STYLE_SYMBOL);
$pinyin->toArray();
// [
// ["é" => ["zhòng", "chóng", "tóng"]],
// ["åº" => ["qìng"]],
// ["é" => ["zhòng", "chóng", "tóng"]],
// ["åº" => ["qìng"]]
// ]
åå转æ¼é³
åå¤é³å类似ï¼ååçè¿åå¼ä¸ºå符串ï¼å¤é³åå°æ ¹æ®è¯¥ååé¢è°æ´å¾å°å¸¸ç¨é³ï¼
$pinyin = Pinyin::chars('éåº');
echo $pinyin['é']; // "zhòng"
echo $pinyin['åº']; // "qìng"
$pinyin->toArray();
// [
// "é": "zhòng",
// "åº": "qìng"
// ]
Warning
å½ååå¤çæ¶ç±äºå¤é³åæ¥èªè¯é¢è¡¨ä¸åå¾å¸¸ç¨é³ï¼æ以å¨è¯è¯ç¯å¢ä¸å¯è½åºç°ä¸æ£ç¡®çæ åµï¼å»ºè®®ä½¿ç¨å¤é³åå¤çã
æ´å¤ä½¿ç¨è¯·åè æµè¯ç¨ä¾ã
v/yu/ü çé®é¢
æ ¹æ®å½å®¶è¯è¨æåå·¥ä½å§åä¼çè§å®ï¼lv
ãlyu
ãlÇ
é½æ¯æ£ç¡®çï¼ä½æ¯ lv
æ¯æ常ç¨çï¼æ以é»è®¤ä½¿ç¨ lv
ï¼å¦æä½ éè¦ä½¿ç¨å
¶ä»çï¼å¯ä»¥å¨åå§åæ¶ä¼ å
¥ï¼
echo Pinyin::sentence('æ
è¡');
// lÇ xÃng
echo Pinyin::sentence('æ
è¡', 'none');
// lv xing
echo Pinyin::yuToYu()->sentence('æ
è¡', 'none');
// lyu xing
echo Pinyin::yuToU()->sentence('æ
è¡', 'none');
// lu xing
echo Pinyin::yuToV()->sentence('æ
è¡', 'none');
// lv xing
Warning
ä» å¨æ¼é³é£æ ¼ä¸ºé
none
模å¼ä¸ææã
å½ä»¤è¡å·¥å ·
ä½ å¯ä»¥ä½¿ç¨å½ä»¤è¡æ¥å®ç°æ¼é³ç转æ¢ï¼
php ./bin/pinyin 带çå¸æå»æ
è¡ --method=sentence --tone-style=symbol
# dà i zhe xÄ« wà ng qù lÇ xÃng
æ´å¤ä½¿ç¨æ¹æ³ï¼å¯ä»¥æ¥ç帮å©ææ¡£ï¼
php ./bin/pinyin --help
# Usage:
# ./pinyin [chinese] [method] [options]
# Options:
# -j, --json è¾åº JSON æ ¼å¼.
# -c, --compact ä¸æ ¼å¼åè¾åº JSON.
# -m, --method=[method] 转æ¢æ¹å¼ï¼å¯éï¼sentence/sentenceFull/permalink/abbr/nameAbbr/name/passportName/phrase/polyphones/chars.
# --no-tone ä¸ä½¿ç¨é³è°.
# --tone-style=[style] é³è°é£æ ¼ï¼å¯éå¼ï¼symbol/none/number, default: none.
# -h, --help æ¾ç¤ºå¸®å©.
å¨ Laravel ä¸ä½¿ç¨
ç¬ç«çå å¨è¿éï¼overtrue/laravel-pinyin
ä¸æç®ç¹è½¬æ¢
å¦ä½ä½ æè¿ä¸ªéæ±ï¼ä¹å¯ä»¥äºè§£æçå¦ä¸ä¸ªå ï¼overtrue/php-opencc
Contribution
欢è¿ææè§åå®åè¡¥å è¯åºï¼
- ååæ¼é³é误请添å å°ï¼sources/pathes/chars.txtï¼
- è¯è¯é误æè¡¥é½ï¼è¯·æ·»å å°ï¼sources/pathes/words.txtï¼
åè
:heart: Sponsor me
å¦æä½ å欢æç项ç®å¹¶æ³æ¯æå®ï¼ç¹å»è¿é :heart:
Project supported by JetBrains
Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.
PHP æ©å±å å¼å
æ³ç¥éå¦ä½ä»é¶å¼å§æ建 PHP æ©å±å ï¼
è¯·å ³æ³¨æçå®æ课ç¨ï¼æä¼å¨æ¤è¯¾ç¨ä¸å享ä¸äºæ©å±å¼åç»éª ââ ãPHP æ©å±å å®ææç¨ - ä»å ¥é¨å°åå¸ã
License
MIT
Top Related Projects
汉字转拼音(pypinyin)
A copy of http://sourceforge.net/projects/pinyin4j, then deploy it to maven central repository.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot