### Install pypinyin using uv Source: https://github.com/mozillazg/python-pinyin/blob/master/README_en.rst Use this command to install the pypinyin library via uv. ```bash uv add pypinyin ``` -------------------------------- ### Install pypinyin using pip Source: https://github.com/mozillazg/python-pinyin/blob/master/README_en.rst Use this command to install the pypinyin library via pip. ```bash pip install pypinyin ``` -------------------------------- ### Using Custom Phrases Dictionary with Jieba Source: https://github.com/mozillazg/python-pinyin/blob/master/CHANGELOG.rst Shows how to load a custom phrases dictionary for pinyin conversion using the jieba segmentation module. This example is from version 0.25.0. ```python >>> from pypinyin import pinyin, load_phrases_dict >>> load_phrases_dict({'了局': [['liǎo'], ['jú']]}) >>> pinyin('了局啊') # 使用 jieba 分词 Building prefix dict from the default dictionary ... Dumping model to file cache /var/folders/s6/z9r_07h53pj_d4x7qjszwmbw0000gn/T/jieba.cache Loading model cost 1.175 seconds. Prefix dict has been built succesfully. [['le'], ['jú'], ['a']] ``` -------------------------------- ### Convert Chinese to Pinyin using pypinyin CLI Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/usage.md Use the `pypinyin` command-line tool to convert Chinese characters to Pinyin. This example shows a basic conversion. ```console $ pypinyin 音乐 yin yuè ``` -------------------------------- ### Standalone Usage of Max-Match Segmentation Source: https://github.com/mozillazg/python-pinyin/blob/master/CHANGELOG.rst Illustrates the standalone usage of the built-in forward max-match segmentation module for text segmentation. This example is from version 0.25.0. ```python >>> from pypinyin.contrib.mmseg import seg >>> text = '你好,我是中国人,我爱我的祖国' >>> seg.cut(text) >>> list(seg.cut(text)) ['你好', ',', '我', '是', '中国人', ',', '我', '爱', '我的', '祖', '国'] >>> seg.train(['祖国', '我是']) >>> list(seg.cut(text)) ['你好', ',', '我是', '中国人', ',', '我', '爱', '我的', '祖国'] >>> ``` -------------------------------- ### Using Custom Phrases Dictionary with Built-in Max-Match Segmentation Source: https://github.com/mozillazg/python-pinyin/blob/master/CHANGELOG.rst Demonstrates using the built-in forward max-match segmentation module with a custom phrases dictionary for pinyin conversion. This example is from version 0.25.0. ```python >>> from pypinyin.contrib.mmseg import seg, retrain >>> retrain(seg) # 没有使用 load_phrases_dict 时可以不调用这个函数 >>> pinyin(seg.cut('了局啊')) # 使用内置的正向最大匹配分词 [['liǎo'], ['jú'], ['a']] ``` -------------------------------- ### Convert Pinyin to different tone styles using toneconvert Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/usage.md Use the `toneconvert` tool to change Pinyin from one tone style to another. This example converts a tone-marked Pinyin string to a numbered tone style. ```default $ python -m pypinyin.tools.toneconvert to-tone 'zhong4 xin1' zhòng xīn ``` -------------------------------- ### Pinyin Accuracy with INITIALS Style Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/faq.md In the `INITIALS` style, characters starting with 'y', 'w', 'yu' may return empty strings because these are not considered initial consonants according to the standard. Consider using `strict=False` if you want 'y' to be treated as an initial. ```python pinyin('火影忍者', style=Style.INITIALS) [['h'], [''], ['r'], ['zh']] ``` -------------------------------- ### Basic Pinyin Conversion Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/usage.md Demonstrates basic pinyin conversion using Style.TONE. ```python from pypinyin import Style, lazy_pinyin lazy_pinyin('乌', style=Style.TONE) ``` ```python lazy_pinyin('迂', style=Style.TONE) ``` -------------------------------- ### Command-line Tool Input from Standard Input Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst Shows how to use the pypinyin command-line tool to process Chinese characters piped from standard input or from a file. ```bash $ echo "你好" | pypinyin nǐ hǎo $ pypinyin < hello.txt nǐ hǎo ``` -------------------------------- ### pypinyin CLI detailed usage and arguments Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/usage.md This displays the full usage information for the `pypinyin` CLI, detailing positional and optional arguments like function, style, separator, error handling, and heteronym support. ```console $ pypinyin -h usage: pypinyin [-h] [-V] [-f {pinyin,slug}] [-s {NORMAL,zhao,TONE,zh4ao,TONE2,zha4o,TONE3,zhao4,INITIALS,zh,FIRST_LETTER,z,FINALS,ao,FINALS_TONE,4ao,FINALS_TONE2,a4o,FINALS_TONE3,ao4,BOPOMOFO,BOPOMOFO_FIRST,CYRILLIC,CYRILLIC_FIRST}] [-p SEPARATOR] [-e {default,ignore,replace}] [-m] hans convert chinese to pinyin. positional arguments: hans chinese string optional arguments: -h, --help show this help message and exit -V, --version show program's version number and exit -f {pinyin,slug}, --func {pinyin,slug} function name (default: "pinyin") -s {NORMAL,zhao,TONE,zh4ao,TONE2,zha4o,TONE3,zhao4,INITIALS,zh,FIRST_LETTER,z,FINALS,ao,FINALS_TONE,4ao,FINALS_TONE2,a4o,FINALS_TONE3,ao4,BOPOMOFO,BOPOMOFO_FIRST,CYRILLIC,CYRILLIC_FIRST}, --style {NORMAL,zhao,TONE,zh4ao,TONE2,zha4o,TONE3,zhao4,INITIALS,zh,FIRST_LETTER,z,FINALS,ao,FINALS_TONE,4ao,FINALS_TONE2,a4o,FINALS_TONE3,ao4,BOPOMOFO,BOPOMOFO_FIRST,CYRILLIC,CYRILLIC_FIRST} pinyin style (default: "zh4ao") -p SEPARATOR, --separator SEPARATOR slug separator (default: "-") -e {default,ignore,replace}, --errors {default,ignore,replace} how to handle none-pinyin string (default: "default") -m, --heteronym enable heteronym ``` -------------------------------- ### Pinyin Conversion with BOPOMOFO Styles Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst Demonstrates the use of BOPOMOFO and BOPOMOFO_FIRST styles for converting Chinese characters to Zhuyin (Bopomofo) symbols. ```python >>> pypinyin.pinyin('中心', style=pypinyin.BOPOMOFO) [['ㄓㄨㄥ'], ['ㄒㄧㄣ']] >>> pypinyin.pinyin('中心', style=pypinyin.BOPOMOFO_FIRST) [['ㄓ'], ['ㄒ']] ``` -------------------------------- ### Display toneconvert tool help information Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/usage.md View the help message for the `toneconvert` tool to see available commands for Pinyin style conversion. ```default $ python -m pypinyin.tools.toneconvert -h usage: toneconvert.py [-h] {to-normal,to-tone,to-tone2,to-tone3} ... options: -h, --help show this help message and exit subcommands: {to-normal,to-tone,to-tone2,to-tone3} to-normal call pypinyin.contrib.tone_convert.to_normal() with inputs to-tone call pypinyin.contrib.tone_convert.to_tone() with inputs to-tone2 call pypinyin.contrib.tone_convert.to_tone2() with inputs to-tone3 call pypinyin.contrib.tone_convert.to_tone3() with inputs ``` -------------------------------- ### Display pypinyin CLI help information Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/usage.md View the help message for the `pypinyin` command-line tool to understand its available arguments and options. ```console $ pypinyin -h ``` -------------------------------- ### Optimize Pinyin with External Dictionaries Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/faq.md Utilize external dictionary files to improve pinyin accuracy. Load data from projects like `pypinyin-dict` for optimized results. ```python # Use pinyin data from cc_cedict.txt in the phrase-pinyin-data project to optimize results from pypinyin_dict.phrase_pinyin_data import cc_cedict cc_cedict.load() # Use pinyin data from cc_cedict.txt in the pinyin-data project to optimize results from pypinyin_dict.pinyin_data import kxhc1983 kxhc1983.load() ``` -------------------------------- ### Register Custom Pinyin Style Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst Demonstrates how to register a custom pinyin style by decorating a function with `@register`. This allows for custom conversion logic for pinyin. ```python from pypinyin.style import register @register('style1') def func(pinyin, **kwargs): # pinyin = xxx # convert to style1 return pinyin def func(pinyin, **kwargs): # pinyin = xxx # convert to style2 return pinyin register('style2', func=func) ``` -------------------------------- ### Convert Pinyin Between Tone Styles Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst The `pypinyin.contrib.tone_convert` module facilitates conversion between `Style.TONE`, `Style.TONE2`, `Style.TONE3`, and `Style.NORMAL`. ```python # Example usage (assuming functions from pypinyin.contrib.tone_convert are imported) # from pypinyin.contrib.tone_convert import to_tone, to_tone2, to_tone3, tone_to_tone2, tone_to_tone3 # to_tone('ni3') -> 'ni3' # to_tone2('ni3') -> 'ni3' # to_tone3('ni3') -> 'ni3' # tone_to_tone2('ni3') -> 'ni3' # tone_to_tone3('ni3') -> 'ni3' ``` -------------------------------- ### Pinyin Conversion with CYRILLIC Styles Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst Shows how to convert Chinese characters to Pinyin using the CYRILLIC and CYRILLIC_FIRST styles, which map Pinyin to Cyrillic characters. ```python >>> pypinyin.pinyin('中心', style=pypinyin.CYRILLIC) [['чжун1'], ['синь1']] >>> pypinyin.pinyin('中心', style=pypinyin.CYRILLIC_FIRST) [['ч'], ['с']] ``` -------------------------------- ### Pinyin Conversion with TONE3 Styles Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst Illustrates the TONE3 and FINALS_TONE3 styles, which represent Pinyin with tone numbers after the syllable or final, respectively. ```python >>> pypinyin.pinyin('中心', style=pypinyin.TONE3) [['zhong1'], ['xin1']] >>> pypinyin.pinyin('中心', style=pypinyin.FINALS_TONE3) [['ong1'], ['in1']] ``` -------------------------------- ### Command Line Pinyin Conversion Source: https://github.com/mozillazg/python-pinyin/blob/master/README_en.rst Converts Chinese characters to pinyin directly from the command line. ```console pypinyin 音乐 ``` -------------------------------- ### Lazy Pinyin with Tone Sandhi Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst Demonstrates the `tone_sandhi` parameter in `lazy_pinyin` which controls tone sandhi rules for pinyin results. ```python from pypinyin import Style, lazy_pinyin print(lazy_pinyin('你好', style=Style.TONE2)) print(lazy_pinyin('你好', style=Style.TONE2, tone_sandhi=True)) ``` -------------------------------- ### Convert Pinyin to Initials and Finals Styles Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst New functions `to_initials` and `to_finals` are added to `pypinyin.contrib.tone_convert` for converting pinyin to `Style.INITIALS` and `Style.FINALS`. ```python from pypinyin import Style from pypinyin.contrib.tone_convert import to_initials, to_finals # Example usage (assuming these functions are available and imported) # to_initials('ni3') -> 'n' # to_finals('ni3') -> 'i' ``` -------------------------------- ### V to U Conversion in Pinyin Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst Demonstrates the `v_to_u` parameter in `lazy_pinyin` for using 'ü' instead of 'v' in pinyin styles without tones. ```python from pypinyin import lazy_pinyin print(lazy_pinyin('战略')) print(lazy_pinyin('战略', v_to_u=True)) ``` -------------------------------- ### Convert Pinyin to FINALS_TONE Styles Source: https://github.com/mozillazg/python-pinyin/blob/master/CHANGELOG.rst Use to_finals_tone, to_finals_tone2, and to_finals_tone3 functions from pypinyin.contrib.tone_convert to convert pinyin to specific final tone styles. ```python from pypinyin.contrib import tone_convert # Example usage (assuming pinyin input) # result_tone = tone_convert.to_finals_tone('ni3') # result_tone2 = tone_convert.to_finals_tone2('ni3') # result_tone3 = tone_convert.to_finals_tone3('ni3') ``` -------------------------------- ### Pinyin Conversion with TONE3 Style and Heteronyms Source: https://github.com/mozillazg/python-pinyin/blob/master/README_en.rst Converts Chinese characters to pinyin using the TONE3 style (numbers at the end of syllables) and includes heteronyms. ```python pinyin('中心', style=Style.TONE3, heteronym=True) ``` -------------------------------- ### Convert Pinyin to Finals with Tones Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst Introduces new functions in `pypinyin.contrib.tone_convert` for converting pinyin to specific finals tone styles. ```python from pypinyin import Style from pypinyin.contrib.tone_convert import to_finals_tone, to_finals_tone2, to_finals_tone3 # Example usage (assuming these functions are available and imported) # to_finals_tone('ni3') -> 'ni' # to_finals_tone2('ni3') -> 'nii' # to_finals_tone3('ni3') -> 'ni3' ``` -------------------------------- ### Pinyin Conversion with TONE2 Style and Heteronyms Source: https://github.com/mozillazg/python-pinyin/blob/master/README_en.rst Converts Chinese characters to pinyin using the TONE2 style (numbers at the end of syllables) and includes heteronyms. ```python pinyin('中心', style=Style.TONE2, heteronym=True) ``` -------------------------------- ### Extracting Syllable Initials (Standard Rules) Source: https://github.com/mozillazg/python-pinyin/blob/master/README_en.rst Extracts only the initial consonant of each pinyin syllable according to standard rules. Characters without an initial will result in an empty string. ```python from pypinyin import Style, pinyin pinyin('下雨天', style=Style.INITIALS) ``` -------------------------------- ### Pinyin Conversion with Heteronyms Source: https://github.com/mozillazg/python-pinyin/blob/master/README_en.rst Converts Chinese characters to pinyin, enabling heteronym mode to capture multiple possible pronunciations. ```python pinyin('中心', heteronym=True) ``` -------------------------------- ### Zhuyin (Bopomofo) Conversion Source: https://github.com/mozillazg/python-pinyin/blob/master/README_en.rst Converts Chinese characters to Zhuyin (Bopomofo) symbols. ```python pinyin('中心', style=Style.BOPOMOFO) ``` -------------------------------- ### Pinyin Styles with Finals Source: https://github.com/mozillazg/python-pinyin/blob/master/CHANGELOG.rst Demonstrates the three new pinyin styles introduced in version 0.3.0 for handling finals: STYLE_FINALS (no tone), STYLE_FINALS_TONE (tone on first letter), and STYLE_FINALS_TONE2 (tone as number suffix). ```python pypinyin.STYLE_FINALS ``` ```python pypinyin.STYLE_FINALS_TONE ``` ```python pypinyin.STYLE_FINALS_TONE2 ``` -------------------------------- ### Convert Pinyin Styles Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/faq.md Use the `tone_convert` module to transform pinyin between different styles, such as converting to normal (no tone marks) or extracting initials and finals. ```python from pypinyin.contrib.tone_convert import to_normal, to_tone, to_initials, to_finals to_normal('zhōng') 'zhong' to_tone('zhong1') 'zhōng' to_initials('zhōng') 'zh' to_finals('zhōng') 'ong' ``` -------------------------------- ### Neutral Tone with Five Indicator Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst Shows the `neutral_tone_with_five` parameter in `lazy_pinyin` for indicating neutral tones with '5' in numbered tone styles. ```python from pypinyin import Style, lazy_pinyin print(lazy_pinyin('衣裳', style=Style.TONE3)) print(lazy_pinyin('衣裳', style=Style.TONE3, neutral_tone_with_five=True)) ``` -------------------------------- ### Customizing Pinyin Phrases for Heteronyms Source: https://github.com/mozillazg/python-pinyin/blob/master/README_en.rst Loads custom phrases to correct or define pinyin for specific character sequences, especially for heteronyms. ```python from pypinyin import Style, pinyin, load_phrases_dict pinyin('步履蹒跚') load_phrases_dict({'步履蹒跚': [['bù'], ['lǚ'], ['pán'], ['shān']]}) pinyin('步履蹒跚') ``` -------------------------------- ### Pinyin Styles: Finals with Tone (Style 1) Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst Demonstrates the STYLE_FINALS_TONE pinyin style, which returns the final part of the pinyin with tone marks indicated on the first letter of the final. This is one of three new pinyin styles. ```python pypinyin.STYLE_FINALS_TONE ``` -------------------------------- ### Handling Errors with a Callback Function Source: https://github.com/mozillazg/python-pinyin/blob/master/CHANGELOG.rst Demonstrates how to handle errors by providing a callback function that extends the result list. This change was introduced in version 0.34.0. ```python >>> pinyin('你好☆☆', errors=lambda x: ['star' for _ in x]) [['nǐ'], ['hǎo'], ['star', 'star']] >>> pinyin('你好☆☆', errors=lambda x: ['star' for _ in x]) [['nǐ'], ['hǎo'], ['star'], ['star']] ``` -------------------------------- ### Extracting Initials with Strict Mode Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/usage.md Shows how the 'strict' parameter affects the extraction of initials. When strict=False, 'w' is treated as an initial for '乌'. ```python lazy_pinyin('乌', style=Style.INITIALS) ``` ```python lazy_pinyin('乌', style=Style.INITIALS, strict=False) ``` -------------------------------- ### Pinyin Styles: Finals with Tone (Style 2) Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst Demonstrates the STYLE_FINALS_TONE2 pinyin style, which returns the final part of the pinyin with tone marks indicated by numbers [0-4] after the final. This is one of three new pinyin styles. ```python pypinyin.STYLE_FINALS_TONE2 ``` -------------------------------- ### Basic Pinyin Conversion Source: https://github.com/mozillazg/python-pinyin/blob/master/README_en.rst Converts Chinese characters to pinyin. For Python 2, use u'中心' instead of '中心'. ```python from pypinyin import pinyin, lazy_pinyin, Style pinyin('中心') ``` -------------------------------- ### Customize Pinyin with Custom Dictionaries Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/faq.md Add custom phrases or single character pinyin mappings to correct or override default results. Use `load_phrases_dict` for multi-character words and `load_single_dict` for individual characters. ```python from pypinyin import load_phrases_dict, load_single_dict load_phrases_dict({'桔子': [['jú'], ['zǐ']]}) # Add "桔子" phrase load_single_dict({ord('还'): 'hái,huán'}) # Adjust pinyin order or override default for "还" ``` -------------------------------- ### Handle Pinyin for Segmented Words Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/faq.md When dealing with text that has already been segmented into words (e.g., by a library like jieba), pass the list of words directly to the pinyin function. ```python # Use other word segmentation modules, like jieba, or segment words using other algorithms based on the phrases_dict.py word data words = list(jieba.cut('每股24.67美元的确定性协议')) pinyin(words) ``` -------------------------------- ### Wade-Giles Pinyin Style Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst Introduces the `Style.WADEGILES` for Wade-Giles romanization without tones. ```python from pypinyin import Style, lazy_pinyin print(lazy_pinyin('威妥玛拼音', style=Style.WADEGILES)) ``` -------------------------------- ### Use '5' for Neutral Tone in Numbered Styles Source: https://github.com/mozillazg/python-pinyin/blob/master/CHANGELOG.rst When neutral_tone_with_five is set to True, numbered tone styles will use '5' to indicate a neutral tone. This applies to the pinyin and lazy_pinyin functions. ```python from pypinyin import lazy_pinyin, Style print(lazy_pinyin('衣裳', style=Style.TONE3)) # Expected: ['yi1', 'shang'] print(lazy_pinyin('衣裳', style=Style.TONE3, neutral_tone_with_five=True)) # Expected: ['yi1', 'shang5'] ``` -------------------------------- ### Extracting Syllable Initials (Non-Strict Mode) Source: https://github.com/mozillazg/python-pinyin/blob/master/README_en.rst Extracts syllable initials, treating 'y' as an initial even when it follows a vowel, deviating from strict pinyin rules. ```python pinyin('下雨天', style=Style.INITIALS, strict=False) ``` -------------------------------- ### Pinyin Conversion with First Letter Style Source: https://github.com/mozillazg/python-pinyin/blob/master/README_en.rst Converts Chinese characters to their first pinyin letter, useful for abbreviations or specific formatting. ```python pinyin('中心', style=Style.FIRST_LETTER) ``` -------------------------------- ### Pinyin Conversion with Mixed Chinese and English Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst Converts a string containing both Chinese characters and English letters to their pinyin representation. This improved support handles mixed content effectively. ```python >> pypinyin.pinyin(u'你好abc') [[u'n\u01d0'], [u'h\u01ceo'], [u'abc']] ``` -------------------------------- ### Extracting Finals with Strict Mode Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/usage.md Illustrates the effect of the 'strict' parameter on extracting finals with tone. When strict=False, 'ü' in '迂' is simplified to 'u'. ```python lazy_pinyin('迂', style=Style.FINALS_TONE) ``` ```python lazy_pinyin('迂', style=Style.FINALS_TONE, strict=False) ``` -------------------------------- ### Pinyin Styles: Finals Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/CHANGELOG.rst Demonstrates the STYLE_FINALS pinyin style, which returns only the final part of the pinyin without tone marks. This is one of three new pinyin styles introduced. ```python pypinyin.STYLE_FINALS ``` -------------------------------- ### Lazy Pinyin Conversion Source: https://github.com/mozillazg/python-pinyin/blob/master/README_en.rst Converts Chinese characters to pinyin without tone information or heteronyms. Uses 'v' for 'ü'. ```python lazy_pinyin('中心') ``` -------------------------------- ### Use 'ü' instead of 'v' in Pinyin Styles Source: https://github.com/mozillazg/python-pinyin/blob/master/CHANGELOG.rst When v_to_u is set to True, pinyin styles without tones will use 'ü' instead of 'v'. This applies to the pinyin and lazy_pinyin functions. ```python from pypinyin import lazy_pinyin print(lazy_pinyin('战略')) # Expected: ['zhan', 'lve'] print(lazy_pinyin('战略', v_to_u=True)) # Expected: ['zhan', 'lüe'] ``` -------------------------------- ### Pinyin Without Initials or Finals Source: https://github.com/mozillazg/python-pinyin/blob/master/docs/faq.md In `strict=True` mode, some pinyin may lack both an initial and a final. This is particularly true for characters like '嗯', '呒', '呣', '唔'. ```default ń ńg ňg ǹg ň ǹ m̄ ḿ m̀ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.