当前位置: 首页>>代码示例>>Python>>正文


Python pypinyin.pinyin函数代码示例

本文整理汇总了Python中pypinyin.pinyin函数的典型用法代码示例。如果您正苦于以下问题:Python pinyin函数的具体用法?Python pinyin怎么用?Python pinyin使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了pinyin函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: prefixs_for_term

  def prefixs_for_term (self,term):
    """
    Get prefixs for TERM.
    """
    # Normalization
    term=term.lower()

    # Prefixs for term
    prefixs=[]
    for i in xrange(1, len(term) + 1):
      word = term[:i]
      prefixs.append(word)
      prefixs.append(''.join([i[0] for i in pinyin(word, style=pypinyin.FIRST_LETTER)]).lower())
      prefixs.append(''.join([i[0] for i in pinyin(word, style=pypinyin.NORMAL)]).lower())
      prefixs.append(word)

    tokens = self.normalize(term)
    for token in tokens:
      for i in xrange (1,len(token)+1):
        word = token[:i]
        prefixs.append(word)
        prefixs.append(''.join([i[0] for i in pinyin(word, style=pypinyin.FIRST_LETTER)]).lower())
        prefixs.append(''.join([i[0] for i in pinyin(word, style=pypinyin.NORMAL)]).lower())
        prefixs.append(word)

    return list(set(prefixs))
开发者ID:ultimate010,项目名称:autocomplete-redis,代码行数:26,代码来源:index.py

示例2: get_pinyin

 def get_pinyin(self):
     su_temp = pinyin(self.names[0], style=pypinyin.NORMAL,heteronym=True)
     fn_temp = pinyin(self.names[1], style=pypinyin.NORMAL, heteronym=True)
     su_py = self.combination(self, su_temp, '')
     fn_py = self.combination(self, fn_temp, '')
     pys = self.combination(self, [su_py, fn_py], ' ')
     return pys
开发者ID:linkseed18612254945,项目名称:NameTranslator,代码行数:7,代码来源:DataBase.py

示例3: test_zh_and_en

def test_zh_and_en():
    """中英文混合的情况"""
    # 中英文
    hans = '中心'
    try:
        assert pinyin(hans + 'abc') == [['zh\u014dng'], ['x\u012bn'], ['abc']]
    except AssertionError:
        assert pinyin(hans + 'abc') == [['zh\u014dng'], ['x\u012bn'],
                                        ['a'], ['b'], ['c']]
开发者ID:alexhe,项目名称:python-pinyin,代码行数:9,代码来源:test_pinyin.py

示例4: test_errors_callable

def test_errors_callable():
    def foobar(chars):
        return 'a' * len(chars)

    class Foobar(object):
        def __call__(self, chars):
            return 'a' * len(chars)

    n = 5
    assert pinyin('あ' * n, errors=foobar) == [['a' * n]]
    assert pinyin('あ' * n, errors=Foobar()) == [['a' * n]]
开发者ID:mozillazg,项目名称:python-pinyin,代码行数:11,代码来源:test_pinyin.py

示例5: test_others

def test_others():
    # 空字符串
    assert pinyin('') == []
    # 单个汉字
    assert pinyin('營') == [['y\xedng']]
    # 中国 人
    assert pinyin('中国人') == [['zh\u014dng'], ['gu\xf3'], ['r\xe9n']]
    # 日文
    assert pinyin('の') == [['\u306e']]
    # 没有读音的汉字,还不存在的汉字
    assert pinyin('\u9fff') == [['\u9fff']]
开发者ID:zhaochl,项目名称:python-pinyin,代码行数:11,代码来源:test_pinyin.py

示例6: get_homophones_by_char

def get_homophones_by_char(input_char):
    """
    根据汉字取同音字
    :param input_char:
    :return:
    """
    result = []
    # CJK统一汉字区的范围是0x4E00-0x9FA5,也就是我们经常提到的20902个汉字
    for i in range(0x4e00, 0x9fa6):
        if pinyin([chr(i)], style=pypinyin.NORMAL)[0][0] == pinyin(input_char, style=pypinyin.NORMAL)[0][0]:
            result.append(chr(i))
    return result
开发者ID:djk111,项目名称:pycorrector,代码行数:12,代码来源:text_utils.py

