Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 声母表
data_for_initials = [
['玻', dict(style=INITIALS), ['b']],
['坡', dict(style=INITIALS), ['p']],
['摸', dict(style=INITIALS), ['m']],
['佛', dict(style=INITIALS), ['f']],
['得', dict(style=INITIALS), ['d']],
['特', dict(style=INITIALS), ['t']],
['讷', dict(style=INITIALS), ['n']],
['勒', dict(style=INITIALS), ['l']],
['哥', dict(style=INITIALS), ['g']],
['科', dict(style=INITIALS), ['k']],
['喝', dict(style=INITIALS), ['h']],
['基', dict(style=INITIALS), ['j']],
['欺', dict(style=INITIALS), ['q']],
['希', dict(style=INITIALS), ['x']],
['知', dict(style=INITIALS), ['zh']],
['蚩', dict(style=INITIALS), ['ch']],
['诗', dict(style=INITIALS), ['sh']],
['日', dict(style=INITIALS), ['r']],
['资', dict(style=INITIALS), ['z']],
['雌', dict(style=INITIALS), ['c']],
['思', dict(style=INITIALS), ['s']],
]
@pytest.mark.parametrize('hans, kwargs, result', data_for_initials)
def test_initials(hans, kwargs, result):
assert lazy_pinyin(hans, **kwargs) == result
assert list(chain(*pinyin(hans, **kwargs))) == result
[dict(style=INITIALS), [['zh'], ['x']]],
[dict(style=INITIALS, strict=False), [['zh'], ['x']]],
[dict(style=FIRST_LETTER), [['z'], ['x']]],
[dict(style=FIRST_LETTER, strict=False), [['z'], ['x']]],
[dict(style=FINALS), [['ong'], ['in']]],
[dict(style=FINALS, strict=False), [['ong'], ['in']]],
[dict(style=FINALS_TONE), [['\u014dng', '\xf2ng'], ['\u012bn']]],
[dict(style=FINALS_TONE, strict=False), [
['\u014dng', '\xf2ng'], ['\u012bn']]],
[dict(style=FINALS_TONE2), [['o1ng', 'o4ng'], ['i1n']]],
[dict(style=FINALS_TONE2, strict=False), [['o1ng', 'o4ng'], ['i1n']]],
[dict(style=FINALS_TONE3), [['ong1', 'ong4'], ['in1']]],
[dict(style=FINALS_TONE3, strict=False), [['ong1', 'ong4'], ['in1']]],
])
def test_heteronym_and_style(kwargs, result):
hans = '中心'
kwargs['heteronym'] = True
from __future__ import unicode_literals
from itertools import chain
import pytest
from pypinyin import (
lazy_pinyin, pinyin, NORMAL, TONE, TONE2, TONE3, INITIALS,
FIRST_LETTER, FINALS, FINALS_TONE, FINALS_TONE2, FINALS_TONE3
)
# test data from http://www.moe.edu.cn/s78/A19/yxs_left/moe_810/s230/195802/t19580201_186000.html # noqa
# 声母表
data_for_initials = [
['玻', dict(style=INITIALS), ['b']],
['坡', dict(style=INITIALS), ['p']],
['摸', dict(style=INITIALS), ['m']],
['佛', dict(style=INITIALS), ['f']],
['得', dict(style=INITIALS), ['d']],
['特', dict(style=INITIALS), ['t']],
['讷', dict(style=INITIALS), ['n']],
['勒', dict(style=INITIALS), ['l']],
['哥', dict(style=INITIALS), ['g']],
['科', dict(style=INITIALS), ['k']],
['喝', dict(style=INITIALS), ['h']],
['基', dict(style=INITIALS), ['j']],
['欺', dict(style=INITIALS), ['q']],
['希', dict(style=INITIALS), ['x']],
['知', dict(style=INITIALS), ['zh']],
['蚩', dict(style=INITIALS), ['ch']],
['诗', dict(style=INITIALS), ['sh']],
assert pinyin(hans) == [['zh\u014dng'], ['x\u012bn']]
assert pinyin(hans, strict=False) == [['zh\u014dng'], ['x\u012bn']]
# 普通风格,不带声调
assert pinyin(hans, NORMAL) == [['zhong'], ['xin']]
assert pinyin(hans, NORMAL, strict=False) == [['zhong'], ['xin']]
# 声调风格,拼音声调在韵母第一个字母上
assert pinyin(hans, TONE) == [['zh\u014dng'], ['x\u012bn']]
assert pinyin(hans, TONE, strict=False) == [['zh\u014dng'], ['x\u012bn']]
# 声调风格2,即拼音声调在各个声母之后,用数字 [1-4] 进行表示
assert pinyin(hans, TONE2) == [['zho1ng'], ['xi1n']]
assert pinyin(hans, TONE2, strict=False) == [['zho1ng'], ['xi1n']]
# 声调风格3,即拼音声调在各个拼音之后,用数字 [1-4] 进行表示
assert pinyin(hans, TONE3) == [['zhong1'], ['xin1']]
assert pinyin(hans, TONE3, strict=False) == [['zhong1'], ['xin1']]
# 声母风格,只返回各个拼音的声母部分
assert pinyin(hans, INITIALS) == [['zh'], ['x']]
assert pinyin(hans, INITIALS, strict=False) == [['zh'], ['x']]
# 首字母风格,只返回拼音的首字母部分
assert pinyin(hans, FIRST_LETTER) == [['z'], ['x']]
assert pinyin(hans, FIRST_LETTER, strict=False) == [['z'], ['x']]
# 注音风格,带声调
assert pinyin(hans, BOPOMOFO) == [['ㄓㄨㄥ'], ['ㄒㄧㄣ']]
assert pinyin(hans, BOPOMOFO, strict=False) == [['ㄓㄨㄥ'], ['ㄒㄧㄣ']]
# 注音风格,首字母
assert pinyin(hans, BOPOMOFO_FIRST) == [['ㄓ'], ['ㄒ']]
assert pinyin(hans, BOPOMOFO_FIRST, strict=False) == [['ㄓ'], ['ㄒ']]
# test CYRILLIC style
assert pinyin(hans, CYRILLIC) == [['чжун1'], ['синь1']]
assert pinyin(hans, CYRILLIC, strict=False) == [['чжун1'], ['синь1']]
# CYRILLIC_FIRST style return only first letters
assert pinyin(hans, CYRILLIC_FIRST) == [['ч'], ['с']]
assert pinyin(hans, CYRILLIC_FIRST, strict=False) == [['ч'], ['с']]
return
# not return the password default
return_password = False
if "return_password" in _request:
return_password = _request["return_password"]
if not return_password:
del _o["user_password"]
_fn = _o.get("user_fullname")
if not isinstance(_fn, unicode):
_fn = _fn.decode("utf-8")
_rdata.update(_o)
_rdata["pinyinname0"] = "".join(lazy_pinyin(_fn))
_rdata["pinyinname1"] = "".join(list(itertools.chain.from_iterable(pinyin(_fn, style=pypinyin.INITIALS))))
return
# not return the password
return_password = False
if "return_password" in _request:
return_password = _request["user_uuid"]
if not return_password:
del _o["user_password"]
_fn = _o.get("user_fullname")
if not isinstance(_fn, unicode):
_fn = _fn.decode("utf-8")
_rdata.update(_o)
_rdata["pinyinname0"] = "".join(lazy_pinyin(_fn))
_rdata["pinyinname1"] = "".join(list(itertools.chain.from_iterable(pinyin(_fn, style=pypinyin.INITIALS))))
return
_updatetime = _o.get("updatetime")
_u = int(time.mktime(_updatetime.timetuple()))
if _u > _timestamp:
_rdata["type"] = YVOBJECT.OG
_rdata["uuid"] = _o.get("uuid")
_rdata["email"] = ""
_rdata["desc"] = _o.get("group_desc")
_rdata["fullname"] = _o.get("group_name")
_fn = _o.get("group_name")
if not isinstance(_fn, unicode):
_fn = _fn.decode("utf-8")
_rdata["pinyinname0"] = "".join(lazy_pinyin(_fn))
_rdata["pinyinname1"] = "".join(list(itertools.chain.from_iterable(pinyin(_fn, style=pypinyin.INITIALS))))
_rdata["icon"] = _o.get("group_icon")
_rdata["timestamp"] = _u
_rdata["updated"] = True
else:
_rdata["updated"] = False
return
def checker_initials(name, pattern):
initials = pypinyin.pinyin(name, style=pypinyin.INITIALS)
return pattern.lower() in u"".join(x[0] for x in initials).lower()
_updatetime = _o["updatetime"]
_u = int(time.mktime(_updatetime.timetuple()))
if _u > _timestamp:
_rdata["type"] = YVOBJECT.AU
_rdata["uuid"] = _o["user_uuid"]
_rdata["email"] = _o["user_email"]
_rdata["desc"] = None
_rdata["fullname"] = _o["user_fullname"]
_fn = _o["user_fullname"]
if not isinstance(_fn, unicode):
_fn = _fn.encode("utf-8")
_rdata["pinyinname0"] = "".join(lazy_pinyin(_fn))
_rdata["pinyinname1"] = "".join(list(itertools.chain.from_iterable(pinyin(_fn, style=pypinyin.INITIALS))))
_rdata["icon"] = _o["user_icon"]
_rdata["timestamp"] = _u
_rdata["updated"] = True
else:
_rdata["updated"] = False
return