【问题标题】:How to check the Emoji property of a character in Python?如何在 Python 中检查字符的 Emoji 属性?
【发布时间】:2017-07-05 11:52:59
【问题描述】:

在 unicode 中,一个字符可以有一个Emoji property

Python 中是否有标准方法来确定字符是否为表情符号?

我知道unicodedata,但它似乎并没有暴露所有这些额外的角色细节。

注意:我询问的是 unicdoe 标准中名为“Emoji”的特定属性,如链接中提供的那样。我不想有任意的模式范围列表,最好使用标准库。

【问题讨论】:

  • @kabanus 不是重复的。其他问题将随机字符列表指定为表情符号,我问的是 Unicode 标准特别标记为表情符号的字符。

标签: python python-3.x unicode


【解决方案1】:

这是我最终创建的用于加载表情符号信息的代码。 get_emoji 函数获取数据文件,对其进行解析,然后调用枚举回调。其余代码使用它来生成我需要的信息的 JSON 文件。

#!/usr/bin/env python3
# Generates a list of emoji characters and names in JS format
import urllib.request
import unicodedata
import re, json

'''
Enumerates the Emoji characters that match an attributes from the Unicode standard (the Emoji list).

@param on_emoji A callback that is called with each found character. Signature `on_emoji( code_point_value )`
@param attribute  The attribute that  is desired, such as `Emoji` or `Emoji_Presentation`
'''
def get_emoji(on_emoji, attribute):
    with urllib.request.urlopen('http://www.unicode.org/Public/emoji/5.0/emoji-data.txt') as f:
        content = f.read().decode(f.headers.get_content_charset())

        cldr = re.compile('^([0-9A-F]+)(..([0-9A-F]+))?([^;]*);([^#]*)#(.*)$')
        for line in content.splitlines():
            m = cldr.match(line)
            if m == None:
                continue

            line_attribute = m.group(5).strip()
            if line_attribute != attribute:
                continue

            code_point = int(m.group(1),16)
            if m.group(3) == None:
                on_emoji(code_point)
            else:
                to_code_point = int(m.group(3),16)
                for i in range(code_point,to_code_point+1):
                    on_emoji(i)


# Dumps the values into a JSON format
def print_emoji(value):
    c = chr(value)
    try:
        obj = {
            'code': value,
            'name': unicodedata.name(c).lower(),
        }
        print(json.dumps(obj),',')
    except:
        # Unicode DB is likely outdated in installed Python
        pass

print( "module.exports = [" )
get_emoji(print_emoji, "Emoji_Presentation")
print( "]" )

这解决了我原来的问题。要回答问题本身,只需将结果放入字典并进行查找即可。

【讨论】:

    【解决方案2】:

    我之前成功使用过以下正则表达式模式

    import re
    
    emoji_pattern = re.compile("["
                                   u"\U0001F600-\U0001F64F"  # emoticons
                                   u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                                   u"\U0001F680-\U0001F6FF"  # transport & map symbols
                                   u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                                   "]+", flags=re.UNICODE)
    

    也可以看看这个问题:removing emojis from a string in Python

    【讨论】:

    • 这些范围不是包含在标准 Unicode 表情符号数据列表中的范围。
    • @edA-qamort-ora-y 好吧,我仍然会做同样的事情,但只是将其扩展为包括整个范围。
    猜你喜欢
    • 2015-09-25
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2021-10-06
    • 2015-08-15
    • 1970-01-01
    • 2022-01-24
    • 2017-01-09
    相关资源
    最近更新 更多