示例7: test_custom_style_with_decorator

def test_custom_style_with_decorator():
    style_value = 'test_custom_style_with_decorator'

    @register(style_value)
    def func(pinyin, **kwargs):
        return pinyin + str(len(pinyin))

    hans = '北京'
    origin_pinyin_s = pinyin(hans)
    expected_pinyin_s = deepcopy(origin_pinyin_s)
    for pinyin_s in expected_pinyin_s:
        for index, py in enumerate(pinyin_s):
            pinyin_s[index] = func(py)

    assert pinyin(hans, style=style_value) == expected_pinyin_s
开发者ID:marcos0318,项目名称:python-pinyin,代码行数:15,代码来源:test_style.py

示例8: lang_zh

def lang_zh(text):
    res = []
    for line in text.split('\n'):
        cut = jieba.cut(line)
        ln = [[i, "'".join(j[0] for j in pypinyin.pinyin(i, style=0))] for i in cut]
        res.append(ln)
    return res
开发者ID:blueset,项目名称:project-lyricova,代码行数:7,代码来源:tr.py

示例9: get_following_users

 def get_following_users(self, user):
     doc = yield self._db.followers.find_one({"user": user}, {"_id":0, "following":1})
     if doc and "following" in doc:
         ret = yield [self.find_user(_, True) for _ in doc["following"] if _]
     else:
         ret = []
     raise gen.Return(sorted(ret, key=lambda x: pinyin(to_unicode(("real_name" in x and x["real_name"]) or ""), style=TONE2)))
开发者ID:BoneLee,项目名称:FBT,代码行数:7,代码来源:users_manager.py

示例10: _du

    def _du(self, _request, _rdata):
        if "user_uuid" not in _request:
            self.setErrorCode(API_ERR.NO_PARA)
            logging.error("Error for no para: %s.", (str(_request)))
            return

        _o = redis_hash_to_dict(self.application.redis, DeviceUser, _request["user_uuid"])

        logging.info(_o)
        
        if _o == None:
            self.setErrorCode(API_ERR.NO_OBJECT)
            logging.error("Error for no user uuid: %s." % (_request["user_uuid"]))
            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 _fn != None and 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
开发者ID:Michael2008S,项目名称:ppmessage,代码行数:31,代码来源:ppgetuserdetailhandler.py

示例11: _get_pinyin_all

def _get_pinyin_all(existing_combinations, characters):
    """
    Get all combinations of pinyin of some chinese characters as list, in a 
    recurrence way, since format of result from pinyin is [['a'], ['b']]
    So a combination of two level loop is needed to get all the pinyin. 
    :param existing_combinations:  Existing combinations, for already calculated characters. 
    :param characters: Characters to get combination of pinyin 
    :return:  A flat list of all combinations of pinyin for 多音字
    """
    first_character, other_characters = characters[0:1], characters[1:]
    if len(first_character) > 0:
        py = pinyin(first_character, style=pypinyin.FIRST_LETTER, heteronym=True)
        new_existing = []
        for p in py:
            for a in p:
                if len(existing_combinations) > 0:
                    for e in existing_combinations:
                        ne = e[:]
                        ne.append(a)
                        new_existing.append(ne)
                else:
                    ne = existing_combinations[:]
                    ne.append(a)
                    new_existing.append(ne)
        return _get_pinyin_all(new_existing, other_characters)
    return existing_combinations
开发者ID:betterlife,项目名称:psi,代码行数:26,代码来源:format_util.py

示例12: test_errors

