python - 从文本中提取表情符号

标签 python regex text-processing emoticons

我需要使用 Python 从文本中提取文本表情符号,我一直在寻找一些解决方案来执行此操作,但大多数解决方案都喜欢 thisthis只覆盖简单的表情符号。我需要解析 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)

关于python - 从文本中提取表情符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30370992/

相关文章:

python - 在 Linux 上为 Kivy 游戏的每个支持平台创建安装程序或可执行文件

python - 除了 to_pickle 之外,序列化 DataFrame 最快的方法是什么?

regex - 在 Rust 中将正则表达式捕获转换为 HashMap?

javascript - 如何从 html 标签中删除子字符串

c# - 快速文本预处理

python - 使用 sqlalchemy (ORM) 进行外连接

python - sklearn roc_auc_score 的阈值是多少

python - 使用 python 正则表达式进行日期匹配

Python Pandas 聚合文本字段中的空格分隔值

java - 计算字符串数组中每个单词最后一次出现后的单词数