• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mozillazg / python-pinyin / 502

pending completion
502

push

travis-ci

mozillazg
使用 `phrase-pinyin-data`_ v0.7.2 的词语拼音数据

* Fixes #112
* Fixes #117
* Fixes #122
* Fixes #131

275 of 527 relevant lines covered (52.18%)

0.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

24.24
/pypinyin/style/_utils.py
1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
3

4
from pypinyin.standard import convert_finals
5
from pypinyin.style._constants import (
6
    _INITIALS, _INITIALS_NOT_STRICT,
7
    RE_PHONETIC_SYMBOL, PHONETIC_SYMBOL_DICT,
8
    RE_NUMBER
9
)
10

11

12
def get_initials(pinyin, strict):
13
    """获取单个拼音中的声母.
14

15
    :param pinyin: 单个拼音
16
    :type pinyin: unicode
17
    :param strict: 是否严格遵照《汉语拼音方案》来处理声母和韵母
18
    :return: 声母
19
    :rtype: unicode
20
    """
21
    if strict:
22
        _initials = _INITIALS
23
    else:
24
        _initials = _INITIALS_NOT_STRICT
25

26
    for i in _initials:
27
        if pinyin.startswith(i):
28
            return i
29
    return ''
30

31

32
def get_finals(pinyin, strict):
33
    """获取单个拼音中的韵母.
34

35
    :param pinyin: 单个拼音
36
    :type pinyin: unicode
37
    :param strict: 是否严格遵照《汉语拼音方案》来处理声母和韵母
38
    :return: 韵母
39
    :rtype: unicode
40
    """
41
    if strict:
42
        pinyin = convert_finals(pinyin)
43

44
    initials = get_initials(pinyin, strict=strict) or ''
45
    # 没有声母,整个都是韵母
46
    if not initials:
47
        return pinyin
48
    # 按声母分割,剩下的就是韵母
49
    return ''.join(pinyin.split(initials, 1))
50

51

52
def replace_symbol_to_number(pinyin):
53
    """把声调替换为数字"""
54
    def _replace(match):
55
        symbol = match.group(0)  # 带声调的字符
56
        # 返回使用数字标识声调的字符
57
        return PHONETIC_SYMBOL_DICT[symbol]
58

59
    # 替换拼音中的带声调字符
60
    return RE_PHONETIC_SYMBOL.sub(_replace, pinyin)
61

62

63
def replace_symbol_to_no_symbol(pinyin):
64
    """把带声调字符替换为没有声调的字符"""
65
    def _replace(match):
66
        symbol = match.group(0)  # 带声调的字符
67
        # 去掉声调: a1 -> a
68
        return RE_NUMBER.sub(r'', PHONETIC_SYMBOL_DICT[symbol])
69

70
    # 替换拼音中的带声调字符
71
    return RE_PHONETIC_SYMBOL.sub(_replace, pinyin)
72

73

74
def has_finals(pinyin):
75
    """判断是否有韵母"""
76
    # 鼻音: 'ḿ', 'ń', 'ň', 'ǹ ' 没有韵母
77
    for symbol in ['\u1e3f', '\u0144', '\u0148', '\u01f9']:
78
        if symbol in pinyin:
79
            return False
80

81
    return True
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2024 Coveralls, Inc