在字符串中计算韩语字符

时间:2018-05-20 12:24:18

标签: python python-3.x unicode

我希望能够计算输入中有多少韩文字母,就像我使用英文字母和数字一样:

a=0
b=0
c=0
d=0
e=0

num = input("type something ")

for i in num:
    if(i.isupper()):
        a=a+1
    elif(i.islower()):
        b=b+1
    elif(i.isdigit()):
        c=c+1


print("uppercase letters: ",a)
print("lowercase letters: ",b)
print("numbers: ",c)
print("korean letters: ",d)

但我不知道应该怎么做,我是否必须以某种方式合并ord()

1 个答案:

答案 0 :(得分:3)

如果您询问如何计算Hangul codepoints,则需要获取该字符的ord()值,并查看其是否在任何记录范围内:

hangul_ranges = (
    range(0xAC00, 0xD7A4),  # Hangul Syllables (AC00–D7A3)
    range(0x1100, 0x1200),  # Hangul Jamo (1100–11FF)
    range(0x3130, 0x3190),  # Hangul Compatibility Jamo (3130-318F)
    range(0xA960, 0xA980),  # Hangul Jamo Extended-A (A960-A97F)
    range(0xD7B0, 0xD800),  # Hangul Jamo Extended-B (D7B0-D7FF)
)
is_hangul = lambda c: any(ord(c) in r for r in hangul_ranges)

然后使用is_hangul(i)

实际上,没有朝鲜语字符被认为是大写,小写或数字,但考虑到这三个类别不仅仅包含英语; Unicode 8.0标准有: