Python 2.7:从文本中检测表情符号

2024-03-29

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

我没有使用 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

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

我正在考虑制作我自己的查找表here ftp://ftp.unicode.org/Public/emoji/1.0/emoji-data.txt。 对其他想法感兴趣……这个似乎可行。


我的问题是使用 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 不是来自 正确导入我的数据。再次感谢你的帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 2.7:从文本中检测表情符号 的相关文章

随机推荐