Skip to main content

g2pC: A Context-aware Grapheme-to-Phoneme for Chinese

There are several open source libraries of Chinese grapheme-to-phoneme conversion such as python-pinyin or xpinyin. However, none of them seem to disambiguate Chinese polyphonic words like "行" ("xíng" (go, walk) vs. "háng" (line)) or "了" ("le" (completed action marker) vs. "liǎo" (finish, achieve)). Instead, they pick up the most frequent pronunciation. Although that may be a simple and economic strategy, machine learning techniques can be of help. We use CRF to determine the pronunciation of polyphonic words. In addition to the target word itself and its part-of-speech, which are tagged by pkuseg, its neighboring words are also featurized.

Requirements

  • python >= 3.6
  • pkuseg
  • sklearn_crfsuite

Installation

pip install g2pc

Main Features

  • Disambiguate polyphonic Chinese characters/words and return the most likely pinyin in the context using CRF implemented with sklearn_crfsuite.
  • By associating segmentation results provided by pkuseg with an open-source dictionary CC-CEDICT, display the following comprehensive information.
    • word
    • part-of-speech
    • pinyin
    • English meaning
    • traditional equivalent

Algorithm (illustrated with an example)

e.g., Input: 我写了几行代码。 (I wrote a few lines of codes.)

  • STEP 1. Segment input string using pkuseg.
    • -> [('我', 'r'), ('写', 'v'), ('了', 'u'), ('几', 'm'), ('行', 'q'), ('代码', 'n'), ('。', 'w')]
  • STEP 2. Look up the CC-CEDICT. Each token, a tuple, consists of word, pos, pronunciation candidates, meaning candidates, traditional character candidates.
    • -> [('我', 'r', ['wo3'], ['/I/me/my/'], ['我']),
      ('写', 'v', ['xie3'], ['/to write/'], ['寫']),
      ('了', 'u', ['le5', 'liao3', 'liao4'], [dal particle ..], ['了', '了', '瞭']),
      ('几', 'm', ['ji3', 'ji1'], ['/how much/..'], ['幾', '几']),
      ('行', 'q', ['xing2', 'hang2'], ['/to walk/.."], ['行', '行']),
      ('代码', 'n', ['dai4 ma3'], ['/code/'], ['代碼']),
      ('。', 'w', ['。'], [''], ['。'])]
  • STEP 3. For polyphonic words, we disambiguate them, using our pre-trained CRF model.
    • -> [('我', 'r', 'wo3', '/I/me/my/', '我'),
      ('写', 'v', 'xie3', '/to write/', '寫'),
      ('了', 'u', 'le5', '/(modal particle ..', '了'),
      ('几', 'm', 'ji3', '/how much/..', '幾'),
      ('行', 'q', 'hang2', "/row/..", '行'),
      ('代码', 'n', 'dai4 ma3', '/code/', '代碼'),
      ('。', 'w', '。', '', '。')]

Usage

>>> from g2pc import G2pC
>>> g2p = G2pC()
>>> g2p("来不了")
# This returns a list of tuples, each of which consists of
# word, pos, pinyin, English meaning, and equivanlent traditional character.
[('来', 'v', 'lai2', '/to come/to arrive/to come round/ever since/next/', '來'), 
('不', 'd', 'bu4', '/(negative prefix)/not/no/', '不'), 
('了', 'v', 'liao3', '/to finish/to achieve/variant of 瞭|了[liao3]/to understand clearly/', '了')]

Respectful comparison with other libraries

>>> text1 = "我写了几行代码。" # pay attention to the 行, which should be read as 'hang2', not 'xing2'
>>> text2 = "来不了" # pay attention to the 了, which should be read as 'liao3', not 'le'

# python-pinyin
>>> pip install pypinyin
>>> from pypinyin import pinyin
>>> pinyin(text1)
[['wǒ'], ['xiě'], ['le'], ['jǐ'], ['xíng'], ['dài'], ['mǎ'], ['。']]
>>> pinyin(text2)
[['lái'], ['bù'], ['le']]

# xpinyin
>>> pip install xpinyin
>>> from xpinyin import Pinyin
>>> p = Pinyin()
>>> p.get_pinyin(text1, tone_marks="numbers")  
'wo3-xie3-le5-ji1-xing2-dai4-ma3-。'
>>> p.get_pinyin(text2, tone_marks="numbers")   
'lai2-bu4-le5'

References

If you use our software for research, please cite:

@misc{gp2C2019,
  author = {Park, Kyubyong},
  title = {g2pC},
  year = {2019},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/Kyubyong/g2pC}}
}

Metadata

Release files for g2pC 0.9.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for g2pC 0.9.4
File Size Uploaded Provenance Actions
g2pC-0.9.4.tar.gz 6.3 MB Without provenance Details for g2pC-0.9.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for g2pC 0.9.4
File Interpreter ABI Platform Actions
g2pC-0.9.4-py3-none-any.whl Python 3 none any Details for g2pC-0.9.4-py3-none-any.whl

Total release size: 12.7 MB

Release files / g2pC-0.9.4.tar.gz

Download URL g2pC-0.9.4.tar.gz
Size 6.3 MB
Tags Source
SHA-256 checksum
How to use checksums
7aa49ecdf49b04c703ca08b0dc28d7995bd16cdffc76e42c3c04c9f33b9af0db
BLAKE2b-256 checksum
How to use checksums
60982ba9099d21c05f1fb898a6115c580131b8be2fb24cd88badb5f32baa3f4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

Release files / g2pC-0.9.4-py3-none-any.whl

Download URL g2pC-0.9.4-py3-none-any.whl
Size 6.3 MB
Tags Python 3
SHA-256 checksum
How to use checksums
d22628c33a268c9be67b2862aaeee5387bb50bbf740706c6188fc418fac2c83a
BLAKE2b-256 checksum
How to use checksums
f5efd4cb12734c4a76192294d2894efc3cffd882a53356c60d364b6f741fe7f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

Security for g2pC 0.9.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page