【问题标题】:Python 2.7: detect emoji from textPython 2.7:从文本中检测表情符号
【发布时间】:2015-02-03 02:12:47
【问题描述】:

我希望能够检测文本中的表情符号并查找他们的名字。

我没有使用 unicodedata 模块,我怀疑我不是 了解 UTF-8 约定。

我猜我需要将我的文档加载为 utf-8,然后将 unicode“字符串”分解为 unicode 符号。遍历这些并查找它们。

#new example loaded using pandas and encoding UTF-8                     
'A man tried to get into my car\U0001f648'          

type(test) = unicode

import unicodedata as uni
uni.name(test[0])
Out[89]: 'LATIN CAPITAL LETTER A'

uni.name(test[-3])
Out[90]: 'LATIN SMALL LETTER R'    

uni.name(test[-1])
ValueError                                Traceback (most recent call last)
<ipython-input-105-417c561246c2> in <module>()
----> 1 uni.name(test[-1])
ValueError: no such name

# just to be clear
uni.name(u'\U0001f648')
ValueError: no such name

我通过 google 查找了 unicode 符号,它是一个合法符号。 也许 unicodedata 模块不是很全面......?

我正在考虑从here 创建自己的查找表。 对其他想法感兴趣……这个似乎可行。

【问题讨论】:

  • 该字符串不包含您认为的内容。试试打印吧。
  • 是的,我没有在这里寻找表情符号,我只是抓住了一些东西......但我会
  • 我的意思是,它不是 Unicode 字符串。这是一个字节字符串,看起来像是包含一些Mojibakeunicodedata 如果你给它喂垃圾,它就不会工作。
  • mojibake..好的,就是这样......再次感谢..我会用更好的例子更新......我还必须首先避免使用mojibake..
  • unicodedata 可能没有一些最近添加的字符的记录。你可能只需要在它周围加上一个try/except

标签: unicode emoji


【解决方案1】:

我的问题在于将 Python2.7 用于 unicodedata 模块。 使用 Conda 我创建了一个 python 3.3 环境,现在 unicodedata 可以工作 正如预期的那样,我已经放弃了我正在研究的所有奇怪的黑客行为。

# using python 3.3
import unicodedata as uni

In [2]: uni.name('\U0001f648')
Out[2]: 'SEE-NO-EVIL MONKEY'

感谢 Mark Ransom 指出我最初有 Mojibake 正确导入我的数据。再次感谢您的帮助。

【讨论】:

    【解决方案2】:

    这是一种阅读您提供的链接的方法。它是从 Python 2 翻译过来的,所以可能有一两个小故障。

    import re
    import urllib2
    rexp = re.compile(r'U\+([0-9A-Za-z]+)[^#]*# [^)]*\) *(.*)')
    mapping = {}
    for line in urllib2.urlopen('ftp://ftp.unicode.org/Public/emoji/1.0/emoji-data.txt'):
        line = line.decode('utf-8')
        m = rexp.match(line)
        if m:
            mapping[chr(int(m.group(1), 16))] = m.group(2)
    

    【讨论】:

    • 谢谢马克,正则表达式很有帮助。我无法让我的实验处理 unicode。我将在今天晚些时候处理这​​个问题,并希望添加更多内容。
    猜你喜欢
    • 2017-10-09
    • 2018-04-04
    • 2019-12-26
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多