def test_errors():
    hans = (
        ('啊', {'style': TONE2}, [['a']]),
        ('啊a', {'style': TONE2}, [['a'], ['a']]),
        # 非中文字符,没有拼音
        ('⺁', {'style': TONE2}, [['\u2e81']]),
        ('⺁', {'style': TONE2, 'errors': 'ignore'}, []),
        ('⺁', {'style': TONE2, 'errors': 'replace'}, [['2e81']]),
        ('⺁⺁', {'style': TONE2, 'errors': 'replace'}, [['2e812e81']]),
        ('⺁⺁', {'style': TONE2, 'errors': lambda x: ['a' for _ in x]},
         [['a'], ['a']]),
        ('⺁⺁', {'style': TONE2, 'errors': lambda x: [['a', 'b'], ['b', 'c']]},
         [['a'], ['b']]),
        ('⺁⺁', {'style': TONE2, 'heteronym': True,
                'errors': lambda x: [['a', 'b'], ['b', 'c']]},
         [['a', 'b'], ['b', 'c']]),
        # 中文字符,没有拼音
        ('鿅', {'style': TONE2}, [['\u9fc5']]),
        ('鿅', {'style': TONE2, 'errors': 'ignore'}, []),
        ('鿅', {'style': TONE2, 'errors': '233'}, []),
        ('鿅', {'style': TONE2, 'errors': 'replace'}, [['9fc5']]),
        ('鿅', {'style': TONE2, 'errors': lambda x: ['a']}, [['a']]),
        ('鿅', {'style': TONE2, 'errors': lambda x: None}, []),
        ('鿅鿅', {'style': TONE2, 'errors': lambda x: ['a' for _ in x]},
         [['a'], ['a']]),
        ('鿅鿅', {'style': TONE2, 'errors': lambda x: [['a', 'b']]},
         [['a'], ['a']]),
        ('鿅鿅', {'style': TONE2, 'heteronym': True,
                'errors': lambda x: [['a', 'b']]},
         [['a', 'b'], ['a', 'b']]),
    )
    for han in hans:
        assert pinyin(han[0], **han[1]) == han[2]
开发者ID:mozillazg,项目名称:python-pinyin,代码行数:33,代码来源:test_pinyin.py

示例13: add_term

def add_term(term, weight):
    words, types = term2words(term)
    if len(words) == 0: #avoid '......'
        return
    #max prefix match
    level, node_id = max_prefix_match(words, types)
    
    # 如果全部存在这个字符序列,则更新 node_id
    if level == len(words):#exist already
        add_weight(node_id, weight)#may lead to parent weight bigger than weight sum of all children
    else:
        for word in words[level:]:
            #insert normal node
            parent = node_id
            node_id = new_node(word, parent)
            if len(word)==1 and ord(word)>=19904 and ord(word)<=40895:
                #insert pinyin node
                pys = pypinyin.pinyin(word, style=pypinyin.NORMAL, heteronym=True)
                for py in pys[0]:
                    #complete pinyin
                    push_pinyin_node(parent, node_id, py)
                    push_pinyin_node(parent, node_id, py[0])
                    if py[0]=='c' or py[0]=='s' or py[0]=='z':
                        if py[1] == 'h':
                            push_pinyin_node(parent, node_id, py[:2])
                
        add_weight(node_id, weight)
开发者ID:zhaochl,项目名称:python-utils,代码行数:27,代码来源:tire_tree.py

示例14: _du

    def _du(self):

        _request = json.loads(self.request.body)

        _user_uuid = _request.get("user_uuid")
        if not _user_uuid:
            self.setErrorCode(API_ERR.NO_PARA)
            return

        _o = redis_hash_to_dict(self.application.redis, DeviceUser, _user_uuid)
        if not _o:
            self.setErrorCode(API_ERR.NO_OBJECT)
            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 _fn != None and not isinstance(_fn, unicode):
            _fn = _fn.decode("utf-8")

        _rdata = self.getReturnData()
        _rdata.update(_o)
        _rdata["pinyinname0"] = "".join(lazy_pinyin(_fn))
        _rdata["pinyinname1"] = "".join(list(itertools.chain.from_iterable(pinyin(_fn, style=pypinyin.INITIALS))))

        _app_uuid = _get_config().get("team").get("app_uuid")
        _o = redis_hash_to_dict(self.application.redis, AppInfo, _app_uuid)
        _rdata.update({"team": _o});
        return
开发者ID:anxiaoyi,项目名称:ppmessage,代码行数:34,代码来源:ppgetuserdetailhandler.py

示例15: addPinyin

def addPinyin(sometext):
    mylist=pinyin(sometext, heteronym=True)
    str=u''
    for pp in mylist:
        str+=pp[0]+u' '
    print str.rstrip()
    return str.rstrip()
开发者ID:jjp9624022,项目名称:pinyin4indesign,代码行数:7,代码来源:pinyin.py


注:本文中的pypinyin.pinyin函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。