python - 从字符串中提取所有表情符号并忽略 Python 中的 Fitzpatrick 修饰符(肤色等)

标签 python unicode utf-8 emoji

我最近遇到了一个问题,我需要提取一个字符串中的所有表情符号来计算特定表情符号的出现次数。 Emoji python package让我提取所有表情符号,但我总是得到特定的修饰符,例如作为单独的表情符号提取的肤色。我想忽略肤色和其他 Fitzpatrick 修饰符 Variant Selectors (参见 this page for typesbackground on Fitzpatrick from Wikpedia )。以下代码将导致 Fitzpatrick 修饰符被选为单独的表情符号(这不是我需要的):

import emoji
def extract_emojis(str):
  return list(c for c in str if c in emoji.UNICODE_EMOJI)

示例:这个表情符号 ❤️实际上由两部分组成,一颗心(Unicode 代码点:U+2764)和一个红色修饰符(Unicode 代码点:U+fe0f)。 print(repr('❤️'))结果:\u2764\ufe0f - 两个独立的 unicode 但只有一个表情符号。单独的第二个代码点本身没有意义,但它在 return list(c for c in str if c in emoji.UNICODE_EMOJI) 的列表中作为单独的表情符号返回。 .

最佳答案

这是一种忽略肤色和其他修饰符并将所有这些表情符号变体视为一个表情符号的解决方案。 answer来自 Martijn Pieters 的帮助为我的问题编写了以下解决方案:

import emoji
import unicodedata

def checkEmojiType(strEmo):
    if unicodedata.name(strEmo).startswith("EMOJI MODIFIER"):
        return False
    else:
        return True
def extract_emojis(str):
    return list(c for c in str if c in emoji.UNICODE_EMOJI and checkEmojiType(c))

[编辑] 然而..目前,上面的解决方案似乎不支持零宽度连接器(见下面的评论)。您可以使用以下代码自行测试:

n = '👨‍⚕️' #copy the medical emoji with zero-width joiner (http://www.unicode.org/emoji/charts/emoji-zwj-sequences.html). This should only fall back to a double-emoji if not otherwise available
#extract all emojis with the function from above
nlist = def_functions.extract_emojis(n)
for xstr in nlist:
    #print codepoints
    print('Emoji Extract: U+%04x' % ord(xstr))
for _c in n:
    #print all Unicode Codepoints directly
    print('Each Codepoint: U+%04x' % ord(_c))

这是输出:

EmojiExtract: U+1f468
EmojiExtract: U+2695
Each Codepoint: U+1f468
Each Codepoint: U+200d
Each Codepoint: U+2695
Each Codepoint: U+fe0f

Emoji Extract 没有加入这两个 Emoji(这在某种程度上是意料之中的)。

关于python - 从字符串中提取所有表情符号并忽略 Python 中的 Fitzpatrick 修饰符(肤色等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48340622/

相关文章:

python - Pandas 混合值列到字符串

javascript - 正则表达式匹配字母——包括非拉丁字母,但不包括表情符号

ios - 如何查看该字符串的真实内容?

java - JCIFS - SMBFileOutputStream 作为 UTF8

python - Django 模板 'ascii' 编解码器无法解码字节 0xd8

python - 从 sql 模式文件中提取 Table Create 语句

python - 如何将函数应用于 Python pandas 中的分组数据框?

python - 使用O(1)空间在python中自下而上的斐波那契

python - 激活snakemake中现有的conda环境

javascript - Unicode 'not perpendicular' 符号