Giter Site home page Giter Site logo

cipherchen / python-pinyin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mozillazg/python-pinyin

0.0 1.0 0.0 3.21 MB

汉语拼音转换工具 Python 版。

Home Page: http://pypinyin.rtfd.org/

License: MIT License

Makefile 0.17% Python 99.67% Shell 0.15%

python-pinyin's Introduction

汉语拼音转换工具(Python 版)

Build Coverage PyPI version PyPI downloads Python 3

将汉语转为拼音。可以用于汉字注音、排序、检索。

基于 hotoo/pinyin 开发。

特性

  • 根据词组智能匹配最正确的拼音。
  • 支持多音字。
  • 简单的繁体支持。
  • 支持多种不同拼音风格。

安装

$ pip install pypinyin

为了更好的处理包含多音字及非中文字符的字符串, 推荐同时安装 jieba 分词模块。

使用示例

>>> from pypinyin import pinyin, lazy_pinyin
>>> import pypinyin
>>> pinyin(u'中心')
[[u'zh\u014dng'], [u'x\u012bn']]
>>> pinyin(u'中心', heteronym=True)  # 启用多音字模式
[[u'zh\u014dng', u'zh\xf2ng'], [u'x\u012bn']]
>>> pinyin(u'中心', style=pypinyin.INITIALS)  # 设置拼音风格
[['zh'], ['x']]
>>> lazy_pinyin(u'中心')
['zhong', 'xin']

命令行工具:

$ pypinyin 音乐
yīn yuè
$ pypinyin -h

分词处理

如果安装了 jieba 分词模块,程序会自动调用。

使用其他分词模块:

  1. 安装分词模块,比如 pip install snownlp

  2. 使用经过分词处理的字符串列表作参数:

    >> from pypinyin import lazy_pinyin, TONE2
    >> from snownlp import SnowNLP
    >> hans = u'音乐123'
    >> lazy_pinyin(hans, style=TONE2)
    [u'yi1n', u'le4', u'1', u'2', u'3']
    >> hans_seg = SnowNLP(hans).words  # 分词处理
    >> hans_seg
    [u'\u97f3\u4e50', u'123']
    >> lazy_pinyin(hans_seg, style=TONE2)
    [u'yi1n', u'yue4', u'123']

自定义拼音库

如果对结果不满意,可以通过自定义拼音库的方式修正结果:

>> from pypinyin import lazy_pinyin, load_phrases_dict, TONE2
>> hans = u'桔子'
>> lazy_pinyin(hans, style=TONE2)
[u'jie2', u'zi3']
>> load_phrases_dict({u'桔子': [[u'jú'], [u'zǐ']]})
>> lazy_pinyin(hans, style=TONE2)
[u'ju2', u'zi3']

Related Projects

python-pinyin's People

Contributors

mozillazg avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.