从文本中提取表情符号

Extract emoticons from a text

我需要使用 Python 从文本中提取文本表情符号,我一直在寻找一些解决方案来执行此操作,但大多数解决方案都像 this or this only cover simple emoticons. I need to parse all of them

目前我正在使用一个表情符号列表,我会为我处理的每个文本重复该列表,但这效率很低。你知道更好的解决方案吗?也许 Python 库可以解决这个问题?

最有效的解决方案之一是使用 Aho–Corasick string matching algorithm 并且是为此类问题设计的非平凡算法。 (在未知文本中搜索多个预定义字符串)

有可用的包。
https://pypi.python.org/pypi/ahocorasick/0.9
https://hkn.eecs.berkeley.edu/~dyoo/python/ahocorasick/

编辑: 还有更多最新的软件包可用(还没有尝试过) https://pypi.python.org/pypi/pyahocorasick/1.0.0

额外:
我用 pyahocorasick 做了一些性能测试,当在字典中搜索超过 1 个单词(2 个或更多)时,它比 python re 更快。

这是代码:

import re, ahocorasick,random,time

# search N words from dict
N=3

#file from http://norvig.com/big.txt
with open("big.txt","r") as f:
    text = f.read()

words = set(re.findall('[a-z]+', text.lower())) 
search_words = random.sample([w for w in words],N)

A = ahocorasick.Automaton()
for i,w in enumerate(search_words):
    A.add_word(w, (i, w))

A.make_automaton()
#test time for ahocorasic
start = time.time()
print("ah matches",sum(1 for i in A.iter(text))) 
print("aho done in ", time.time() - start)


exp = re.compile('|'.join(search_words))
#test time for re
start = time.time()
m = exp.findall(text)
print("re matches",sum(1 for _ in m))
print("re done in ",time.time()-start)