[code snippet]汉字如何转为拼音(ASCII字符)

In [1]: from unidecode import unidecode
In [2]: s = u'我把大门我的妈孙冬梅'
In [3]: s
Out[3]: u'\u6211\u628a\u5927\u95e8\u6211\u7684\u5988\u5b59\u51ac\u6885'
In [4]: unidecode(s)
Out[4]: 'Wo Ba Da Men Wo De Ma Sun Dong Mei '
In [20]: s = '我把大门我的妈孙冬梅'
In [21]: s
Out[21]: '\xe6\x88\x91\xe6\x8a\x8a\xe5\xa4\xa7\xe9\x97\xa8\xe6\x88\x91\xe7\x9a\x84\xe5\xa6\x88\xe5\xad\x99\xe5\x86\xac\xe6\xa2\x85'
In [26]: unidecode(s.decode('utf8'))
Out[26]: 'Wo Ba Da Men Wo De Ma Sun Dong Mei '

需要注意:若s是Unicode类型,就可以直接使用 unidecode(s)。否则需要 decode,一般使用utf8即可。
这是最基本的情况,无法判断一些特殊情况,比如说多音字。

原理:

def _unidecode(string):
    retval = []

    for char in string:
        codepoint = ord(char)

        if codepoint < 0x80: # Basic ASCII
            retval.append(str(char))
            continue
        
        if codepoint > 0xeffff:
            continue # Characters in Private Use Area and above are ignored

        if 0xd800 <= codepoint <= 0xdfff:
            warnings.warn(  "Surrogate character %r will be ignored. "
                            "You might be using a narrow Python build." % (char,),
                            RuntimeWarning, 2)

        section = codepoint >> 8   # Chop off the last two hex digits
        position = codepoint % 256 # Last two hex digits

        try:
            table = Cache[section]
        except KeyError:
            try:
                mod = __import__('unidecode.x%03x'%(section), globals(), locals(), ['data'])
            except ImportError:
                Cache[section] = None
                continue   # No match: ignore this character and carry on.

            Cache[section] = table = mod.data

        if table and len(table) > position:
            retval.append( table[position] )

    return ''.join(retval)

可见,是通过对unicode解码,找到对应位置的ASCII字符。其中,mod = __import__('unidecode.x%03x'%(section), globals(), locals(), ['data']) 即为unidecode包中的各个数据文件,其中包含了对应的ASCII字符元组。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值