我正在寻找一种简单的方法来匹配所有看起来像给定字母的 unicode 字符。考虑一个选择看起来像小写字母
n
的字符的示例。
import re
import sys
sys.stdout.reconfigure(encoding='utf-8')
look_alike_chars = [ 'n', 'ń', 'ņ', 'ň', 'ʼn', 'ŋ', 'ո', 'п', 'ո']
pattern = re.compile(r'[nńņňʼnŋոп]')
for char in look_alike_chars:
if pattern.match(char):
print(f"Character '{char}' matches the pattern.")
else:
print(f"Character '{char}' does NOT match the pattern.")
而不是
r'[nńņňʼnŋոп]'
我期望类似 r'[\LookLike{n}]
的东西,其中 LookLike
是一个令牌。
如果这是不可能的,您是否知道有一个程序或网站可以列出给定 ASCII 字母的所有相似符号?
有一个名为 Unidecode 的 python 库,它会将其中一些特殊的 Unicode 字符转换为其相应的 ascii 字符。代码示例应该是这样的:
import unidecode
input_string = "ñ"
clean_string = unidecode.unidecode(input_string) # = 'n'
但是,对应关系是根据其实际的成语等效项进行的。
因此,每当您想要翻译大写希腊语“PI”(写为 п)时,它都会返回“p”,因为 pi 相当于“p”而不是“n”。 此外,没有此类信件的公开数据库。请随意构建一个并分享它!