我明白那个unicodedata.normalize
将变音符号转换为非变音符号:
import unicodedata
''.join( c for c in unicodedata.normalize('NFD', u'B\u0153uf')
if unicodedata.category(c) != 'Mn'
)
我的问题是(可以在这个例子中看到): unicodedata 是否有办法将组合的 char 变音符号替换为对应的变音符号? (u'œ' 变为 'oe')
如果不是,我想我将不得不对这些进行打击,但我也可能会用所有 uchar 及其对应项编译我自己的字典,然后忘记unicodedata
共...
您的问题中的术语有些混乱。 A变音符号 http://en.wikipedia.org/wiki/Diacritic是可以添加到字母或其他字符的标记,但通常不独立。 (Unicode 还使用更通用的术语组合字符.) What normalize('NFD', ...)
所做的是转换预制字符 http://en.wikipedia.org/wiki/Precomposed_character到他们的组件中。
不管怎样,答案是 – 不是一个预组合字符。它是印刷连字 http://en.wikipedia.org/wiki/Typographic_ligature:
>>> unicodedata.name(u'\u0153')
'LATIN SMALL LIGATURE OE'
The unicodedata
模块没有提供将连字分割成各个部分的方法。但数据存在于角色名称中:
import re
import unicodedata
_ligature_re = re.compile(r'LATIN (?:(CAPITAL)|SMALL) LIGATURE ([A-Z]{2,})')
def split_ligatures(s):
"""
Split the ligatures in `s` into their component letters.
"""
def untie(l):
m = _ligature_re.match(unicodedata.name(l))
if not m: return l
elif m.group(1): return m.group(2)
else: return m.group(2).lower()
return ''.join(untie(l) for l in s)
>>> split_ligatures(u'B\u0153uf \u0132sselmeer \uFB00otogra\uFB00')
u'Boeuf IJsselmeer ffotograff'
(当然,在实践中您不会这样做:您将按照您在问题中建议的方式预处理 Unicode 数据库以生成查找表。Unicode 中没有那么多连字。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)