从文本中提取表情符号

Dav*_*cía 9 python regex text-processing emoticons

我需要从使用Python文本中提取文本表情符号,我一直在寻找一些解决方案,这样做,但他们大多像这样这样只涉及简单的表情符号.我需要解析所有这些.

目前我正在使用一个表情符号列表,我为我处理的每个文本进行迭代,但这样效率很低.你知道更好的解决方案吗?也许是一个可以处理这个问题的Python库?

Luk*_*hne 4

最有效的解决方案之一是使用Aho\xe2\x80\x93Corasick 字符串匹配算法,它是针对此类问题设计的重要算法。(在未知文本中搜索多个预定义字符串)

\n\n

有可用于此目的的软件包。
\n https://pypi.python.org/pypi/ahocorasick/0.9
\n https://hkn.eecs.berkeley.edu/~dyoo/python/ahocorasick/

\n\n

编辑:\n还有更新的可用软件包(尚未尝试过其中任何一个)\n https://pypi.python.org/pypi/pyahocorasick/1.0.0

\n\n

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

\n\n

这是代码:

\n\n
import re, ahocorasick,random,time\n\n# search N words from dict\nN=3\n\n#file from http://norvig.com/big.txt\nwith open("big.txt","r") as f:\n    text = f.read()\n\nwords = set(re.findall(\'[a-z]+\', text.lower())) \nsearch_words = random.sample([w for w in words],N)\n\nA = ahocorasick.Automaton()\nfor i,w in enumerate(search_words):\n    A.add_word(w, (i, w))\n\nA.make_automaton()\n#test time for ahocorasic\nstart = time.time()\nprint("ah matches",sum(1 for i in A.iter(text))) \nprint("aho done in ", time.time() - start)\n\n\nexp = re.compile(\'|\'.join(search_words))\n#test time for re\nstart = time.time()\nm = exp.findall(text)\nprint("re matches",sum(1 for _ in m))\nprint("re done in ",time.time()-start)\n
Run Code Online (Sandbox Code Playgroud)\n