【问题标题】:How to find unicode planes for emojis in Python如何在 Python 中为表情符号查找 unicode 平面
【发布时间】:2021-03-06 11:29:51
【问题描述】:

我有包含表情符号的 pandas 数据框,我想根据它们的 Unicode Planes 对其进行分类。

emoji | unicode
---------------
 ????   |  1F602
 ????   |  1F60A

预期输出

emoji | unicode | Plane
-----------------------
 ????   |  1F602  |   1    
 ????   |  1F60A  |   1
 ⛹   |  26F9   |   0

这里的平面 0 是指基本多语言平面 (BMP),平面 1 是指补充多语言平面 (SMP)。

[注意:请在 Mac 上使用 Safari,在 Linux 上使用 Firefox,在 Windows 上使用 Chrome 来查看带有正确表情符号的问题]

【问题讨论】:

  • 注意:表情符号由一个或多个代码点创建(如许多字形/“字母”)。旗帜(和其他表情符号)需要平面 0xE。您可以在unicode.org/reports/tr51/tr51-18.html 中找到更多信息
  • @GiacomoCatenazzi 谢谢。是的,但是有一些表情符号属于平面 0。所以基本上我想找到表情符号在所有平面上的分布。

标签: python-3.x pandas dataframe unicode emoji


【解决方案1】:

?? 都属于Plane 1, the Supplementary Multilingual Plane (SMP)

下面的代码 sn-p 可以举例说明获取Unicode平面#的算法(它是ord(ch)>>16,见bitwise right shift)。

for ch in '✌⛹☹☺☻??':
    print( ch, '\t{:04x}\t'.format(ord(ch)), ord(ch)>>16)
✌       270c     0
⛹       26f9     0
☹       2639     0
☺       263a     0
☻       263b     0
?      1f602    1
?      1f60a    1

【讨论】:

  • 谢谢。我为相应的表情符号更正了我的问题中的飞机信息。
  • 似乎相同的逻辑不适用于零宽度连接器 (ZWJ) 表情符号,如?‍?‍?。对于相同情况如何处理它们有什么建议吗?
  • @abu 代码点位于平面内。表情符号可以由多个代码点组成。您的示例在平面 1/0/1/0/1 中使用了五个代码点。你想怎么处理?
  • @MarkTolonen 很抱歉没有说清楚。然后我将其标记为 1,0,1,0,1 或多平面。是否有基于平面 0 和 1 的表情符号列表以快速查找?谢谢。
【解决方案2】:

请始终提供minimum reproducible example 以帮助他人帮助您。

根据您在Unicode Planes 上的链接,

有 17 个平面,由数字 0 到 16 标识,对应于六位十六进制格式 (U+hhhhhh) 的前两个位置的可能值 00-10(以 16 为底)。

基于该解释,让我们编写一个函数来获取该信息。

# in the comments, we can use char = '?'
def unicode_to_plane(char: str) -> int:
    unicode_codepoint = ord(char)       # 128512
    hex_repr = hex(unicode_codepoint)   # '0x1f600'
    hex_digits = hex_repr[2:]           # '1f600'
    plane = 0                           # Assume plane is 0 until proven otherwise
    if len(hex_digits) > 4:             # The plane is 0 if hex representation is four hex digits or less
        hex_plane = hex_digits[:-4]     # '1' (take away the last four characters)
        plane = int(hex_plane, 16)      # 1 (convert hex characters to integer)
    return plane                        # 1

请注意根据wiki on Emoji

大多数(但不是全部)表情符号都包含在 Unicode 的补充多语言平面 (SMP) 中。

SMP 是平面 1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2018-06-04
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多